• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

GridBagLayout components are centered

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!