java - 双击 jtable 中的 imageicon 会使 imageIcon 消失
问题描述
我有一个JTable
从数据库获取数据并显示的地方。其中一列是blob
oject 类型,它包含一个图像(如果存在,否则为 null)。我还有一个 JTable 的双击事件处理程序,这样当用户只双击图像时,JFrame
就会打开一个新的全屏显示图像的事件处理程序。我面临的问题是,当用户双击图像时,图像会按预期显示在新窗口中,但是之前在图像中可见的 imageIconJTable
会消失,而是显示一个字符串,其值为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();
}
解决方案
ImageIcon
在两个s 中使用相同的可能JFrame
会导致问题;为新窗口复制图像。
java 组件使用的本机资源,尤其是图像和 BufferedImages 对重用和处置很敏感。
推荐阅读
- python - Python 无法检测到安装在我的系统中的 OpenCv
- python-3.x - 错误“对象没有属性”应用于状态栏不显示状态
- python - 根据另一个系列中的条件值创建熊猫系列
- php - 2个字符串位置之间的PHP变量
- wordpress - 如何计算 Woocommerce 中的储蓄总额
- powershell - 如何在 windows server 2008 R2 中获取总虚拟内存
- node.js - 无服务器部署:发生可恢复错误(写入 ECONNRESET),休眠约 5 秒。尝试 1 的 4
- c# - 服务器中的另一个进程正在使用 Excel 文件
- docker - tiangolo/uwsgi-nginx-flask 目录结构
- javascript - 如何在反应JavaScript中将id传递给navlink