问题描述
我有一个JTable
,我从db中获取数据并显示.列之一是blob
oject类型,它包含一个图像(如果存在,则为null).我还为JTable提供了一个双击事件处理程序,以便当用户仅双击图像时,将打开一个新的JFrame
,该JFrame
以全屏方式显示该图像.我面临的问题是,当用户双击图像时,图像将按预期显示在新窗口中,但是消失在JTable
中的imageIcon消失了,而是显示了一个值为javax.swing.ImageIcon@1ce5bc4d
的字符串.那么,双击事件完成后如何取回imageIcon呢?这是我的代码(列payment_receipt
是blob):
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();
}
推荐答案
我通过将ImageIcon.class更改为Icon.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;
}
这篇关于双击jtable中的imageicon会使imageIcon消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!