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?
We can remove a selected row from a JTable using the removeRow() method of the DefaultTableModel class.
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.
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);
In addition to returning false
from isCellEditable()
, add these invocations.
table.setFocusable(false);
table.setRowSelectionAllowed(false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With