Make a column non-editable in a JTable

Azer Rtyu picture Azer Rtyu · May 28, 2013 · Viewed 25k times · Source

I created a MasterDetail Simple Form using Netbeans, and I have a JTable which is related to the database.

I want to edit a column in this JTable to make it non-editable.

I Googled about it and this all I can find :

this.masterTable.getColumn("Validation").setEditable(false);

which won't work with me !

Answer

camickr picture camickr · May 28, 2013

Override the isCellEditable(...) method of the TableModel.

DefaultTableModel model = new DefaultTableModel(...)
{
    @Override 
    public boolean isCellEditable(int row, int column)
    {
        // add your code here
    }
}

JTable table = new JTable( model );