Hey there!

I have been trying to get all my JTable cells to be not Editable so you can't change the info displayed on the JTable. It sounds easy to do but I cant seem to do it.

Any help would be appreciated
Thanks!!

Dani AI

Generated

If you want the table to look and behave normally (selection, copy, sorting, keyboard navigation) but not allow edits, avoid disabling the table. Instead, either make the model report cells as non-editable, or remove editors from the table.

  • If you are using DefaultTableModel, override its editability:
DefaultTableModel model = new DefaultTableModel(data, columns) {
  @Override
  public boolean isCellEditable(int row, int column) {
    return false; // all cells read-only
  }
};
JTable table = new JTable(model);
  • Alternatively, remove cell editors so clicking never starts editing:
JTable table = new JTable(model);
table.setDefaultEditor(Object.class, null);          // most columns
table.setDefaultEditor(Boolean.class, null);         // checkboxes too
table.setDefaultEditor(Number.class, null);          // numbers, if needed

Tip: if only certain columns should be editable, have isCellEditable return true only for those columns, or leave editors installed only for the column classes you want editable.

Caution: setEnabled(false) makes the table non-interactive (no selection or focus), which is rarely desirable for a read-only view.

For deeper details, see the JTable editing section in the official tutorial How to Use Tables and the Javadoc for JTable and DefaultTableModel.

Recommended Answers

All 8 Replies

Hi everyone,
It really depends on what your default editor component is
if your component is say a JTextArea the do the following

textarea1 = (JTextArea)table1.getEditorComponent();
   textarea1.setEditable(false);

One more thing do not forget to add a table listener so that no matter which cell is clicked it cannot be edited or in your initialization you can do a loop an set all cells to uneditble mode

I you have any other questions on this subject post them here

Richard West

Hi,
I guess you can implement the isEditable() method in your table model and return false and you table will not be editable. Let's see a quick example

public class MyTableModel extends AbstractTableModel{

public void isEditable(){
return false;
}
}

Then you have a table and you set its model to a MyTableModel object eg.

JTable table = new JTable();
table.setModel(new MyTableModel());

Remember to implement MyTableModel well so that it will have some data. I hope this helps

Well, it was helpful for me now in 2011 :)

Well I made my JTable non-editable by disabling it, i.e., using setEnabled(false). It works in my situation.

and, if you answered this seven years ago, it might even have mattered to the original poster.

i dont think u understand the concept of a forum like this one. it doesnt matter how old the post is. if the matter is still relevant it is most appreciated to post a solution.

me helped this a lot so thanks for the solution.

Smolof, thumbs up to you ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.