Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

GridbagLayout not aligning to the left

align grid

  • Please log in to reply
5 replies to this topic

#1 vaironl

vaironl

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 138 posts
  • Programming Language:Java
  • Learning:Java, Objective-C, Python, JavaScript

Posted 06 December 2011 - 02:52 PM

Hello forum, Vaironl here.
I have a JPanel containing a gridbaglayout which I would like to align to the left side of my panel. When I write the code to anchor my labels to
the left it still does not work. I have indeed done some research and ask this question somewhere else but the help I received did not fix my problem.

This class extends the JPanel

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;

public class Panel extends JPanel{


	JLabel field[] = new JLabel[60];
	public Panel()
	{
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.gridx=0;
		c.gridy=0;
		
		c.anchor = GridBagConstraints.WEST; //Align to the left?
		
		setBackground(Color.red);
		
		for(int i=0;i<field.length;i++)
		{
			field[i] = new JLabel("Test"); // Initialize labels
			field[i].setText("Field["+(i+1)+"]");
			add(field[i], c);
			
			if(c.gridx== 2) // if 3 colums are made go to next row and start colums from 0
			{
				c.gridx= 0;
				c.gridy++;
			}
			else
			{
				c.gridx++;
			}
		}
	}

}

  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 06 December 2011 - 11:36 PM

align.png
Seems perfectly aligned to the left to me.

Maybe you're confusing the aligment of the stuff added to the grid with the alignment of the whole grid itself.
To adjust the grid's alignment you'll need to set the layout of whatever you add this panel to:
Like so:
        JFrame frame = new JFrame("test");
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.add(new Panel());   

  • 0

#3 vaironl

vaironl

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 138 posts
  • Programming Language:Java
  • Learning:Java, Objective-C, Python, JavaScript

Posted 07 December 2011 - 04:16 AM

[ATTACH=CONFIG]4368[/ATTACH]
Seems perfectly aligned to the left to me.

Maybe you're confusing the aligment of the stuff added to the grid with the alignment of the whole grid itself.
To adjust the grid's alignment you'll need to set the layout of whatever you add this panel to:
Like so:

        JFrame frame = new JFrame("test");
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.add(new Panel());   


Thans wim,
This was exactly what I needed I thought the things inside of the panel would be align to the left.

Once again thanks!! one more thing I clearly understand now
  • 0

#4 vaironl

vaironl

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 138 posts
  • Programming Language:Java
  • Learning:Java, Objective-C, Python, JavaScript

Posted 07 December 2011 - 10:06 AM

[ATTACH=CONFIG]4370[/ATTACH]

[ATTACH=CONFIG]4368[/ATTACH]
Seems perfectly aligned to the left to me.

Maybe you're confusing the aligment of the stuff added to the grid with the alignment of the whole grid itself.
To adjust the grid's alignment you'll need to set the layout of whatever you add this panel to:
Like so:

        JFrame frame = new JFrame("test");
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.add(new Panel());   


Wim how could I actually do this if I have a Jframe which contains a Jpanel.
That JPanel contains labels, textfields, a big JEditorPane, and another JPanel and I'm using absolute position to place all of those things.
Now the subJpanel is the one that needs to be aligned to the left.
Here it's a picture is since my explanation is not reliable.
Codecall1.png
  • 0

#5 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 08 December 2011 - 12:51 AM

Same thing?
Somewhere you must have this:
x.add(new Panel());
So before doing that, just do

x.setLayout(new FlowLayout(FlowLayout.LEFT));

  • 0

#6 vaironl

vaironl

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 138 posts
  • Programming Language:Java
  • Learning:Java, Objective-C, Python, JavaScript

Posted 08 December 2011 - 03:18 AM

Same thing?
Somewhere you must have this:

x.add(new Panel());
So before doing that, just do

x.setLayout(new FlowLayout(FlowLayout.LEFT));


Sorry for the misleading info. If you look at the picture at the bottom there is a red rectangle with ingredients labels. I would just like the ingredients to be set to the left not the entire panel.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download