File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes GridBagLayout components are  centered Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GridBagLayout components are  centered" Watch "GridBagLayout components are  centered" New topic
Author

GridBagLayout components are centered

Paul Murphy
Greenhorn

Joined: Jan 03, 2002
Posts: 6
Hi all. I should begin by letting you know that i am fairly new to swing, so please forgive my lack of knowledge.

Here's my problem. Im trying to create a GUI with multiple panels. I've created a root panel or contentPanel for all other panel to be placed onto. At the top of this panel, occupying 1/3 of the screen I have created a header panel, which itself will contain two more panels, search and book. Within these two panels (search and book) my components are centred. However i would like the components to flow from the top of the panel. I'm using a GridBagLayout for these two panels(search and book) and i tried to use the setAnchor(GridBagConstraints.PAGE_START) method to align the components with no joy.

Any ideas or helpful comments would be greatly appreciated



Trying is the first step towards failure!!!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by Paul Murphy:
...

Here's my problem...
...Within these two panels (search and book) my components are centred. However i would like the components to flow from the top of the panel. I'm using a GridBagLayout for these two panels(search and book) and i tried to use the setAnchor(GridBagConstraints.PAGE_START) method to align the components with no joy.


When asking questions about laying out compomenents, you may do well to give a much more detailed description of what you are trying to accomplish, that or post an image of what you want to create. Compilable and runnable code would also help quite a bit. We have to see it, either literally or figuratively, to understand it.

Good luck.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> GridBagLayout components are centered

generally indicates not setting gridBagConstraint's weightx/weighty.
(read the apidocs for detailed explanation)

do you really need to use a GridBagLayout?
perhaps a JSplitPane might be easier to give you the 1/3 & 2/3 split
Paul Murphy
Greenhorn

Joined: Jan 03, 2002
Posts: 6
Hi guys

I must apologise about the confusing post. I should probably just said that my problem was aligning components (JLabel, JTextField, JButton) in a panel which uses GridBagLayout.

Michael you were right about the GridBagConstraints weightx and weighty. That fixed the problem for me. But i've since changed the layout to a GroupLayout. Seems to be the more sensible option...at least till i meet another problem.

Many thanks for your help guys
 
Consider Paul's rocket mass heater.
 
subject: GridBagLayout components are centered
 
Similar Threads
i keep on seeing the console window and not the applet
create screen comaptible for all resoluions?
How do I pass a frame into another class?
keyboard action of my JButton
GridBagLayout