I'm having some troulbe with my JTables. I want to hide some columns, but setting the preferred column width or even the maximum column width isn't doing it.
Not working to hide columns:
MyTable.getColumnModel().getColumn(0).setPreferredWidth(0);
or
MyTable.getColumnModel().getColumn(0).setMaxWidth(0);
I tried removing the columns, but I ran into some problems there, too.
Hides columns, but causes other problems:
MyTable.getColumnModel().removeColumn(MyTable.getColumnModel().getColumn(0));
or
MyTable.removeColumn(MyTable.getColumnModel().getColumn(0));
First, my table is being dynamically updated. About every two or three minutes new data is coming into the table.
MyTableModel.addRow(new Object[] { "Some Value" });
Second, I'm using the data in the column I want to hide to render a highlighting feature.
public class MyTable extends JTable
{
public Component prepareRenderer(TableCellRenderer r, int row, int col)
{
Component c = super.prepareRenderer(r, row, col);
if (((String) getValueAt(row, 0)).equals("Some Value"))
{
c.setBackground(Color.black);
c.setForeground(Color.white);
}
}
If I remove that column, this highlighting function doesn't work.
So, how do I hide/remove the column while still being able to use the highlighting function and dynamically inserting data into the table so that when the column is unhidden/added it will have all the new data in it?