Results 1 to 5 of 5
  1. #1
    P123 is offline Member
    Join Date
    Mar 2016
    Posts
    1
    Rep Power
    0

    Default GridBagLayout problem

    Hello.

    I'm new to the forum and very new to java layouts. I've written some small apps using BoxLayouts, but this is my first attempt with GridBagLayouts.

    Anyway, i have a JPanel which is dynamically resized by selections in a JComboBox contained in the JPanel. When i debug the app, the layout is correct in terms of the Y positions, but the widths are just over half what they should be.

    After changing the selection in the JComboBox, everything works perfectly.

    Before setting up the layout, i set the min, max, preferred, and size of the JPanel and all of the components.

    Can anyone tell me what's missing?
    Thanks...

  2. #2
    jameswarner is offline Member
    Join Date
    Mar 2016
    Location
    9345 Cemetery Dr. Scarsdale
    Posts
    5
    Rep Power
    0

    Default Re: GridBagLayout problem

    Hi P123, you can't set layout at JComboBox. Here some example help how to set layout size in JPanel?

    public newPanel1() {
    setLayout(new GridLayout(2,0));

    String[] example = new String[] {"one, "two", "three", "four"};

    JComboBox cBox = new JComboBox(levels);
    cBox.setPreferredSize(new Dimension(300, 20));

    this.add(new JLabel("This is example of JcomboBox"));
    this.add(cBox);
    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: GridBagLayout problem

    Quote Originally Posted by P123 View Post
    Before setting up the layout, i set the min, max, preferred, and size of the JPanel and all of the components.
    That's probably the cause of your problems. When using a layout correctly, it's almost never necessary to set any sizes in your code. Just let the layout manager take care of sizing.

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

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: GridBagLayout problem

    Darryl, good answer, I agree (also: MigLayout for the win!).

    Please note that this post is almost a year old. It's jameswarner resurrecting old threads with his mysterious answers...
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

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

    Default Re: GridBagLayout problem

    Quote Originally Posted by SurfMan View Post
    Please note that this post is almost a year old. It's jameswarner resurrecting old threads with his mysterious answers...
    Missed that, thanks.

    db

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

Similar Threads

  1. Problem with GridBagLayout.
    By orushw in forum AWT / Swing
    Replies: 1
    Last Post: 05-26-2012, 03:12 PM
  2. GridBagLayout Component Problem...
    By danpotter in forum AWT / Swing
    Replies: 5
    Last Post: 05-09-2012, 11:34 PM
  3. GridBagLayout problem!
    By canersen in forum AWT / Swing
    Replies: 9
    Last Post: 03-06-2011, 11:35 PM
  4. Problem with GridBagLayout
    By ze snow in forum New To Java
    Replies: 20
    Last Post: 02-28-2010, 06:08 AM
  5. Problem with GridBagLayout
    By Daniel in forum SWT / JFace
    Replies: 2
    Last Post: 07-01-2007, 06:57 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
  •