Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTable Hide Columns

843804Jun 17 2005 — edited Aug 18 2005
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?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 15 2005
Added on Jun 17 2005
5 comments
723 views