blue igloo blue igloo - 1 year ago 106
Java Question

JScrollPane layout

I want to add table2 into the scrollpanel (called feedback) which already has table1 in there. But only one table shows up. If I use feedback.add(table2), only the 1st table shows (I guess the 2nd table is behind the first one, but I don't know how to make the second one below the first one). if I use feedback.getViewport().add(table2, null), only the 2nd table shows. Do I need to use some layout manager here? i tried to search online about scrollpanel layout but didn't get any solutions. Could anyone tell me what's the problem or give me some related example links? Thanks a lot. The relative code are:

content = getContentPane();
content.setLayout(new FlowLayout());
scrollPane = new JScrollPane(tree);
feedback = new JScrollPane(table1);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,scrollPane, feedback);
//i add this, but still doesn't work

Answer Source

Only a single component can be added to the "viewport" of a JScrollPane. This is done by using:

JScrollPane scrollPane = new JScrollPane( table );


scrollPane.setViewportView( table );

If you want multiple component to appear in a scrollPane then add the component to a panel first and add the panel to the viewport.

Read the JScrollPane API for more information and follow the link to the Swing tutorial as well for examples.