-1

I have a simple JPanel so defined:

JPanel listObject = new JPanel(null);
listObject.setBounds(0, 350, 300, 100);

I would insert this into a JScrollPanel I tried to define it so:

JScrollPane scroll = new JScrollPane(listObject);
scroll.setBounds(0, 375, 270, 100);
mainPanel.add(scroll);

The problem is that with this code the panel is not show inside the scrollPane. How can i do for that?

Thank you.

user3253955
  • 433
  • 1
  • 8
  • 17
  • possible duplicate of [Java JPanel inside JScrollPane?](http://stackoverflow.com/questions/9393480/java-jpanel-inside-jscrollpane) – RealSkeptic Jan 23 '15 at 21:56
  • Consider improving your question by creating and posting a [minimal code example](http://stackoverflow.com/help/mcve), a small program that runs and shows your problem for us. By doing this, we'll have a vastly improved understanding of your problem and be able to provide much better help. Also images help. – Hovercraft Full Of Eels Jan 23 '15 at 22:06

1 Answers1

1

Note some problems with your code:

JPanel listObject = new JPanel(null);

You're adding this to a JScrollPane and note that JScrollPanes don't handle null layout-using viewport views well at all. It in fact can completely mess up the scrollpane's ability to scroll.

listObject.setBounds(0, 350, 300, 100);

By calling setBounds(...) you're in effect saying try to put this component at the [0, 350] position of the container that holds it and size it to [300, 100]. Note that the container that you're adding it to is the JScrollPane's viewport. So if the viewport follows this request, and since you're trying to make your JScrollPane and its viewport have a width of 270 with this code here:

scroll.setBounds(0, 375, 270, 100);

You're in effect asking the JScrollPane to add a JPanel way off to the right beyond the bounds of the JScrollPane itself. Ignoring that the viewport is likely not even using a null layout, this really makes little sense.

You will want to get rid of the setBounds(...) business to start with. JScrollPanes use their own layouts, and so there is no need or good effect by trying to set the bounds of a component being added to it. Instead consider overriding the JPanel's getPreferredSize. Also, a component held by a JScrollPane should not use a null layout as this will usually prevent the JScrollPane from working correctly, from knowing how to scroll the container.

The bottom line here is that you will want to avoid null layouts in general as they lead to the creation of rigid GUIs that are very difficult to enhance or debug, that may look OK on one platform but will definitely look bad on all other platforms or screen resolutions. It's a newbie trap to think that null layout is the way to go, one that you'll quickly learn to avoid the more experience you gain.

If you're still stuck after reading the answers, please consider creating and posting your minimal example program, a small compilable and runnable program that shows for us your problem.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373