如何在 Java swing 应用程序中实现 JTable 的展开/折叠特性

发布于 2024-11-06 10:13:43 字数 199 浏览 0 评论 0原文

在我的应用程序中,我显示了包含大量数据的表列表。窗口可能无法容纳所有表格,我们需要滚动才能看到底部的表格。在这里,我需要表格的展开/折叠性质。用户可以在所有表​​格中选择他想要查看的表格。这是我的应用程序的示例屏幕截图。

在此处输入图像描述

我该怎么做?

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.

enter image description here

How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

轻许诺言 2024-11-13 10:13:43
  1. 您可以尝试JXTreeTableTreeTable

  2. 或者您可以切换jtable的可见性以产生展开/折叠效果。首先,当它可见并且使用选择折叠时,使用 setVisible(false); 并在展开时执行相反的操作。 [不确定这是否有效。]

  1. You can try JXTreeTable or TreeTable

  2. or you can switch jtable's visibility to make a expand/collapse effect. First when it is visible and use selects collapse use setVisible(false); and do reverse in expand. [Not sure this will work or not.]

不再见 2024-11-13 10:13:43
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();}

将面板(panel1、panel2..)添加到“masterlayout”以获得您想要的外观

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

茶花眉 2024-11-13 10:13:43

org .netbeans.swing.outline.Outline 是一个有吸引力的替代方案,正如所讨论的此处此处

org.netbeans.swing.outline.Outline is an appealing alternative, as discussed here and here.

做个ˇ局外人 2024-11-13 10:13:43

基于可见性的展开/折叠效果(如 Harry 提出的第二个选项)如果使用 DesignGridLayout

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

面板中的每个表格重复上面的代码(在“...”之后)。

在上面的代码中,每个标签都替换为一个复选框,当取消选中该复选框时,会隐藏下面的表格。

您可以查看“动态布局”部分下的 DesignGridLayout 示例应用程序(在主页上以 WebStart 形式提供)。

The expand/collapse effect based on visibility, as proposed as 2nd option by Harry, can be easy to achieve if you use DesignGridLayout:

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.

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.

空气里的味道 2024-11-13 10:13:43

另一种选择是使用 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(忘记了代码片段中的 taskPane,永远不要在咖啡之前发布;-)

Yet another alternative is to use 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 ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文