aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes BorderLayout - JPanel inside JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "BorderLayout - JPanel inside JPanel" Watch "BorderLayout - JPanel inside JPanel" New topic
Author

BorderLayout - JPanel inside JPanel

Brent Van Scoy
Greenhorn

Joined: Dec 10, 2013
Posts: 19
Hi,

I'm new here and I am in a beginner's java programming class. Our project is to create a simple GUI and then set up classes and simple functionality. In Java 1, I created a simple Java GUI and I am adding to this project for the final in Java 2 project.

I am working with BoderLayout manager with a plan of adding a few Main JPanels, then inside those JPanels, add my components (images, radiobuttons, checkboxes). I would use the borderlayout inside the JPanel to configure the positions. I've added a few border and color changes in my GUI just help me see where everything is at.

I added two images; upperIntelJPanel and amdMainJPanel, but I can't make the images move. I've tried using the LayoutManager and also setBounds, but eitherway they just show in the center and top of my JPanels. I thought I was able to add a JPanel inside another JPanel and use the layout manager to position the component, but maybe I am wrong in my assumption?

I have read the http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html again and again, but can't seem grasp how to move my components using the BorderLayout manager. I was able to move them with my "main JPanels", but not my nested ones.

Thanks for any push in the right direction. I've attached my code to show what I am trying to do.

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2284
    
  49
Welcome to the Ranch.

This thread is more suited to the Swing forum so I'll move it there for you.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2284
    
  49
To confirm your thoughts on nesting JPanels, yes you can nest them as deep as you want and each JPanel can have a different type of layout manager. But if you want your nested JPanels to layout components using a different layout manager than FlowLayout (which is the default) you need to specify the LayoutManager you want the JPanel to use. You can pass an instance of the appropriate layout manager to the constructor or the JPanel's setLayout() method.

Brent Van Scoy
Greenhorn

Joined: Dec 10, 2013
Posts: 19
Tony,

I think that is my issue. I thought the default was BorderLayout, but if it is FlowLayout, that makes sense why it is not working. I will try specifing the layoutmanager for the Jpanel.

I added the below code and I have control of the components again.

upperIntelJPanel.setLayout(new BorderLayout () );



Thanks for the help!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
I thought the default was BorderLayout,


The default layout for the content pane of a JFrame (JDialog, JWindow) is a BorderLayout, but for a JPanel it is a FlowLayout.
Brent Van Scoy
Greenhorn

Joined: Dec 10, 2013
Posts: 19
Rob,

Thank you. I was able to make the changes. Brent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BorderLayout - JPanel inside JPanel