I have designed one GUI in which I have used one JTable from which I have to make 2 columns invisible . How should I do that ?
Developers can hide a row or column by calling the HideRow and HideColumn methods of the Cells collection respectively. Both methods take the row/column index as a parameter to hide the specific row or column. Note: It is also possible to hide a row or column if we set the row height or column width to 0 respectively.
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.
If you want to allow users to edit the data, then you need to set a TableCellEditor on the cells that you want people to edit. You probably also want to start using a TableModel instead of hard coding the data in the JTable itself.
By default, we can edit the text and modify it inside a JTable cell. We can also disable the cell editing inside a table by calling the editCellAt() method of JTable class and it must return false.
Remove the TableColumn
from the TableColumnModel
.
TableColumnModel tcm = table.getColumnModel(); tcm.removeColumn( tcm.getColumn(...) );
If you need access to the data then you use table.getModel().getValueAt(...)
.
For a more complex solution that allows the user to hide/show columns as they wish check out the Table Column Manager.
First remove the column from the view
table.removeColumn(table.getColumnModel().getColumn(4));
Then retrieve the data from the model.
table.getModel().getValueAt(table.getSelectedRow(),4);
One thing to note is that when retrieving the data, it must be retrieve from model not from the table.
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