Jump to content

GridbagLayout not aligning to the left

- - - - -

  • Please log in to reply
5 replies to this topic

#1
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
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++;

			}

		}

	}


}



#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Attached File  align.png   26.92K   48 downloads
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());   


#3
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

wim DC said:

[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

#4
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
[ATTACH=CONFIG]4370[/ATTACH]

wim DC said:

[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.
Attached File  Codecall1.png   15.85K   17 downloads

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Same thing?
Somewhere you must have this:
x.add(new Panel());
So before doing that, just do

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


#6
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

wim DC said:

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.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users