Results 1 to 3 of 3
  1. #1
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default GridBagLayout alignment problem

    The problem is that the GridBagLayout always CENTERS the layout within the container, if there is enough space. No matter what alignmet(x,y) I assign to the components, if there is enough space, they are always being put to the center. In fact if I call the getLayoutAlignment method of the GridBagLayout it always returns "0.5" (CENTER).
    Is there a way to have my componets to be ALWAYS on the top left side of their container ?

    EDIT:
    here is what i get:

    with:
    Java Code:
    m.gridx = 0;
    		m.gridy = cronLimit-subCount;
    		m.gridwidth = 1;
    		m.gridheight = 1;
    		m.weightx = 1;
    		[COLOR="red"]m.weighty = 1;[/COLOR]
    		m.anchor = GridBagConstraints.NORTHWEST;
    		m.insets = new Insets(10,100,0,0);
    		m.fill = GridBagConstraints.NONE;
    		mid.add(cron.lastElement(), m);
    i get
    http://img21.imageshack.us/img21/5136/img11gm.jpg
    with:
    Java Code:
    m.gridx = 0;
    		m.gridy = cronLimit-subCount;
    		m.gridwidth = 1;
    		m.gridheight = 1;
    		m.weightx = 1;
    		[COLOR="red"]m.weighty = 0;[/COLOR]
    		m.anchor = GridBagConstraints.NORTHWEST;
    		m.insets = new Insets(10,100,0,0);
    		m.fill = GridBagConstraints.NONE;
    		mid.add(cron.lastElement(), m);
    i get
    http://img805.imageshack.us/img805/5633/img12n.jpg

    With "weighty = 0" it has the right alignment but the problem is the extra space between the firsts components.

    EDIT 2: Since i wasn't able to find a better fix, i just used a filler panel:
    Java Code:
    		JPanel filler = new JPanel();
    		filler.setOpaque(false);
    		m.gridx = 0;
    		m.gridy = cronLimit;
    		m.gridwidth = 1;
    		m.gridheight = 1;
    		m.weightx = 1;
    		m.weighty = 1;
    		mid.add(filler, m);
    Last edited by adwart; 08-15-2011 at 07:29 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    12

    Default

    The problem is that the GridBagLayout always CENTERS the layout within the container,
    Read the Swing tutorial on How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container). Especially the part on Specifying Constrants and the weightx/weighty paramaters.

  3. #3
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Read the Swing tutorial on How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container). Especially the part on Specifying Constrants and the weightx/weighty paramaters.
    I edited the first post to help everyone understand my problem.

Similar Threads

  1. Gridbaglayout, hint on label alignment...
    By Marcoita_88 in forum AWT / Swing
    Replies: 1
    Last Post: 08-09-2011, 08:53 PM
  2. BoxLayout Alignment problem
    By whiteMath in forum AWT / Swing
    Replies: 12
    Last Post: 07-19-2010, 10:04 PM
  3. Spacing/Alignment problem with java 2D printing
    By myselfmayur in forum Java 2D
    Replies: 2
    Last Post: 06-25-2010, 03:37 PM
  4. GridLayout alignment problem
    By q8inq8 in forum New To Java
    Replies: 9
    Last Post: 03-16-2010, 07:54 PM
  5. [SOLVED] alignment problem
    By nanimtech in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-10-2008, 01:23 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
  •