2

I have a jTable calendar that works just fine and I just want the user not be able to edit anything. How can I do it?

Thanks a lot!!

PS: I use netbeans 6.9.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Cristian
  • 21
  • 1
  • 1
  • 2

5 Answers5

9
DefaultTableModel tableModel = new DefaultTableModel() {

    @Override
    public boolean isCellEditable(int row, int column) {
        //all cells false
        return false;
    }
};

table.setModel(tableModel);
Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
imanzi
  • 91
  • 1
  • 2
  • @Matthew - when I try to implement this, it makes my table disappear. Does it matter that my table is in a JScrollPane? Like this: `JScrollPane editPatDocScrollPane = new JScrollPane(); JTable editPatDocTable = new JTable(data, columnNames); editPatDocScrollPane.setViewportView(editPatDocTable);` – Jona Apr 14 '14 at 22:05
  • @Jona I don't see why that should make any difference. The cells are still there, they're just read-only. If you need help with your situation I suggest you open a new question with a self-contained example that demonstrates the disappearing table. – Matthew Strawbridge Apr 15 '14 at 12:34
  • I had the same problem. What I did was JTable table = new JTable(data, columnNames); and then setModel. But that overrides your data and columnNames ! so you have to initialize your defaultTableModel with your data and columnNames and then pass it to your JTable. – Adrien Gorrell Apr 28 '14 at 13:24
3

Simply return false in the isCellEditable() implementation of your TableModel

http://download.oracle.com/javase/6/docs/api/javax/swing/table/TableModel.html#isCellEditable%28int,%20int%29

  • Will that keep people from clicking on the values and changing them. That probably keeps people from saving data back to a file if they check with a conditional statement. – Doug Hauf Feb 27 '14 at 14:06
  • Let me ask this. If you implement a JTable model you are basically creating the methods that will search through the two-dimensional array. So if you set isCellEditable = false then you just would not write any data back to the array. And when you click on say a save button then it will write the new info in the JTable to the object array or if you have a keylistener on any of the keys. If key changes write new values. What if you do not want them to be able to click in the boxes and even change the values at this point. – Doug Hauf Feb 27 '14 at 14:09
3

in your table model (which extends AbstractTableModel), you need to implement as such:

     public class MyTableModel extends AbstractTableModel {
           //table is not editable
            public boolean isCellEditable() {
                return false;
            }
     }
Dhruv Gairola
  • 9,102
  • 5
  • 39
  • 43
  • 1
    This is a good solution. But note: the TableModel might actually extend DefaultTableModel in Cristian's code. There not typically a good reason to extend AbstractTableModel when you can just extend DefaultTableModel. – jzd Jan 15 '11 at 14:12
  • 1
    well, normally i don't use DefaultTableModel as you cannot specify your own datatype and more importantly, it uses vectors of vectors to store things which can get annoying. but yeah, for plain old convenience, DefaultTableModel is the easier choice for such a small task since it provides most of the implementations! personally, i avoid it though. – Dhruv Gairola Jan 15 '11 at 16:31
3

Okay, maybe this is a new feature, but I found that I could use:

JTable table = new JTable();
...
table.setEnabled(false);

And that did the job for me.

rtfminc
  • 6,243
  • 7
  • 36
  • 45
0

Try this code; it's simple and easy to understand.

 DefaultTableModel  model =new DefaultTableModel(6,8);
 table = new JTable(model){

            private static final long serialVersionUID = 2485117672771964339L;

            @Override
                public boolean isCellEditable ( int row, int column )
                {
                    return editable;
                }
      };
james.garriss
  • 12,959
  • 7
  • 83
  • 96
Alejandro
  • 289
  • 3
  • 10