Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jpanel inside another JPanel  RSS feed

 
Steve Avi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Sorry if this is a trivial problem, i'm only just learning GUI's in java.
I have a JPanel with a setPreferredSize at 800,600, but when i put another JPanel inside that one, it is positioned at the top and in the centre of the first one. No matter what i do i cant seem to move the inner JPanel around (to the left for example).
I've tried multiple layout options but i guess im using them wrong, can anyone please help?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most layouts work well only if there are several components, which are then laid out in relation to one another. So the trick to using, say, a BoxLayout or a GridLayout might be to use other (empty) JPanels on the left, right, top and bottom of your panel.

If the JPanel is the only component in the other JPanel, do you actually need it? And if you need it, where/how do you want to position it in relation to its parent?
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Avi:
I have a JPanel with a setPreferredSize at 800,600, but when i put another JPanel inside that one, it is positioned at the top and in the centre of the first one. No matter what i do i cant seem to move the inner JPanel around (to the left for example).


Well if you want it at the left, you can try outerPanel.setLayout(new FlowLayout(FlowLayout.LEFT)). That will lay it out with its preferred height.

Depending on how you set up your layout managers, you can put it pretty much anywhere.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!