Results 1 to 5 of 5
  1. #1
    rizowski is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default [GUI] BroderLayout Center Issues

    I have a problem with trying to add a JPanel to the center of my Gui.

    I have a JFrame we will call Main.
    I also have a JPanel we will call Login.

    Main extends JFrame. Main is laid out with a BorderLayout. Main then instatiates Login that extends JPanel and adds them to the CENTER of Main. I Have other things like a menu bar and toolbar taking up the spots of NORTH and SOUTH. EAST and WEST are not filled with anything as of yet.

    From what i know, the Center condition for a BorderLayout will take up as much space is left over. So I was wondering if there was a way I could make Login to be in the center of my window with out it taking up all remaining space? I have tried to set its size to a specific size. But it ends up filling up the remaining space just like the BorderLayout.CENTER constraints says it should.

    This is what it looks like now:

    This is what i want it to look like:

    I have tried to set the size to Login using:
    • SetMaximumSize
    • SetSize
    • SetBounds

    None of them seem to have any effect on it. Any suggestions would be Awesome. Maybe I have been going at this the wrong way. Anyways Thanks

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power


    Well normally a "login" would be done by displaying a modal dialog with your login panel, instead of imbedding the panel into the main frame.

    But if you really want to do this then you need to use "nested panels" to take advantage of other layout manager features. The easiest way is probably:
    Java Code:
    JPanel wrapper = new JPanel( new GridBagLayout() );
    wrapper.add( loginPane );
    frame.add(wrapper, BorderLayout.CENTER);

  3. #3
    rizowski is offline Member
    Join Date
    Nov 2010
    Rep Power


    Would I have to use a GridBag or do you assume i could use another layout?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    There's no "have to" here, but understand that it is easy to center a single component in a container if you use GridBagLayout, and in fact there's no need to use GridBagConstraints in this situation. Also the key concept from camickr's post if you get no other is that you can nest JPanels, each using its own layout and thereby easily create complex-appearing and useful layouts for your gui applications.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

Similar Threads

  1. buttons won't center
    By gib65 in forum AWT / Swing
    Replies: 4
    Last Post: 10-07-2010, 08:49 PM
  2. Media Center
    By udalcse in forum Jobs Offered
    Replies: 0
    Last Post: 10-27-2009, 02:35 PM
  3. jsf center tag needed
    By Srikala in forum JavaServer Faces (JSF)
    Replies: 5
    Last Post: 07-27-2009, 12:49 PM
  4. center a form
    By tommy in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 09:47 PM
  5. Help Center Live 2.1.3
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-06-2007, 04:43 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