How to make a JTable
non-editable? I don't want my users to be able to edit the values in cells by double-clicking them.

- 54,432
- 29
- 203
- 199

- 1,255
- 2
- 8
- 6
-
1Much better answer available here: http://stackoverflow.com/questions/9919230/disable-user-edit-in-jtable – Paul Jun 01 '15 at 04:22
7 Answers
You can override the method isCellEditable and implement as you want for example:
//instance table model
DefaultTableModel tableModel = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
};
table.setModel(tableModel);
or
//instance table model
DefaultTableModel tableModel = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
//Only the third column
return column == 3;
}
};
table.setModel(tableModel);
Note for if your JTable disappears
If your JTable
is disappearing when you use this it is most likely because you need to use the DefaultTableModel(Object[][] data, Object[] columnNames)
constructor instead.
//instance table model
DefaultTableModel tableModel = new DefaultTableModel(data, columnNames) {
@Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
};
table.setModel(tableModel);

- 7,286
- 6
- 49
- 114

- 2,030
- 2
- 14
- 10
-
This is the most simple solution indeed for someone who just wants a default table model, only not editable. – Gnoupi Jun 28 '10 at 16:06
-
3This is also the solution for making double click events work correctly with JTable. A double click is normally consumed by a cell as it goes into edit mode, and this will keep a cell from doing that and instead send the double click to the JTable itself. Thanks for the solution, Nelson! – anchorite Mar 15 '12 at 15:39
-
2@Jop then something is wrong elsewhere in your code - a table _never_ disappears just because the table's model is not editable ... – kleopatra Jan 27 '14 at 11:59
-
Isn't they way the isCellEditable written supposed to return a true or false value and not return column == 3. – Doug Hauf Feb 27 '14 at 14:05
-
1@DougHauf it appears you don't know Java, or really any language that well since most languages use the same-ish syntax for boolean operations...that does return a boolean...I would recommend you go find a very basic Java tutorial and start from the beginning. Trying to jump into swing like this is going to end up with you learning everything wrong. – searchengine27 Apr 01 '15 at 16:37
-
1@DougHauf, the == is an alias for the Equals function that compare the objects (on the left and the right) and returns a boolean. I suppose you have used the statement if that evaluates booleans and usually is used with this kind of expressions for example if(a == b) do something. Try to print a==b or just assign this for a variable and understand the result. – nelson eldoro Apr 01 '15 at 20:12
-
1The above fix doesn't work for me either. It causes the table's column and data elements to disappear. My table is created with table = new JTable(tempTable, columnNames);, where tempTable is a String[][] and Column Names is a String[]. I believe the issue is caused by new DefaultTableModel() not specifying the data and column names for the table model. How do I specify those in the data model. I tried table.setModel (tableModel(tempTable, columnNames));, but that causes a "can not find symbol" error. – user1164199 Aug 27 '15 at 13:17
table.setDefaultEditor(Object.class, null);

- 5,751
- 4
- 46
- 54
-
2This is the best and easiest answer. Of course, you have to set the editor to null so you won't be able to edit it. – LEMUEL ADANE Mar 31 '17 at 22:48
-
This works great with single selection. Wich was my requierement. – Wesos de Queso Jul 13 '17 at 19:14
-
just add
table.setEnabled(false);
it works fine for me.

- 1,188
- 2
- 14
- 24
-
1
-
29
-
3
-
This is the simplest solution to what I understand the initial question to be asking. With regard to the inability to select cells and rows, would `setCellSelectionEnabled()` help? I suspect that the disabled default cell colors could be overridden at the very least with `setOpaque(false)` and using the containing JPanel background color. – Will Nielsen Jan 28 '14 at 16:23
-
2That works. Thanks. However, I would still like to know why nelson's data model method causes the table's column and data elements to disapper. – user1164199 Aug 27 '15 at 14:28
-
1
-
@WillNielsen This is the worst solution. It should have negative votes. – Silidrone May 24 '23 at 13:18
You can use a TableModel
.
Define a class like this:
public class MyModel extends AbstractTableModel{
//not necessary
}
actually isCellEditable()
is false
by default so you may omit it. (see: http://docs.oracle.com/javase/6/docs/api/javax/swing/table/AbstractTableModel.html)
Then use the setModel()
method of your JTable
.
JTable myTable = new JTable();
myTable.setModel(new MyModel());

- 32,039
- 22
- 142
- 171

- 5,423
- 2
- 28
- 42
-
1
-
1
-
While the approach you specify works, there is no such method as isEditable in the AbstractTableModel. What exists is the method isCellEditable(int,int) which takes rowIndex and coulmnIndex as parameters. The user can selectively enable/disable editing for a particular row/column by overriding "isCellEditable" method or can use the default implementation to disable editing for all cells. – sateesh Jan 02 '10 at 08:20
If you are creating the TableModel automatically from a set of values (with "new JTable(Vector, Vector)"), perhaps it is easier to remove editors from columns:
JTable table = new JTable(my_rows, my_header);
for (int c = 0; c < table.getColumnCount(); c++)
{
Class<?> col_class = table.getColumnClass(c);
table.setDefaultEditor(col_class, null); // remove editor
}
Without editors, data will be not editable.

- 1,114
- 3
- 17
- 26
-
5
-
Please, can you explain why it is a "random hacking at inappropriated locations"? According to the [setDefaultEditor() doc](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#setDefaultEditor%28java.lang.Class,%20javax.swing.table.TableCellEditor%29): "If editor is null, removes the default editor for this column class." – freesoft Jan 27 '14 at 14:35
-
yeah, and falls back to the default for Object ;-) So you don't really disable editing (just accidentally if one of the columns is of type Object), also you are missing any columns that might have a custom editor. There _is_ api meant to be implemented to control cell editability, and that's the model's. Everything else is hacking and as such inappropriate. – kleopatra Jan 27 '14 at 14:52
-
I see your point. But what I understood from the api is that, if I create a table in the fast way (with "new JTable(Vector, Vector)"), then the table will have default editors for all of its columns. So, if I run "table.setDefaultEditor(column, null)", then I will remove the default editor from the column, so the column will not have any editor, and the column will not be editable. Will it be? – freesoft Jan 28 '14 at 07:35
-
I'd created the table that way `new JTable(Vector, Vector)` and worked for me. I need no editor in no cell, so you have my vote. – Dani Sancas Dec 18 '14 at 12:35
-
so this work if we didn't add any custom editors for columns. and that would be the case most of the times. why would someone add custom editor and then disable it. only for one case that editing mode is getting enabled and disabled during app. journey. otherwise you are safe?! – hasan Feb 22 '15 at 12:20
-
A tweak on this approach allows for selective enable/disable edits by column. – Felype Apr 01 '15 at 16:22
I used this and it worked : it is very simple and works fine.
JTable myTable = new JTable();
myTable.setEnabled(false);

- 107
- 1
- 3
- 10
-
4
-
-
1Question is about editable not enabled. Enable does not solve the purpose – shaILU Jun 26 '20 at 21:49
create new DefaultCellEditor class :
public static class Editor_name extends DefaultCellEditor {
public Editor_name(JCheckBox checkBox) {
super(checkBox);
}
@Override
public boolean isCellEditable(EventObject anEvent) {
return false;
}
}
and use setCellEditor :
JTable table = new JTable();
table.getColumn("columnName").setCellEditor(new Editor_name(new JCheckBox()));

- 1,544
- 19
- 25