I'm trying to add an image to a column in a JTable but they appear blank when the columns are filled.
I am overriding the getColumnClass. Here is the table declaration...
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
}
});
so obviously, column 4 is my column where images are to be displayed. I am populating my table with..
for(int i = 0; i < 200; i++){
ImageIcon icon = new ImageIcon("Images/test.jpg");
laneTable.getModel().setValueAt(icon,i,4);
}
The path to my images is /src/Images/test.jpg in case my pathname is somehow incorrect... Ive been dealing with this for the last week and i CANNOT figure out why these images keep showing up blank in my table.
Ideas?
edit here is what I guess is an SSCCE. No idea if it will work or not..
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开发者_C百科.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
}
edit Tested the images in a new label, the images show correctly inside a label, but are still blank inside of the table.
A blank image might not be a path issue but a rendering issue. Look at your CellRenderer for that column. Is it using the default renderer or are you doing something special.
Try just printing out the path to make sure it is getting in the cell. Also try accessing that path from outside the JTable
to make sure there is not a path issue at all.
精彩评论