JTable隐藏和显示列 [英] JTable hide and show columns

查看:113
本文介绍了JTable隐藏和显示列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表中添加一些列(Swing JTable)。其中一些将具有默认大小(例如250),其他将被隐藏(因此它们的大小将为0)。我使用这段代码:

I want to add some columns to a table (Swing JTable). Some of them will have a default size (e.g. 250), others will be hidden (so their size will be 0). I use this code:

 model = new DefaultTableModel();
 table = new JTable(model);
 setAutoResizeMode(AUTO_RESIZE_OFF);
 for (int i = 1; i < COLUMN_NAMES.length; i++) {
    model.addColumn(COLUMN_NAMES[i]);
    if (show[i]) show(index);
    else hide(index);
 }
 ........

 private void hide(int index) {
    TableColumn column = getColumnModel().getColumn(index);
    column.setMinWidth(0);
    column.setMaxWidth(0);
    column.setWidth(0);
    column.setPreferredWidth(0);
    doLayout();
}

private void show(int index) {
    final int width = 250;
    column.setMinWidth(15);
    column.setMaxWidth(width);
    column.setWidth(width);
    column.setPreferredWidth(width);
    doLayout();
}

问题是当显示表格时,显示所有列(无是隐藏的),它们的大小不是250,但它们的大小都相同。

the problem is when the table is displayed, all the columns are showed (none is hidden) and their size is not 250 but they have all the same size.

我怎样才能得到想要的效果?

How can I get the wanted effect?

推荐答案

JTable #removeColumn 仅从JTable视图中删除列,更多内容在此示例

JTable#removeColumn remove Column only from JTable view, more in this example

这篇关于JTable隐藏和显示列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆