Take the 2-minute tour ×

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 []:

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[]) {
            if (o[i] == null) {
                o[i] = new String();
        fireTableRowsInserted(data.size() - 1, data.size() - 1);

    public MyModel() {
        data = new Vector<Object>();
        //Adding rows
        for (Object[] r : row) {

    public Class getColumnClass(int columnIndex) {
        if (columnIndex == 0) {
            return getValueAt(0, columnIndex).getClass();
        } else {
            return super.getColumnClass(columnIndex);

    public int getColumnCount() {
        // TODO Auto-generated method stub
        return 0;

    public int getRowCount() {
        // TODO Auto-generated method stub
        return 0;

    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) {

    public void showGUI() {
        JTable table = new JTable();
        table.setModel(new MyModel());//invoking our custom model
        table.setDefaultRenderer(JLabel.class, new Renderer());// for the rendering of cell
        JScrollPane jp = new JScrollPane(table);
        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.


share|improve this question

3 Answers 3

up vote 4 down vote accepted

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

  1. Why bother storing JLabel instances in your TableModel. If you want to render the contents of your TableModel by using JLabel instances, you can do this in the renderer
  2. Your TableModel implementation returns 0 for both the model and row count (see your getColumnCount() and getRowCount() methods)
  3. Your getValueAt always returns null, leading to a NullPointerException in your getColumnClass method since you use getValueAt(0, columnIndex).getClass()
  4. Please call the contents of your main method on the EDT

Solve these first and see what happens

share|improve this answer
hehehe put both answers together and right now will be there valuable answer, +1 –  mKorbel Jan 26 '12 at 23:54
@mKorbel Indeed. It seems we both missed some points (or left it for the other to share the upvotes). +1 for your answer –  Robin Jan 26 '12 at 23:56
@Robin Well the code is a mix of the example and the table I'm actually using and did it before going to bed, was really tired and didn't bother about unimplemented methods. The first point is absolutely right and lead me to the solution. I'm just checking the column index in the Renderer and return a JLabel instead of a String. Thanks! +1 and correct answer :) –  AlejandroVK Jan 27 '12 at 8:46
@mKorbel Hi, I don't really get your first 2 points. And the 3rd, what is the problem of using Object[][] in an AbstractTableModel, specification says nothing wrong about that...docs.oracle.com/javase/tutorial/uiswing/components/…;, please correct me if I'm wrong. Anyway thanks for you time and help :), really appreciate it. –  AlejandroVK Jan 27 '12 at 8:48

1) you can't put JLabel to the Cell, Cell by default returns JComponent/JLabel

2) you can't put Icon to JLabel, put Icon to JTable directly

3) your AbstractTableModel has mistake ýou declare for Object[][]/Object[] and Model is based on Vector, by changing Object[][] to Vector<Vector<Object>> / Object[] to Vector<String> you can remove this issue

4) don't use AbstractTableModel, no reason if you can't restrict or override methods from DefaultTableModel

share|improve this answer

If use JXTreeTable, i will set column 0 is a label type

ArrayNode dataRow = new ArrayNode(new Object[]{new JLabel("text",icon), ...});
// add Root --> Set TreeTableModel


jxTreeTable.setTreeCellRenderer(new TreeCellRenderer() {
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        AbstractMutableTreeTableNode data = (AbstractMutableTreeTableNode) value;
        if (data.isLeaf()) {// Is Leaf as Label 
            return (JLabel) data.getValueAt(0); // Value 0 is a label
        } else if(data.getValueAt(0)!=null){ // Is Root
            return new JLabel(data.getValueAt(0).toString());
        }else{ // IsRoot, but is Cell Null Value
            return new JLabel("");
share|improve this answer
Missing a } bracket. Alo, would You care to comment, how is it actually related to the question? –  Kamiccolo Dec 17 '13 at 16:25
Thanks! Kamiccolo –  Vu Duy Hao Dec 23 '13 at 7:49

Your Answer


