Solved

Simple BoderLayout in Java - position components

Posted on 2013-12-10
3
348 Views
Last Modified: 2013-12-10
Hi,

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. I a wondering if what I am trying to do is not possible because of the properties in java.

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

package javafinalprojecttwo;  
  
// Tutorial 2: Welcome.java  
// This application welcomes the user to Java programming.  
import java.awt.Dimension;  
import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;  
import javax.swing.border.*;  
import java.awt.BorderLayout;  
import javax.swing.event.*;  
import java.text.*;  
public class Welcome extends JFrame  
{  
   // Labels for totals  
   private JLabel subtotalJLabel, taxJLabel, totalDueLabel;  
     
   //Images  
   private JLabel pictureIntelJLabel;  
   private JLabel pictureAmdJLabel;  
     
   // JPanels   
   private JPanel intelMainJPanel,amdMainJPanel,componentsMainJPanel,   
   intelprocessorJPanel, amdprocessorJPanel, upperIntelJPanel;  
       
   // JPanel borders (only 3 of 5 jpanels have borders)  
   private TitledBorder intelMainTitledBorder,amdMainTitledBorder,  
   componentsMainTitledBorder;   
     
   // JButtons  
   private JButton placeOrderJButton, clearJButton, exitJButton;  
     
   //text fields for totals  
   private JTextField subtotalJTextField, taxJTextField, totaldueJTextField;   
     
   //  check boxes for confirm order and email check  
   private JCheckBox confirmOrderJCheckBox, emailJCheckBox;  
   //private JCheckBox addon3JCheckBox; DELETE ALL   
     
   /////////////BUTTON AND BUTTON GROUPS  
   private ButtonGroup intelButtonGroup;  
   private JRadioButton intel1JRadioButton, intel2JRadioButton,  
           intel3JRadioButton, intel4JRadioButton;  
     
   /// constants  
  // private final double HAMBURGER = 6.95;  
  // private final double PIZZA = 5.95;  
  // private final double SALAD = 4.95;  
  // private final double TAXRATE = .0785;  
             
  
   // no-argument constructor  
   public Welcome()  
   {  
      createUserInterface();  
   }  
  
   // create and position GUI components; register event handlers  
   private void createUserInterface()  
   {  
      // get content pane and set layout to null  
      Container contentPane = getContentPane();  
      contentPane.setBackground( Color.LIGHT_GRAY );  
     // contentPane.setLayout(new BorderLayout(10,20));  
      
        
      // intelMainTitledBorder for use with intelMainJPanel  
      intelMainTitledBorder = new TitledBorder( "Intel" );  
      intelMainTitledBorder.setBorder(BorderFactory.createLineBorder(Color.GRAY));  
      intelMainTitledBorder.setTitleColor(Color.GRAY);  
       
      // intelMainTitledBorder for use with amdMainJPanel  
      amdMainTitledBorder = new TitledBorder( "AMD" );  
      amdMainTitledBorder.setBorder(BorderFactory.createLineBorder(Color.GRAY));  
      amdMainTitledBorder.setTitleColor(Color.GRAY);  
        
       // intelMainTitledBorder for use with intelMainJPanel  
      componentsMainTitledBorder = new TitledBorder( "Order total" );  
      componentsMainTitledBorder.setBorder(BorderFactory.createLineBorder(Color.GRAY));  
      componentsMainTitledBorder.setTitleColor(Color.GRAY);  
        
      // set up intelMainJPanel  
      intelMainJPanel = new JPanel();  
      intelMainJPanel.setBackground(Color.LIGHT_GRAY);  
      intelMainJPanel.setBorder( intelMainTitledBorder );  
      intelMainJPanel.setPreferredSize(new Dimension(325, 200));        
      contentPane.add( intelMainJPanel, BorderLayout.WEST );  
      intelMainJPanel.setVisible(true);  
        
       // set up intelMainJPanel  
      upperIntelJPanel = new JPanel();  
      upperIntelJPanel.setBackground(Color.GRAY);  
      upperIntelJPanel.setPreferredSize(new Dimension(320, 175));        
      intelMainJPanel.add( upperIntelJPanel, BorderLayout.NORTH );  
      upperIntelJPanel.setVisible(true);  
        
        // set up amdMainJPanel  
      amdMainJPanel = new JPanel();  
      amdMainJPanel.setBackground(Color.LIGHT_GRAY);  
      amdMainJPanel.setBorder( amdMainTitledBorder );  
      amdMainJPanel.setPreferredSize(new Dimension(325, 200));  
      contentPane.add( amdMainJPanel, BorderLayout.EAST );     
      amdMainJPanel.setVisible(true);  
        
       // set up componentsMainJPanel  
      componentsMainJPanel = new JPanel();  
      componentsMainJPanel.setBackground(Color.LIGHT_GRAY);  
      componentsMainJPanel.setBorder( componentsMainTitledBorder );  
      componentsMainJPanel.setPreferredSize(new Dimension(100, 300));  
      contentPane.add( componentsMainJPanel, BorderLayout.SOUTH );  
        
           
      pictureIntelJLabel = new JLabel();  
      pictureIntelJLabel.setBounds (100,100,100,110);  
      pictureIntelJLabel.setIcon(new ImageIcon ("my.jpg"));  
      pictureIntelJLabel.setHorizontalAlignment(JLabel.CENTER);  
      upperIntelJPanel.add(pictureIntelJLabel);  
        
      pictureAmdJLabel = new JLabel();  
      //pictureAmdJLabel.setBounds (1,1,100,110);  
      pictureAmdJLabel.setIcon(new ImageIcon ("myy.png"));  
      pictureAmdJLabel.setHorizontalAlignment(JLabel.CENTER);  
      amdMainJPanel.add(pictureAmdJLabel, BorderLayout.SOUTH);  

Open in new window

Welcome.java
my.jpg
myy.png
0
Comment
Question by:bvanscoy678
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39710391
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?
Basically, you have two options... You use a LayoutManager (of whatever implementation is appropriate, ie. BorderLayout, FlowLayout, GridLayout, GridBagLayout, etc) if you want Swing to position your components for you. If YOU want to move them manually (apart from whatever slight flexibility the LayoutManager provides), then you DON'T use a LayoutManager, by calling .setLayout(null) and then you can call .setLocation() method to position components as you require.

Note also, that the Layout that is set is only on a particular container, and if the child components of that container are also containers themselves (ie. if you have JPanels inside JPanels) then the child containers can have different (or null) LayoutManagers.


Apart from this, it isn't 100% clear exactly what you are trying to do, so at this point I can't really guide you as to which actual path to take. Other than to say, that generally you should try and use the LayoutManagers where you can (as opposed to manually positioning) as generally you will get a GUI that better responds to changes in the environment, ie. window resizes, font type/size changes, etc, etc. Maybe the below tutorial can help you better understand the different possibilities (and their restrictions)

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Also, if you can better explain (maybe some drawings, screenshots might be the best way) what you have and what you are aiming for, we might be able to help further!
0
 

Author Closing Comment

by:bvanscoy678
ID: 39710527
The information you provided clued me into my problem. I thought the default manager for the JPanel was BorderLayout. Once I figured out it was FlowLayout, I was able to place my components.


Thanks for the help!
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39710533
Not a problem, glad I could help! :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now