英文:
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 = "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();
}
答案1
得分: 0
可能是在两个JFrame
中使用相同的ImageIcon
导致了问题;为新窗口复制图像。
Java组件使用的本地资源,尤其是图像和BufferedImage对于重复使用和销毁是敏感的。
英文:
It's possible using the same ImageIcon
in two JFrame
s 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<?> getColumnClass(int column) {
if(column==2)
{
return Icon.class;
}
else
return Object.class;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论