简体   繁体   English

jtable标头隐藏/无列

[英]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. 例如,我想创建一个表,该表具有3列标题,但仅显示两列并隐藏第3列。

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. 我知道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. 因此,通过鼠标左键单击该列标题,将显示一个用于隐藏/显示列的弹出菜单。此列标题实际上没有列或行,但标题类似于JDownloader。

you have look at, JTable methods , 您已经看过JTable方法

these two methods only to hide/show JTables Column(s) , data are still available in the TableModel 这两种方法仅用于隐藏/显示JTables Column(s) ,但TableModel数据仍然可用

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). SwingXJXTable具有内置的UI,用于在弹出窗口中显示/隐藏列(不幸的是,我找不到它的图像)。

You can of course create this yourself using the suggested methods but why re-invent the wheel 当然,您可以使用建议的方法自己创建,但是为什么要重新发明轮子呢?

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: 将最后一列的宽度设置为0:

table.getColumnModel().getColumn(lastIndex).setPrefferedWidth(0);
table.getColumnModel().getColumn(lastIndex).setMaximumWidth(0);
table.getColumnModel().getColumn(lastIndex).setMinimumWidth(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM