I am using Java Swingx framework. I have 4 columns in my DefaultTableModel
object. I wish to display only 3 of the columns. But, I need all four for computing.<
try this to remove a single column:
myTableModel = new DefaultTableModel();
myTableModel.setColumnIdentifiers(new Object[]{"S.No.", "ID", "GDC ID", "Decsription"});
JTable myTable = new JTable(myTableModel);
// remember to save the references
TableColumn myTableColumn0 = guiLoteryNumbersTable.getColumnModel().getColumn(0);
TableColumn myTableColumn1 = guiLoteryNumbersTable.getColumnModel().getColumn(1);
TableColumn myTableColumn2 = guiLoteryNumbersTable.getColumnModel().getColumn(2);
TableColumn myTableColumn3 = guiLoteryNumbersTable.getColumnModel().getColumn(3);
myTable.getColumnModel().removeColumn(myTableColumn1);
Then to show the column again keeping the order:
// 1- remove all the existent columns
myTable.getColumnModel().removeColumn(myTableColumn0);
myTable.getColumnModel().removeColumn(myTableColumn2);
myTable.getColumnModel().removeColumn(myTableColumn3);
// 2- add all the columns again in the right order
myTable.getColumnModel().addColumn(myTableColumn0);
myTable.getColumnModel().addColumn(myTableColumn1);
myTable.getColumnModel().addColumn(myTableColumn2);
myTable.getColumnModel().addColumn(myTableColumn3);
Sorry, but that's the best way I know.
by default minimum size is 10 pixels
widht,
you can to remove / add column from JTable view, column presents in the XxxTableModel, you can to hide and show any of column(s)
You can hide it by setting its width to 0.
_table.getColumn("ID").setPreferredWidth(0);
_table.getColumn("ID").setMinWidth(0);
_table.getColumn("ID").setWidth(0);
_table.getColumn("ID").setMaxWidth(0);
You manipulate the getValueAt , getColumnCount to achieve this.
So for example the getColumnCount you give it as 3
and on getValueAt - to skip if the column index is above the skipped column
JTable will first call getColumnCount and getRowCount and fetch calling getValueAt for each of the cells.
NOTE: Ignore this and see link by trashgod below.
No need to adjust your model, or to try to make that column very small. JTable
has built-in functionality for this: the removeColumn method. As stated in the javadoc of that method
Removes aColumn from this JTable's array of columns. Note: this method does not remove the column of data from the model; it just removes the TableColumn that was responsible for displaying it.
Also note the existence of the following methods:
Since the column order and column count in the view (the JTable
) might be different from the one in the model you need those methods to switch between view and model