[英]How to implement expand/collapse nature to JTables in java swing application
In my application i am displaying a list of tables with huge data. 在我的应用程序中,我显示了一个包含大量数据的表列表。 All of the tables may not fit in the window, we need to scroll to see the bottom tables.
所有表都可能不适合窗口,我们需要滚动才能看到底部表格。 Here i need expand/collapse nature to the tables.
在这里,我需要扩展/折叠性质到表。 User may select which tables he would like to see among all of the tables.
用户可以选择他希望在所有表中看到哪些表。 This is my sample screen shot of the application.
这是我的应用程序的示例屏幕截图。
How can i do it. 我该怎么做。 Any suggestions would be greatly appreciated.
任何建议将不胜感激。
You can try JXTreeTable or TreeTable 您可以尝试JXTreeTable或TreeTable
or you can switch jtable's visibility to make a expand/collapse effect. 或者您可以切换jtable的可见性以进行展开/折叠效果。 First when it is visible and use selects collapse use
setVisible(false);
首先当它可见并使用选择折叠时使用
setVisible(false);
and do reverse in expand. 并在扩展中反转。 [Not sure this will work or not.]
[不确定这是否有效。]
org.netbeans.swing.outline.Outline
是一个吸引人的选择,正如这里和这里所讨论的。
The expand/collapse effect based on visibility, as proposed as 2nd option by Harry, can be easy to achieve if you use DesignGridLayout : 如果您使用DesignGridLayout ,可以很容易地实现基于可见性的展开/折叠效果(如Harry提出的第二个选项):
DesignGridLayout layout = new DesignGridLayout(panel);
...
JCheckBox expand1 = newCheckBox("First");
layout.row().left().add(expand1);
final IRow row = layout.row().grid().add(table1);
expand.setSelected(true);
expand.addItemListener(new ItemListener()
{
@Override public void itemStateChanged(ItemIvent event)
{
if (event.getStateChanged() == ItemIvent.SELECTED)
row.show();
else
row.hide();
}
};
Repeat the code above (after " ...
") for every table in your panel
. 对
panel
每个表重复上面的代码(在“ ...
”之后)。
With the code above, each label is replace with a checkbox that, when unselected, hides the table underneath. 使用上面的代码,每个标签都替换为一个复选框,当取消选中时,隐藏下面的表格。
You can take a look at DesignGridLayout example application (available as WebStart on the home page) under the " Dynamic Layouts " section. 您可以在“ 动态布局 ”部分下查看DesignGridLayout示例应用程序(在主页上可用作WebStart)。
Yet another alternative is to use SwingX' JXTaskPane 另一种方法是使用SwingX'JXTaskPane
JXTaskPaneContainer container = new JXTaskPaneContainer();
forEach table:
JXTaskPane pane = new JXTaskPane(tableTitle);
pane.add(table);
container.add(pane);
somePanel.add(new JScrollPane(container);
Edited (forgot the taskPane in the snippet, never post before-coffee ;-) 编辑(忘记了片段中的taskPane,从未发布过咖啡;-)
public DesignGridTest(){
JFrame frame = new JFrame("Example 1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel masterpanel = new JPanel();
DesignGridLayout masterlayout = new DesignGridLayout(masterpanel);
JCheckBox chk1 = new JCheckBox("Check Box1");
masterlayout.row().left().add(chk1);
JPanel panel1 = new JPanel();
JTable table1 = new JTable();
table1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
panel1.add(table1);
final IRow row1 = masterlayout.row().grid().add(panel1);
chk1.setSelected(true);
chk1.addItemListener(new ItemListener()
{
@Override public void itemStateChanged(ItemEvent event)
{
if (event.getStateChange() == ItemEvent.SELECTED)
row1.show();
else
row1.hide();
frame.pack();
}
});
frame.add(masterpanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String a[]){new DesignGridTest();}
Add panels(panel1,panel2..) to 'masterlayout' to get your desired look 将面板(panel1,panel2 ..)添加到'masterlayout'以获得所需的外观
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.