user1429552
user1429552

Reputation: 21

jtable header hiding/without column

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.

Upvotes: 1

Views: 2663

Answers (3)

Robin
Robin

Reputation: 36601

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

Upvotes: 3

mprabhat
mprabhat

Reputation: 20323

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);

Upvotes: 2

mKorbel
mKorbel

Reputation: 109813

you have look at, JTable methods,

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

Upvotes: 4

Related Questions