双击 JTable 中的 ImageIcon 会使图标消失。

huangapple go评论89阅读模式
英文:

double clicking on an imageicon in jtable makes the imageIcon dissapear

问题

以下是翻译好的代码部分:

String query = "Select payment_date, payment_amt, payment_receipt from fooTable";
conn = dbTest.connect();
try {
    PreparedStatement ps = conn.prepareStatement(query);
    ResultSet rs = ps.executeQuery();
    ResultSetMetaData rsMetaData = (ResultSetMetaData) rs.getMetaData();
    int columns = rsMetaData.getColumnCount();

    // 列名
    Vector<String> columnNames = new Vector<String>();
    for (int i = 1; i <= columns; i++) {
        columnNames.add(rsMetaData.getColumnName(i));
    }

    // 表格数据
    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    while (rs.next()) {
        Vector<Object> vector = new Vector<Object>();
        for (int columnIndex = 1; columnIndex <= columns; columnIndex++) {
            if (columnIndex == 3) { // 图片列
                Blob blob = rs.getBlob("payment_receipt");
                if (blob != null) {
                    int blobLength = (int) blob.length();
                    byte[] bytes = blob.getBytes(1, blobLength);
                    blob.free();
                    BufferedImage img = null;
                    try {
                        img = ImageIO.read(new ByteArrayInputStream(bytes));
                        ImageIcon icon = new ImageIcon(img);
                        vector.addElement(icon);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else {
                vector.addElement(rs.getObject(columnIndex));
            }
        }
        data.add(vector);
    }
    table = new JTable(new DefaultTableModel(data, columnNames)) {
        @Override
        public Class<?> getColumnClass(int column) {
            if (column == 2) {
                return ImageIcon.class;
            } else {
                return Object.class;
            }
        }
    };

    table.setRowHeight(200); // 显示收据
    table.setPreferredScrollableViewportSize(this.getPreferredSize());
    table.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent mouseEvent) {
            JTable table = (JTable) mouseEvent.getSource();
            Point point = mouseEvent.getPoint();
            int row = table.rowAtPoint(point);
            int column = table.columnAtPoint(point);
            if (mouseEvent.getClickCount() == 2 && table.getSelectedRow() != -1) {
                if (column == 2) {
                    Object obj = table.getValueAt(row, column);
                    if (obj != null) {
                        if (obj.getClass().equals(ImageIcon.class)) {
                            ImageIcon icon = (ImageIcon) obj;
                            jf = new JFrame();
                            jf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                            jf.setBounds(GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds());
                            jf.add(new JLabel(icon));
                            jf.setVisible(true);
                            loadPaymentsTable();
                        } else {
                            JOptionPane.showMessageDialog(null, "无可用图像");
                            loadPaymentsTable();
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "无可用图像");
                        loadPaymentsTable();
                    }
                }
            }
        }
    });
} catch (SQLException e) {
    e.printStackTrace();
}

注意:代码中的注释也已经翻译为中文。

英文:

I have a JTable where i fetch data from db and display. One of the columns is a blob oject type and it contains a image(if present ,else null ).
I also have a double click event handler for the JTable so that when user double clicks only on the image,a new JFrame is opened which shows the image in full screen.
The problem i am facing is that when the user double clicks the image is shown in new window as expected, however the imageIcon which was earlier visible in the JTable gets dissapeared and instead a string is shown having value as javax.swing.ImageIcon@1ce5bc4d. So how do i get back the imageIcon after completion of the double click event ?
Here is my code(the column payment_receipt is the blob):

String query = &quot;Select payment_date,payment_amt,payment_receipt from fooTable&quot;;
conn = dbTest.connect();
try {
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
ResultSetMetaData rsMetaData = (ResultSetMetaData) rs.getMetaData();
int columns = rsMetaData.getColumnCount();
//names of columns
Vector&lt;String&gt;columnNames = new Vector&lt;String&gt;();
for(int i=1;i&lt;=columns;i++)
{
columnNames.add(rsMetaData.getColumnName(i));
}
//data of table
Vector&lt;Vector&lt;Object&gt;&gt; data = new Vector&lt;Vector&lt;Object&gt;&gt;();
while (rs.next()) {
Vector&lt;Object&gt; vector = new Vector&lt;Object&gt;();
for (int columnIndex = 1; columnIndex &lt;= columns; columnIndex++) 
{
if (columnIndex ==3 ) //starting index is 1
{  // image column
Blob blob = rs.getBlob(&quot;payment_receipt&quot;);
if(blob!=null)
{
int blobLength = (int) blob.length();  
byte[] bytes = blob.getBytes(1, blobLength);
blob.free();
BufferedImage img=null;
try {
img = ImageIO.read(new ByteArrayInputStream(bytes));
icon = new ImageIcon(img);
vector.addElement(icon);
} 
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} 
else 
{
vector.addElement(rs.getObject(columnIndex));
}
}
data.add(vector);
}
table = new JTable(new DefaultTableModel(data,columnNames))
{
@Override
public Class&lt;?&gt; getColumnClass(int column) {
if(column==2)
{
return ImageIcon.class;
}
else
return Object.class;
}
};
table.setRowHeight(200);//to display the receipt
table.setPreferredScrollableViewportSize(this.getPreferredSize());
table.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent)
{
JTable table = (JTable)mouseEvent.getSource();
Point point = mouseEvent.getPoint();
int row = table.rowAtPoint(point);//the index of row where the double click event too place
int column = table.columnAtPoint(point);
if(mouseEvent.getClickCount()==2 &amp;&amp; table.getSelectedRow() !=-1)
{
if(column==2)//image column,so open image in full screen
{
Object obj = table.getValueAt(row, column);
if(obj!=null)
{
if(obj.getClass().equals(ImageIcon.class))
{
ImageIcon icon = (ImageIcon)obj;
jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
jf.setBounds(GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds());
jf.add(new JLabel(icon));
jf.setVisible(true);
loadPaymentsTable();
}
else
{
JOptionPane.showMessageDialog(null, &quot;No image available&quot;);
loadPaymentsTable();
}
}
else
{
JOptionPane.showMessageDialog(null, &quot;No image available&quot;);
loadPaymentsTable();
}
}
}
}
});
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

答案1

得分: 0

可能是在两个JFrame中使用相同的ImageIcon导致了问题;为新窗口复制图像。

Java组件使用的本地资源,尤其是图像和BufferedImage对于重复使用和销毁是敏感的。

英文:

It's possible using the same ImageIcon in two JFrames is causing the issue; make a copy of the image for the new window.

Native resources used by java components, especially images and BufferedImages are sensitive to re-use and disposal.

答案2

得分: 0

我通过将 ImageIcon.class 更改为 Icon.class 来获得解决方案:

    @Override
    public Class<?> getColumnClass(int column) {
        if (column == 2) {
            return Icon.class;
        } else {
            return Object.class;
        }
    }
英文:

i got the solution by changing the ImageIcon.class to Icon.class here:

@Override
public Class&lt;?&gt; getColumnClass(int column) {
if(column==2)
{
return Icon.class;
}
else
return Object.class;
}

huangapple
  • 本文由 发表于 2020年4月9日 02:32:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61107627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定