简体   繁体   中英

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.

  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.]

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 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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