1

I currently have multiple jpanels on a jframe. I can add all of the jpanels fine to the jframe but when I try and add a jscrollpane, nothing shows up. Essentially I just want a jscrollbar/pane on the jframe so that I can scroll down as the size of the jpanels goes off screen. This is the main code that I used:

   JPanel Jpanel = new JPanel();
   JScrollPane Jpane = new JScrollPane();

   frame.getContentPane().add(Jpanel);
   frame.getContentPane().add(Jpane);

Any help would be appreciated. Thanks

kleopatra
  • 51,061
  • 28
  • 99
  • 211
user1060187
  • 957
  • 5
  • 12
  • 28

4 Answers4

1

You need to add the JPanel to the scroll pane, then add the scrollpane to the frame. One of the way of doing it is when creating the JScrollPane, with the constructor :

JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(panel);
Raveline
  • 2,660
  • 1
  • 24
  • 28
  • Did you add the scrollPane to the frame ? Could you show more code ? – Raveline Mar 12 '12 at 13:35
  • Yeah essentially I just want to put the scrollPane to the frame, not the Jpanel. When I do this I get an error saying that "java.lang.IllegalArgumentException: adding a window to a container" – user1060187 Mar 12 '12 at 13:36
  • Show us the whole class if you can. – Raveline Mar 12 '12 at 13:39
  • i can't really as it's an assignment. I'm just having issues with that one particular part :( – user1060187 Mar 12 '12 at 13:39
  • If "frame" is a JFrame, you don't need the getContentPane(), you can (and should) do a "add()" directly. – Raveline Mar 12 '12 at 13:39
  • Changed it to add. Still the same issue. Am I getting it wrong that I can add jpanels to a jframe and then add the scrollpane to the jframe? is there a different and better way to do this? – user1060187 Mar 12 '12 at 13:44
  • Yes, you must not add the JPanels to the JFrame if you put them in a JScrollPane. You add things to the scrollPane, and then you add the scrollPane to the JFrame. Without more code, I can't really help you, sorry ! – Raveline Mar 12 '12 at 13:46
  • Thanks for the help. Do you have msn or skype perhaps where we could IM and I could send you more code? – user1060187 Mar 12 '12 at 13:50
  • Nope, I don't do that for assignment, sorry. mKorbel has posted a more comprehensive answer, I'm sure you can make something that work with his answer. – Raveline Mar 12 '12 at 13:52
  • the last sentence is wrong: you never use any of the _add_ methods of the scrollPane – kleopatra Mar 13 '12 at 09:58
1

1) code that you posted caused that (JFrame has implemented by default BorderLayout, and there only one JComponent can to fill concrete area or layst added JComponent), only frame.getContentPane().add(Jpane); is possible to dispay on the screen

2) you have to accept that you can put to the JScrollPane only one JComponent

3) JScrollPane works correctly if is there used proper LayoutManager (not AbsoluteLayout) and in the case that Dimmension of JComponent is wider that JViewport from JScrollPane

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • random bullets (LayoutManagers are important but not an issue here) and hardly understandable in its wording (even me can barely glint at what exactly you mean)... – kleopatra Mar 13 '12 at 10:03
1

This has already been answered with the correct way to do it. The only thing I can think of that you might have done wrong (since it's not working), is that you're adding the panels to the jframe.

When you put a panel inside a scrollpane, you need to add the scrollpane to the JFrame, and not add the panel to the JFrame:

JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(panel);
add(scrollPane); // or if you have a panel inside a panel, add the pane to that panel. ie west.add(scrollPane);
John Snow
  • 5,214
  • 4
  • 37
  • 44
0

What you could do is this:

   JPanel Jpanel = new JPanel(); 
   JScrollPane Jpane = new JScrollPane(Jpanel); 

   frame.setLayout(new BorderLayout());
   frame.add(Jpane, BorderLayout.CENTER);

The reason your panels aren't showing is beacause your frame doesn't know where to put them. It needs a layoutmanager.

Here you can find some basic layoutmanagers: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

Byron Voorbach
  • 4,365
  • 5
  • 27
  • 35