Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a columns in JTable Invisible for Swing Java

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 ?

like image 416
om. Avatar asked Sep 29 '09 12:09

om.


People also ask

How do you hide a column in Java?

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.

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.

How can you change the appearance of data in cells in JTable?

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.

How can we disable the cell editing inside a JTable in Java?

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.


2 Answers

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.

like image 184
camickr Avatar answered Sep 27 '22 23:09

camickr


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.

like image 41
zawhtut Avatar answered Sep 27 '22 23:09

zawhtut