Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a column non-editable in a JTable

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 !

like image 291
Azer Rtyu Avatar asked May 28 '13 20:05

Azer Rtyu


People also ask

How do you make a JTable column not movable?

setReorderingAllowed() method and set the value as false.

Is JTable editable by default?

The isCellEditable() method of JTable (or the TableModel) controls whether a cell is editable or not. By default it just return "true". So you can override the method to return a boolean value that is set by your "Modify" button.

How do you make a column editable?

If you want to be able to make a table column editable right click on the table and select on Customizers > Table Customizers > Editable and check whatever columns you want to make editable. The same applies to hiding columns.


2 Answers

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 );
like image 164
camickr Avatar answered Oct 11 '22 19:10

camickr


Disabling user edits on JTable for multiple columns

JTable table = new JTable(10, 4) {
    @Override
    public boolean isCellEditable(int row, int column) {
        return column == 3 || column==4 || column==5 ? true : false;
    }
};
like image 22
Amarnath Avatar answered Oct 11 '22 17:10

Amarnath