Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set all JTable cells unselectable

Tags:

java

swing

jtable

I'm trying to create a JTable that simply displays data and does not allow any edits or selections. I set all cells to be uneditable by running:

TableModel model = new DefaultTableModel(data, titles) {
    public boolean isCellEditable(int rowIndex, int mColIndex) {
        return false;
    }
};

But I'm now trying to make all of the cells unselectable as well. I found the setRowSelectionAllowed method which allowed me to disable the whole row being selected when a cell is selected, but that didn't stop the cell from being selectable. I looked through the methods of DefaultTableModel but I didn't seen any isCellSelectable method. Any suggestions?

like image 894
BitFiber Avatar asked Nov 28 '11 03:11

BitFiber


People also ask

How do you clear a JTable row?

We can remove a selected row from a JTable using the removeRow() method of the DefaultTableModel class.

How do you make a JTable column not editable?

Right-click on the table cells. From popup menu, choose "Table Contents..". Uncheck the editable check box for the column you want to make it non-editable.

What is JTable TableModel?

The TableModel interface specifies the methods the JTable will use to interrogate a tabular data model. The JTable can be set up to display any data model which implements the TableModel interface with a couple of lines of code: TableModel myData = new MyTableModel(); JTable table = new JTable(myData);


1 Answers

In addition to returning false from isCellEditable(), add these invocations.

table.setFocusable(false);
table.setRowSelectionAllowed(false);
like image 50
trashgod Avatar answered Sep 27 '22 18:09

trashgod