7

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 !

mKorbel
  • 108,320
  • 17
  • 126
  • 296
Azer Rtyu
  • 319
  • 3
  • 9
  • 18
  • Possible duplicate of [Java JTable - Make only one column editable](https://stackoverflow.com/questions/8167173/java-jtable-make-only-one-column-editable) – Line Aug 07 '18 at 17:06

6 Answers6

11

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 );
camickr
  • 308,339
  • 18
  • 152
  • 272
  • I can't override the isCellEditable(...) method, because my object extends the JTable class directly. – Azer Rtyu May 28 '13 at 21:14
  • i have used it but my cell is still editable, i have write inside isCellEditable return column==4 but all cells in the table are still editable, why? – mautrok Nov 13 '16 at 11:36
  • @mautrok I just edited the code. There was a typo. The class should return "boolean" not "Boolean". Maybe that is the problem. If not I can't help because I can't guess what your code looks like. If you need more help then ask a question and post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. – camickr Nov 13 '16 at 18:30
5

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;
    }
};
krystan honour
  • 5,934
  • 2
  • 31
  • 61
Amarnath
  • 8,121
  • 10
  • 51
  • 79
4

I found the solution using GUI :

  • Right-click on the table cells.
  • From popup menu, choose "Table Contents..".
  • Uncheck the editable check box for the column you want to make it non-editable.

enter image description here

Azer Rtyu
  • 319
  • 3
  • 9
  • 18
2

Here is solution if you are extending JTable directly:(place this code in constructor)

DefaultTableModel tableModel = new DefaultTableModel(data, colNames){

        @Override
        public boolean isCellEditable(int row, int column)
        {
            // make read only fields except column 0,13,14
            return column == 0 || column == 13 || column == 14;
        }
    };

this.setModel(tableModel);

P. Pandey
  • 108
  • 11
1
isCellEditable()

Here is the Javadoc: isCellEditable(int, int) is the method you want. If you are using a TableModel then this method can then be overridden in the subclass of the TableModel for that JTable instance.

blackpanther
  • 9,800
  • 10
  • 42
  • 74
0

If the jtable name is wordListJTable:

        String colTitles[] = {"#", "Word", "Definiton"};
        boolean[] isEditable = {false,true,true};
        wordTableModel = new DefaultTableModel(colTitles, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                // make read only fields except column 0,13,14
                return isEditable[column];
            }
        };
        // The 0 argument is number rows. 
        wordListJTable.setModel(wordTableModel);