Results 1 to 3 of 3
  1. #1
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default BorderLayout for 2 Panels

    I see "Button 3" in the Center and "Button 6" in the South but, missing the rest of the buttons. How do I fix it so that I see all 3 buttons in each panel for each quadrant? Help would be greatly appreciated

    Java Code:
     
    import java.awt.*;
    import javax.swing.*;
    
    
    public class ShowBorderLayout {
    
    
    	public static void main(String[] a)
    	{
        	JFrame frame = new JFrame("BorderFlow Frame");
    
          	Container pane = frame.getContentPane();
          	//pane.setLayout(new BorderLayout());
          	pane.add(getMainPanel(),BorderLayout.CENTER);
          	pane.add(getSubPanel(),BorderLayout.SOUTH );
          	frame.setSize(400,300);
          	frame.setVisible(true);
          	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       	}
    
    	
                 ///Center panel
                private static JPanel getMainPanel()
       	{
    	JPanel mp = new JPanel();
    	mp.setLayout(new BorderLayout());
        	mp.add(new JButton("Button 1"));
        	mp.add(new JButton("Button 2"));
    	mp.add(new JButton("Button 3"));
    	return mp;
       	}
    
    	//South panel
                 private static JPanel getSubPanel()
    	{
        	JPanel sp = new JPanel();
    	sp.setLayout(new BorderLayout());
           	sp.add(new JButton("Button 4"));
          	sp.add(new JButton("Button 5"));
          	sp.add(new JButton("Button 6"));
    
          	return sp;
    	}
    
    
    }
    Last edited by collwill; 03-31-2011 at 04:49 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Read the layout API and you'll see what happens when you add a component to a BorderLayout-using container without specifying a constant. In other words what's the default location for adding a component to a BorderLayout-using container. The API states this:

    As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER:
    Java Code:
        Panel p2 = new Panel();
        p2.setLayout(new BorderLayout());
        p2.add(new TextArea());  // Same as p.add(new TextArea(), BorderLayout.CENTER);

    So you are adding all three buttons to the BorderLayout.CENTER position of your container, and only the last button will show. A solution: don't use BorderLayout for these containers. Use perhaps a GridLayout.

Similar Threads

  1. Panels with BorderLayout Question
    By collwill in forum New To Java
    Replies: 6
    Last Post: 03-31-2011, 05:36 AM
  2. Need some help with panels inside panels
    By kakefjes in forum AWT / Swing
    Replies: 0
    Last Post: 03-17-2011, 12:36 PM
  3. BorderLayout problem
    By TGH in forum New To Java
    Replies: 3
    Last Post: 05-27-2010, 10:51 PM
  4. BorderLayout
    By oneself in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 11:59 PM
  5. Help with BorderLayout
    By lenny in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 08:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •