How to implement expand / collapse JTables character in java swing application

In my application, I am showing a list of tables with huge data. All tables may not fit into the window, we need to scroll to see the lower tables. Here I need to expand / collapse the character to the tables. The user can select the tables that he would like to see among all the tables. This is my sample application screenshot. enter image description here

How can i do this. Any suggestions would be greatly appreciated.

+6
source share
5 answers
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 the desired look

+1
source
  • You can try JXTreeTable or TreeTable

  • or you can switch the jtable visibility to make the expand / smooth effect. First, when it is visible and uses selects collapse, use setVisible(false); and do the opposite in disclosure. [Not sure if this will work or not.]

+3
source

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

+2
source

The expand / collapse effect, based on visibility, proposed by Harry as a second option, can be easily achieved 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 above code (after " ... ") for each table in the panel .

Using the code above, each label is replaced with a checkbox, which, if not selected, hides the table below.

You can see an example of the DesignGridLayout application (available as WebStart on the home page) in the Dynamic Layouts section.

+2
source

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 TaskPane in fragment, never publish pre-coffee;)

+2
source

Source: https://habr.com/ru/post/887687/


All Articles