Results 1 to 9 of 9
  1. #1
    holden321 is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Question need help with simple layout

    need help with simple layout-layout.jpg
    JFrame has FlowLayout
    JPanel (red colour) has BoxLayout (PAGE_AXIS)

    I want to see JPanel on top (not in the middle)
    is it possible?

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

    Default Re: need help with simple layout

    Don't use FlowLayout then. I'd suggest that the JFrame's contentPane use its default BorderLayout and that you place the red JPanel in the BorderLayout.NORTH (also known as BorderLayout.PAGE_START) position and the JLabel in the BorderLayout.CENTER position. If you don't want the buttons to expand the width of the GUI, then nest the JPanel in another JPanel that uses FlowLayout (the default for JPanel) and place this wrapper JPanel in the JFrame's BorderLayout.NORTH slot.

  3. #3
    holden321 is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Default Re: need help with simple layout

    Thank you, it almost works.
    JLabel fill the entire height :(
    I'm planning to set PreferredSize for JLabel
    need help with simple layout-layout2.jpg
    with two buttons it works fine:
    need help with simple layout-layout3.jpg
    Last edited by holden321; 01-12-2013 at 05:43 PM.

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

    Default Re: need help with simple layout

    Please clarify -- what are you seeing currently, and what are you trying to see. Also consider creating and posting an SSCCE, a small compilable and runnable code that contains only enough code to compile, run, and show your problem. In other words, it will contain the JButtons, JLabels and JPanels but the buttons should have no listeners attached to them since this would have nothing to do with the problem at hand.

  5. #5
    holden321 is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Default Re: need help with simple layout

    I want JPanel and JLabel align top.
    JLabel's width and height will change pragmatically afterward.
    this is now:
    need help with simple layout-layout4.jpg need help with simple layout-layout5.jpg
    this what I want:
    need help with simple layout-layout6.jpg need help with simple layout-layout7.jpg

  6. #6
    holden321 is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Default Re: need help with simple layout

    this is
    Java Code:
    	frame.setLayout(new FlowLayout());
    	JPanel panel=new JPanel();
    	panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
    	panel.setBackground(Color.red);
    	panel.setBorder(BorderFactory.createLineBorder(Color.red));
    	frame.add(panel,BorderLayout.LINE_START);
    	
    	
    	JButton button1 = new JButton("Button1");panel.add(button1);
    	button1 = new JButton("Button1");panel.add(button1);
    	button1 = new JButton("Button2");panel.add(button1);
    	button1 = new JButton("Button3");panel.add(button1);
    	button1 = new JButton("Button4");panel.add(button1);
    	button1 = new JButton("Button5");panel.add(button1);
    	
    	JLabel textLabel = new JLabel("Label",SwingConstants.CENTER); 
    	textLabel.setPreferredSize(new Dimension(300, 100));//300,300
    	textLabel.setBorder(BorderFactory.createLineBorder(Color.black));
    	frame.add(textLabel, BorderLayout.CENTER);

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

    Default Re: need help with simple layout

    One way:

    • Consider using BorderLayout for the entire GUI, which by the way is the default layout for top level windows such as JFrames, JDialogs, and JApplets.
    • Create a JPanel, say called westPanel and give it a BorderLayout.
    • Add your red-bordered JButton holding JPanel to this westPanel in the BorderLayout.NORTH position.
    • Add the westPanel JPanel to the GUI in the BorderLayout.WEST position.
    • Create a JPanel, say called centerPanel and also give it a BorderLayout.
    • Add your JLabel to the centerPanel in the BorderLayout.NORTH position.
    • Add the centerPanel to the GUI in the BorderLayout.CENTER position.
    • This way everything will try to stick to the north or top of your GUI.

  8. #8
    holden321 is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Default Re: need help with simple layout

    I'm sorry, but this is what I get:
    need help with simple layout-layout8.jpg
    Java Code:
    	frame.setLayout(new BorderLayout());
    	
    	JPanel westPanel=new JPanel(new BorderLayout());
    	westPanel.setBorder(BorderFactory.createLineBorder(Color.red));
    	
    	JButton button1 = new JButton("Button1");westPanel.add(button1);
    	button1 = new JButton("Button1");westPanel.add(button1);
    	button1 = new JButton("Button2");westPanel.add(button1);
    	button1 = new JButton("Button3");westPanel.add(button1);
    	button1 = new JButton("Button4");westPanel.add(button1);
    	button1 = new JButton("Button5");westPanel.add(button1);
    	
    	frame.add(westPanel,BorderLayout.WEST);
    	
    	JPanel centerPanel=new JPanel(new BorderLayout());
    	JLabel textLabel = new JLabel("Label",SwingConstants.CENTER); 
    	textLabel.setPreferredSize(new Dimension(300, 300));
    	textLabel.setBorder(BorderFactory.createLineBorder(Color.black));
    	centerPanel.add(textLabel,BorderLayout.NORTH);
    	frame.add(centerPanel,BorderLayout.CENTER);

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

Similar Threads

  1. Grid Layout change layout alignment of control s
    By rellicott in forum SWT / JFace
    Replies: 1
    Last Post: 02-13-2012, 05:11 PM
  2. Layout manager for a very simple problem!
    By Anoyz in forum AWT / Swing
    Replies: 2
    Last Post: 11-10-2011, 06:13 PM
  3. Replies: 0
    Last Post: 08-23-2011, 12:38 PM
  4. Simple Layout Question
    By Guy in forum AWT / Swing
    Replies: 7
    Last Post: 07-13-2011, 01:57 PM
  5. Simple GUI layout manager
    By globo in forum New To Java
    Replies: 3
    Last Post: 11-16-2010, 01:17 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
  •