gpt4 book ai didi

java - 双击 jtable 中的 imageicon 会使 imageIcon 消失

转载 作者:行者123 更新时间:2023-12-01 17:31:48 28 4
gpt4 key购买 nike

我有一个JTable,我可以在其中从数据库获取数据并显示。其中一列是 blob 对象类型,它包含一个图像(如果存在,否则为 null )。我还有一个用于 JTable 的双击事件处理程序,这样当用户仅双击图像时,就会打开一个新的 JFrame,它会全屏显示图像。我面临的问题是,当用户双击图像时,图像会按预期显示在新窗口中,但是之前在 JTable 中可见的 imageIcon 会消失,而是显示一个字符串,其值为javax.swing.ImageIcon@1ce5bc4d。那么双击事件完成后如何取回 imageIcon 呢?这是我的代码( payment_receipt 列是blob):

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();

//names of columns
Vector<String>columnNames = new Vector<String>();

for(int i=1;i<=columns;i++)
{
columnNames.add(rsMetaData.getColumnName(i));
}

//data of table
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 ) //starting index is 1
{ // image column
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));
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<?> 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 && 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, "No image available");
loadPaymentsTable();
}
}
else
{
JOptionPane.showMessageDialog(null, "No image available");
loadPaymentsTable();
}
}
}
}

});
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

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

Java 组件使用的 native 资源,尤其是图像和 BufferedImages 对重用和处置很敏感。

关于java - 双击 jtable 中的 imageicon 会使 imageIcon 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61107627/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com