0

I have been searching around for an easy way to implement a JScrollPlane. I am trying to add it to a JPanel, and it will contain a dynamic number of JPanels (which will be filled with other stuff).

Here is my (failing miserably) attempt to make said JScrollPane:

final JPanel info = new JPanel();
final JScrollPane infoS = new JScrollPane(info,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
info.setLayout(new GridLayout(0,1));
info.setLocation(10,78);
info.setSize(420,490);
infoS.setPreferredSize(new Dimension(600, 600));
gui.add(infoS);
Roman C
  • 49,761
  • 33
  • 66
  • 176
Thrfoot
  • 125
  • 2
  • 7
  • 1
    `gui.add(infoS);`? don't you mean `info.add(infoS)`? oh yeah, and please read [this](http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html). – mre Nov 19 '12 at 20:10
  • @mre: I hope not, because it would mean that info would be both the child and parent of infoS (and vice-versa). – JB Nizet Nov 19 '12 at 20:28
  • Ah, sorry I left that bit of information out. `gui` is the overall JPanel that contains the content of the frame. – Thrfoot Nov 19 '12 at 21:18
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 19 '12 at 21:51

2 Answers2

2

In this example, a series of nested panels are added to a panel having BoxLayout. That panel is used to create a JScrollPane which is then added to a JFrame.

public class BoxTest extends JPanel {
...
JScrollPane jsp = new JScrollPane(this,
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
...
JFrame f = new JFrame();
f.add(jsp); // BorderLayout.CENTER, by default
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I think I remember coming across that at one point. However it isn't what I'm looking for. This makes the entire frame a JScrollPane. I just want a specific JPanel to become a JScrollPane. – Thrfoot Nov 19 '12 at 21:41
  • @Thrfoot The principle is still the same. `JFrame` is just a component after all (well, actually, it defers to the content pane, but...) – MadProgrammer Nov 19 '12 at 22:39
  • @Thrfoot: No, the `JScrollPane` is added to the frame's content pane. – trashgod Nov 20 '12 at 02:34
2

The primary problem you're having is the fact that the default layout manager's layout is set to FlowLayout, which means that the JScrollPane will want to use it's preferred size to be layout with, which may not fill the entire panel.

Instead, use a BorderLayout

final JPanel info = new JPanel(new BorderLayout()); // <-- Change me :D
final JScrollPane infoS = new JScrollPane(info,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// These are bad ideas, setLocation and setSize won't work, as the panel should be
// under the control of a layout manager
//info.setLocation(10,78);
//info.setSize(420,490);
//infoS.setPreferredSize(new Dimension(600, 600));
gui.add(infoS);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • After a lot of tinkering and googling, I have gotten it working. Thanks for providing this excellent example! – Thrfoot Nov 20 '12 at 00:35