I'm using JXTable which has a setColumnControlVisible(true) which shows a button on the upper right corner above scroll, we can show and hide column by pressing it. I want to remember the changes when the app get close, but cannot figure it out yet, Here what I have tried so far. I check the src of JxTable, but didn't how to get the column index or column number which is hidden.
package paractice;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.table.AbstractTableModel;
import org.jdesktop.swingx.JXTable;
public class TableTest extends JFrame{
private static final long serialVersionUID = 1L;
private JXTable table;
public TableTest() {
setLayout(new BorderLayout());
table = new JXTable(new model());
//add(table.getTableHeader(), BorderLayout.NORTH);
add(new JScrollPane(table), BorderLayout.CENTER);
table.setColumnControlVisible(true);
setSize(700, 700);
}
public class model extends AbstractTableModel{
String[] columns = {"column1", "column2", "column3", "column4", "column5"};
@Override
public String getColumnName(int column) {
return columns[column];
}
@Override
public boolean isCellEditable(int arg0, int arg1) {
return super.isCellEditable(arg0, arg1);
}
@Override
public void setValueAt(Object arg0, int row, int col) {
super.setValueAt(arg0, row, col);
fireTableCellUpdated(row, col);
}
public int getColumnCount() {
return columns.length;
}
public int getRowCount() {
return 0;
}
public Object getValueAt(int arg0, int arg1) {
return null;
}
}
public static void main(String args[]) {
TableTest test = new TableTest();
test.setVisible(true);
}
}
but didn't how to get the column index or column number which is hidden.
Compare the TableModel with the JTableHeader.
Just create a simple loop to check all the columns name of the TableModel to see if the table contains that column. Something like:
for (int i = 0; i < model.getColumnCount(); i++)
{
Object name = model.getColumnName();
TableColumn column = table.getColumn( name );
if (column == null)
// column is hidden do your processing
}
Then the next time you display the table you can get the names of all hidden columns and then use:
table.removeColumn( table.getColumn( name ) );