我正在尝试将图像添加到JTable中的列,但填充列时它们显示为空白。
我重写了getColumnClass。这是表格声明......
laneTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null}
},
new String [] {
"Lane ID", "Status", "Traffic", "Gate Arm", "Lane Indicator", "Temperature"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
};
@Override
public Class getColumnClass(int column)
{
if (column == 4)
{
return ImageIcon.class;
}
return Object.class;
// other code; default to Object.class
}
});
很明显,第4列是我要显示图像的列。我用..填充我的桌子。
for(int i = 0; i < 200; i++){
ImageIcon icon = new ImageIcon("Images/test.jpg");
laneTable.getModel().setValueAt(icon,i,4);
}
我的图片路径是/src/Images/test.jpg,以防我的路径名不正确... 上周我一直在处理这个问题,我无法弄清楚为什么这些图像在我的表格中一直显示为空白。
想法?
编辑这里是我猜的SSCCE。不知道它是否会起作用..
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class LaneGUI extends javax.swing.JApplet implements Runnable {
private Connection connection;
private XMLParser xmlParser;
private laneData laneData;
private SiteData siteData;
private Thread connectionThread;
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
ImageIcon icon = new ImageIcon("Images/test.jpg");
for(int i = 0; i < 200; i++){
laneTable.getModel().setValueAt(icon,i,4);
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
laneTable = new javax.swing.JTable();
laneTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null}
},
new String [] {
"Lane ID", "Status", "Traffic", "Gate Arm", "Lane Indicator", "Temperature"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
};
@Override
public Class getColumnClass(int column)
{
if (column == 4)
{
return ImageIcon.class;
}
return Object.class;
// other code; default to Object.class
}
});
laneTable.setRowHeight(100);
jScrollPane1.setViewportView(laneTable);
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addContainerGap())
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable laneTable;
// End of variables declaration
}
编辑在新标签中测试图像,图像在标签内正确显示,但在表格内仍然是空白。
答案 0 :(得分:1)
空白图片可能不是路径问题,而是渲染问题。查看该列的CellRenderer。它是使用默认渲染器还是您正在做一些特殊的事情。
尝试打印出路径以确保它进入单元格。另请尝试从JTable
外部访问该路径,以确保根本没有路径问题。