• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BorderLayout - JPanel inside JPanel

 
Brent Van Scoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2953
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

This thread is more suited to the Swing forum so I'll move it there for you.
 
Tony Docherty
Bartender
Posts: 2953
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2565
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,

Thank you. I was able to make the changes. Brent
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic