I’m running into a weird thingy here. I’m trying to add a JLabel inside a JTable cell. Label icon may change based on some criteria. I created a dummy project following specs from here: http://javanepal.wordpress.com/2010/06/30/adding-jlabel-in-jtable/
And it works fine. I changed the TableModel to extend from AbstractTableModel instead of DefaultTableModel and when adding a row now I get this if I inspect the Object []:
[javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,
flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,
disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,
iconTextGap=4,labelFor=,text=Row 1,Col1,verticalAlignment=CENTER,
verticalTextPosition=CENTER], whatever....]
The code is this, for the TableModel:
public class MyModel extends javax.swing.table.AbstractTableModel {
Object[][] row = {{new JLabel("Row 1 Col 1"), "Row 1 Col 2", "Row 1 Col3"},
{new JLabel("Row 2 Col 1"), "Row 2 Col 2", "Row 2 Col3"},
{new JLabel("Row 3 Col 1"), "Row 3 Col 2", "Row 3 Col3"},
{new JLabel("Row 4 Col 1"), "Row 4 Col 2", "Row 4 Col3"}};
Object[] col = {"Column 1", "Column 2", "Column 3"};
protected Vector<Object> data;
public void addRow(Object o[]) {
for (int i = 0; i < o.length; i++) {
if (o[i] == null) {
o[i] = new String();
}
}
data.addElement(o);
fireTableRowsInserted(data.size() - 1, data.size() - 1);
}
public MyModel() {
super();
data = new Vector<Object>();
//Adding rows
for (Object[] r : row) {
addRow(r);
}
}
@Override
public Class getColumnClass(int columnIndex) {
if (columnIndex == 0) {
return getValueAt(0, columnIndex).getClass();
} else {
return super.getColumnClass(columnIndex);
}
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getValueAt(int arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
}
Here is the code for the testing class:
public class MyTableTest extends JFrame {
public MyTableTest(String title) {
super(title);
showGUI();
}
public void showGUI() {
JTable table = new JTable();
table.setModel(new MyModel());//invoking our custom model
//loadMessages(table);
table.setDefaultRenderer(JLabel.class, new Renderer());// for the rendering of cell
JScrollPane jp = new JScrollPane(table);
getContentPane().add(jp);
setVisible(true);
setSize(500, 300);
}
public static void main(String[] args) {
MyTableTest t = new MyTableTest("Table Custom");
}
}
I’m really confused why this happens, the message is not very clarifying regarding the issue.
Thanks!
You did not post the message, nor what the problem is. But just looking at your code I can already remark a number of issues
JLabelinstances in yourTableModel. If you want to render the contents of yourTableModelby usingJLabelinstances, you can do this in the rendererTableModelimplementation returns 0 for both the model and row count (see yourgetColumnCount()andgetRowCount()methods)getValueAtalways returnsnull, leading to aNullPointerExceptionin yourgetColumnClassmethod since you usegetValueAt(0, columnIndex).getClass()Solve these first and see what happens