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.
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);
-
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! Commented Mar 15, 2012 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 ... Commented Jan 27, 2014 at 11:59
-
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. Commented Apr 1, 2015 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. Commented Apr 1, 2015 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. Commented Aug 27, 2015 at 13:17
table.setDefaultEditor(Object.class, null);
-
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. Commented Mar 31, 2017 at 22:48
-
This works great with single selection. Wich was my requierement. Commented Jul 13, 2017 at 19:14
-
-
just add
table.setEnabled(false);
it works fine for me.
-
1
-
29
-
3
-
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. Commented Aug 27, 2015 at 14:28
-
1
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());
-
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.– sateeshCommented Jan 2, 2010 at 8: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.
-
5-1 random hacking at inappropriated locations is never an option Commented Jan 27, 2014 at 11:56
-
Please, can you explain why it is a "random hacking at inappropriated locations"? According to the setDefaultEditor() doc: "If editor is null, removes the default editor for this column class."– freesoftCommented Jan 27, 2014 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. Commented Jan 27, 2014 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?– freesoftCommented Jan 28, 2014 at 7: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. Commented Dec 18, 2014 at 12:35
I used this and it worked : it is very simple and works fine.
JTable myTable = new JTable();
myTable.setEnabled(false);
-
4
-
-
1Question is about editable not enabled. Enable does not solve the purpose– shaILUCommented Jun 26, 2020 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()));