1

I want to create a table, for example, that has 3 column headers but only shows two columns and hiding the 3rd.

The last column is fixed and used to hide/show columns like most of applications do by clicking it and showing a popup menu.

Cheers

probably I didn't describe clearly. I know removeColumn/addColumn. By clicking on the table header, i can add column or remove them. However what I am curious is that a dedicated column header at the most right corner of the table header, which is fixed, small width and with descriptive table-like icon. So, by left mouse clicking this column header, a popup menu shows up for hiding/showing columns.This column header doesn't actually have column or rows but the header, like JDownloader does.

user1429552
  • 21
  • 1
  • 3

3 Answers3

4

you have look at, JTable methods,

these two methods only to hide/show JTables Column(s), data are still available in the TableModel

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • probably I didn't describe clearly. I know removeColumn/addColumn. By clicking on the table header, i can add column or remove them. However what I am curious is that a dedicated column header at the most right corner of the table header, which is fixed, small width and with descriptive table-like icon. So, by left mouse clicking this column header, a popup menu shows up for hiding/showing columns. This column header doesn't actually have column or rows but the header, like JDownloader does. – user1429552 Jun 04 '12 at 22:59
  • then answer by @Robin (re-invent the wheel +1) is right way to go, – mKorbel Jun 05 '12 at 07:12
3

The JXTable of SwingX has built-in UI for showing/hiding columns in a pop-up (unfortunately I could not find an image of it).

You can of course create this yourself using the suggested methods but why re-invent the wheel

Robin
  • 36,233
  • 5
  • 47
  • 99
2

There are two ways that you can accomplish this:

Correct Approach :

Remove the column from table

TableColumn lastColumn = table.getColumnModel().getColumn(lastIndex);
table.removeColumn(lastColumn);

Don't do this :

Set the width of last column as 0:

table.getColumnModel().getColumn(lastIndex).setPrefferedWidth(0);
table.getColumnModel().getColumn(lastIndex).setMaximumWidth(0);
table.getColumnModel().getColumn(lastIndex).setMinimumWidth(0);
mprabhat
  • 20,107
  • 7
  • 46
  • 63