Can not get the index of the column with the table.getColumn method using the Custom Model table

advertisements

I have created a custom TableModel using AbstractTableModel. I am able to populate my JTable. But my JTable has a button column say "Button1". So I am using CellRenderer method to add buttons to column and CellEditor to add actions, but I am getting exception at LINE:3.

CustomModelForTable customTableModel = new CustomModelForTable(colNames, data);
tableA = new JTable(customTableModel);

**LINE:3**
tableA.getColumn("Button1").setCellRenderer(new JButtonRendererClass());
tableA.getColumn("Button1").setCellEditor(new ButtonEditor(new JCheckBox()));

I am getting the following error.

java.lang.IllegalArgumentException: Identifier not found
at javax.swing.table.DefaultTableColumnModel.getColumnIndex(DefaultTableColumnModel.java:265)

I am getting this error because I am not able to get the Column from my custom Table. But can some one help me out with this issue.

I am using the following source to perform this task. In this source they are using DefaultTableModel where as in my case I am using AbstractTableModel.


In order to retrieve columns by identifier, you have to set one using TableColumn.setIdentifier().

EDIT:

Note that according to specs of TableColumn.getIdentifier():

If the identifier is null, getIdentifier() returns getHeaderValue as a default.

That is how it works in the linked example.

EDIT:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.GridLayout;

public class TableDemo extends JPanel {
    public TableDemo() {
        super(new GridLayout(1,0));

        JTable table = new JTable(new MyTableModel());

        JScrollPane scrollPane = new JScrollPane(table);

        add(scrollPane);

        table.getColumn("Column1").setCellRenderer(new TestCellRenderer());
        table.getColumn("Column2").setCellRenderer(new TestCellRenderer());
    }

    class TestCellRenderer extends DefaultTableCellRenderer{ }

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = { "Column1", "Column2" };
        private Object[][] data = { { "1", "1" }, { "2", "2" } };

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("TableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        TableDemo newContentPane = new TableDemo();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}