0

I am a student, I have a program that I have been working on for about a month now. I have to hand it in on Wednesday and everything works fine but I just want to tidy up the interface.

I have a JTable which I use to display records after the user has sorted them or to display records that match a criteria as input by the user when searching but what I dont like about the JTable is that the user can click into the cells and delete the text and write something else. Although I suppose it does'nt matter because the user is not actually changing the records, the records are only put into a two dimensional array in order to display them in the JTable, the records are actually stored in a linked list until the program is closed and then saved to file, but all the same I would like to remove the ability to click into the cells if possible.

I have tried to prevent editing the cells by using

table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(false);
table.cellSelectionEnabled(false);

But they don't have the effect of preventing cell editing.

4
Contributors
5
Replies
9
Views
6 Years
Discussion Span
Last Post by rf.saboia
Featured Replies
  • 1
    Ezzaral 2,714   6 Years Ago

    You need to override the isCellEditable(int,int) method in your table model to return false[code] public boolean isCellEditable(int row, int column) { return false; }[/code] Read More

  • 1
    Ezzaral 2,714   5 Years Ago

    [QUOTE]You should not override the method isCellEditable on DefaultTableModel but override it on the JTable itself[/QUOTE] And what is your reasoning for that? Read More

1

You need to override the isCellEditable(int,int) method in your table model to return false

public boolean isCellEditable(int row, int column) {
        return false;
    }
0

I have seen that before and tried it and it did not work so I abandonded it. I have just realised that I was putting the code in the wrong place. I just plonked it in after my table

table = new JTable(row, column);

public boolean isCellEditable(int row, int column){
     return false;
}

It never occured to me that it needed to be

table = new JTable(row, column){
   public boolean isCellEditable(int row, int column){
        return false;
   }
};

It now works fine - thank you

Edited by Reverend Jim: Fixed formatting

0

DefaultTableModel model = new DefaultTableModel(data,col);
table = new JTable(model){
public boolean isCellEditable(int rowIndex, int colIndex) {
return false; //Disallow the editing of any cell
}
};

I think this is the best way to set the cell un-editable
You should not override the method isCellEditable on DefaultTableModel
but override it on the JTable itself

1

You should not override the method isCellEditable on DefaultTableModel
but override it on the JTable itself

And what is your reasoning for that?

Edited by Ezzaral: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.