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

    Default Panels with BorderLayout Question

    I have created a frame which has 2 panels. The frame and panels has to use BorderLayout. 1 panel with the 3 buttons need to display in South and the other panel with 3 buttons in the Center. I understand about adding buttons in the panel and the panels are the ones that is need to be set in the cordinates but I am having problems. Could someone please show me how I can get my panels to go to their respective spot? I tried -
    JPanel mp = new JPanel(new BorderLayout(BorderLayout.SOUTH));, but getting error. Help would be greatly appreciated. If not maybe someone can show me how to get 1 panel with the multi buttons to 1 quandrant.

    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());
          	pane.add(getSubPanel());
          	frame.setSize(400,300);
          	frame.setVisible(true);
          	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       	}
    
    	private static JPanel getMainPanel()
       	{
    		JPanel mp = new JPanel(new BorderLayout);
    		mp.setLayout(new BorderLayout());
        	mp.add(new JButton("Button 1"));
        	mp.add(new JButton("Button 2"));
    		mp.add(new JButton("Button 3"));
    		return mp;
       	}
    
    	private static JPanel getSubPanel()
    	{
        	JPanel sp = new JPanel(new BorderLayout());
    		sp.setLayout(new BorderLayout());
          	sp.add(new JButton("Button 4"));
          	sp.add(new JButton("Button 5"));
          	sp.add(new JButton("Button 6"));
          	return sp;
    	}
    
    
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    There is a bit wrong with the panel creating code. When you initially create the panel you give it an argument of new BorderLayout, which is incorrect. You need to use a default constructor and then the next line correctly sets the layout. When you add the buttons you should be specifying where to place them.

    Java Code:
    panel.add(new JButton("hi"), BorderLayout.CENTER);
    this adds a button to the center of the panel, it's nearly identical for all other border layout locations. Finally you would add the panels to the frames panel in a similar fashion.

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

    Default

    Ok did that, but now all I see is the "Button 6" on the SOUTH quadrant and missing all the buttons in the Center and 4-5 for South. What am I missing?

    Java Code:
    public static void main(String[] a)
    	{
        	JFrame frame = new JFrame("BorderFlow Frame");
          	Container pane = frame.getContentPane();
          	//pane.setLayout(new BorderLayout);
          	pane.add(getMainPanel());
          	pane.add(getSubPanel());
          	frame.setSize(400,300);
          	frame.setVisible(true);
          	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       	}
    
    	private static JPanel getMainPanel()
       	{
    		JPanel mp = new JPanel();
    		mp.setLayout(new BorderLayout());
        	mp.add(new JButton("Button 1"),BorderLayout.CENTER );
        	mp.add(new JButton("Button 2"),BorderLayout.CENTER);
    		mp.add(new JButton("Button 3"),BorderLayout.CENTER);
    		return mp;
       	}
    
    	private static JPanel getSubPanel()
    	{
        	JPanel sp = new JPanel();
    		sp.setLayout(new BorderLayout());
          	sp.add(new JButton("Button 4"),BorderLayout.SOUTH);
          	sp.add(new JButton("Button 5"),BorderLayout.SOUTH);
          	sp.add(new JButton("Button 6"),BorderLayout.SOUTH);
          	return sp;
    	}

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    You can only add 1 component to "SOUTH", 1 component to the NORTH etc...

    If you want multiple components in the "SOUTH", then create a panel, add components to the panel and then add the panel to the SOUTH.

  5. #5
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    I didn't think it sounded right but, did what was on reply. I have tow panels main & sub with one (sub) sp.setLayout(new BorderLayout());...but am confused by "add the panel to the SOUTH". Could someone please give me an example or steer me in the right path:confused::confused::confused:?

    Java Code:
    public static void main(String[] a)
    	{
        	JFrame frame = new JFrame("BorderFlow Frame");
          	Container pane = frame.getContentPane();
          	//pane.setLayout(new BorderLayout());
          	pane.add(getMainPanel());
          	pane.add(getSubPanel());
          	frame.setSize(400,300);
          	frame.setVisible(true);
          	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       	}
    
    	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;
       	}
    
    	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;
    	}

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    Could someone please give me an example or steer me in the right path
    Read the Swing tutorial: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container). There are plenty of working examples.

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

Similar Threads

  1. Need some help with panels inside panels
    By kakefjes in forum AWT / Swing
    Replies: 0
    Last Post: 03-17-2011, 11:36 AM
  2. BorderLayout problem
    By TGH in forum New To Java
    Replies: 3
    Last Post: 05-27-2010, 09:51 PM
  3. BorderLayout
    By oneself in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 10:59 PM
  4. BorderLayout Demo
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:51 PM
  5. Help with BorderLayout
    By lenny in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 07: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
  •