Results 1 to 4 of 4
  1. #1
    simon66 is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Java Layout not working ?!

    Hi all!

    I'm new to java GUI and I ran into a problem that I can't find a solution.

    My app is split into 3 parts (NORTH,CENTER,SOUTH.... You knew that already right?)

    But on my north I want to have 2 JPanels side by side.

    Now that seems to be impossible! I can't get it to work.

    Here is my code.

    Java Code:
    JPanel pNorth = new JPanel( new GridBagLayout() );
    GridBagConstraints c = new GridBagConstraints();
      
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setTitle("Google Static Maps");
    setIconImage(null);
    setLocation(250,300);
    setSize(700,500);
      
    add(pNorth, BorderLayout.NORTH);
    
    c.insets = new Insets(10,10,10,10);
    //c.gridx = 10;
    //c.gridy = 1;
    pNorth.add(panel1, c);
    pNorth.add(panel1,c);
        
    panel1.setBorder(new CompoundBorder(
    			new TitledBorder("Configure the inputs to Google Static Maps"),
    			Borders.DLU2_BORDER));
      
    panel2.setBorder(new CompoundBorder(
    			new TitledBorder("Configure the inputs to Google Static Maps"),
    			Borders.DLU2_BORDER)); 
    }
    The thing here is that panel1 is smaller than panel 2, and they are both side by side but not at the left corner.
    How can I have my panel1 on the left corner and Panel2 right beside it.

    What I did was create a Panel called pNorth and I set that to North, then I added both panels inside of it. Should I do it like that or is there a better way?

    Thank you!

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

    Default Re: Java Layout not working ?!

    Quote Originally Posted by simon66 View Post
    What I did was create a Panel called pNorth and I set that to North, then I added both panels inside of it. Should I do it like that or is there a better way?
    That's the best way -- you should nest JPanels, each using its own layout. You may want to play with the layout manager of the pNorth JPanel, for instance trying GridLayout, or BoxLayout.

    Welcome to the forum by the way! Look forward to seeing you around!

  3. #3
    simon66 is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Java Layout not working ?!

    But how can I get the inner JPanel to be at the left of the parent frame?

    Thank you!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Java Layout not working ?!

    Moved from New to Java.

    Quote Originally Posted by simon66 View Post
    how can I get the inner JPanel to be at the left of the parent frame?
    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Grid Layout change layout alignment of control s
    By rellicott in forum SWT / JFace
    Replies: 1
    Last Post: 02-13-2012, 06:11 PM
  2. GUI layout not working
    By jobud9 in forum New To Java
    Replies: 4
    Last Post: 01-15-2012, 05:27 AM
  3. My Second Layout is not working(Tapestry 5)
    By yuzi123 in forum Web Frameworks
    Replies: 0
    Last Post: 11-25-2011, 08:30 AM
  4. Replies: 8
    Last Post: 05-28-2008, 08:00 AM
  5. Java GUI layout
    By nick2price in forum New To Java
    Replies: 2
    Last Post: 05-27-2008, 10:42 PM

Posting Permissions

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