113

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.

1

7 Answers 7

173

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);
8
  • 3
    This 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
    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 ...
    – kleopatra
    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
  • 1
    The 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
77
table.setDefaultEditor(Object.class, null);
4
  • 2
    This 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
  • This should be the ANSWER! Commented Nov 22, 2019 at 10:50
  • Overriding the editor is a much cleaner solution. Commented Aug 24 at 16:42
45

just add

table.setEnabled(false);

it works fine for me.

9
  • 1
    This is great if you're not using a custom table model!
    – Kingsolmn
    Commented Sep 10, 2012 at 18:13
  • 29
    But you won't be able to select a cell or row.
    – tianz
    Commented Mar 31, 2013 at 4:21
  • 3
    This will look "greyed out" in some L&F's.
    – jobukkit
    Commented Aug 10, 2013 at 9:45
  • 2
    That 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
    Works well for me!
    – Colby Cox
    Commented Dec 12, 2016 at 5:58
24

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());
3
  • 1
    You can't have a public void method return a boolean.
    – Geo
    Commented Jan 2, 2010 at 8:14
  • 1
    Also the method is isCellEditable
    – Matt
    Commented Jan 2, 2010 at 8:17
  • 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
    Commented Jan 2, 2010 at 8:20
10

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.

7
  • 5
    -1 random hacking at inappropriated locations is never an option
    – kleopatra
    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."
    – freesoft
    Commented 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.
    – kleopatra
    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?
    – freesoft
    Commented 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
3

I used this and it worked : it is very simple and works fine.

JTable myTable = new JTable();
myTable.setEnabled(false);
3
  • 4
    Selection will not be possible.
    – h3xStream
    Commented Jul 14, 2017 at 17:11
  • Simple and nice Solution Commented Jun 24, 2020 at 17:17
  • 1
    Question is about editable not enabled. Enable does not solve the purpose
    – shaILU
    Commented Jun 26, 2020 at 21:49
2

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()));

Not the answer you're looking for? Browse other questions tagged or ask your own question.