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.

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;
    }

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

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

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

And what is your reasoning for that?

I can get this in the swing´s netbeans??? thank, helped

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.