Some problems with JPanel alignment

I have a panel with preferred size set using GridBagLayout. When i add first components to it, the components starts right at the middle of the panel. Even though i used anchor = GridBagConstraints.FIRST_LINE_START, it just stick to the left side of the panel but still in middle of y-axis.

How can i make the components start at top-left corner of the panel?

Thanks alot.

[392 byte] By [echizena] at [2007-10-3 9:17:57]
# 1
set follwing ConstraintsGridBagConstraints gbc = new GridBagConstraints();int anchor = GridBagConstraints.FIRST_LINE_START;gbc.weightx = 0.0;gbc.weighty = 0.0;
Prashant_SDNa at 2007-7-15 4:30:56 > top of Java-index,Desktop,Core GUI APIs...
# 2

[url http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html]How to Use GridBagLayout[/url]

Read the section on weightx, wieghty:

Unless you specify at least one nonzero value for weightx or weighty, all the components clump together in the center of their container.

I never use the GridBagLayout because I find is so confusing. Try using combinations of the other Layout Managers to get you desired layout. A FlowLayout with left alignment would seem to be a good choice in this case.

camickra at 2007-7-15 4:30:56 > top of Java-index,Desktop,Core GUI APIs...
# 3
I have tried the code with anchor and weightx, weighty.. the components did start from left , but the y-axis align is in the middle ... i cant get it to start from the top. :(i felt that it is the alignment problem with the JPanel but i'm not sure about it ... ~.~
echizena at 2007-7-15 4:30:56 > top of Java-index,Desktop,Core GUI APIs...
# 4

Worked fine for me when I add two components to the container.

import java.awt.*;

import javax.swing.*;

public class GridBagTest

{

public static void main(String[] args)

{

JFrame f = new JFrame("GridBagTest");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().setLayout( new GridBagLayout() );

GridBagConstraints gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.FIRST_LINE_START;

gbc.weightx = 1.0;

gbc.weighty = 1.0;

f.getContentPane().add(new JLabel("Name:"), gbc);

gbc.weightx = 0.0;

f.getContentPane().add(new JTextField(10), gbc);

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

}

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-15 4:30:56 > top of Java-index,Desktop,Core GUI APIs...
# 5

I think i have problem with that as i have set preferred size of my panel...

using weightx and weighty it expand the gap of every components ...

any ways to fix that problem? i could get the first component to start at first line using weightx = 1.0 and weighty = 1.0 .. but subsequently next component at gridy = 1 gets a huge gap..

echizena at 2007-7-15 4:30:56 > top of Java-index,Desktop,Core GUI APIs...
# 6
erm i think i got it running by not specifying the preferred size of the panel ... thanks alot camickr :)
echizena at 2007-7-15 4:30:56 > top of Java-index,Desktop,Core GUI APIs...