Question

I've two JPanels inside a JFrame but when one JPanel is bigger in height that other then it is being automatically aligned vertically center.I'm using GridBagLayout.Why is that though I set weighty=0? What should I do for aligning Panel1 to top whether Panel2 is bigger or smaller? Thanks. Two JPanel inside a JFrame

Was it helpful?

Solution

GridBagLayout is honouring the preferred size of the components.

If you would like the components to have the same height, you can use the fill property of GridBagConstraints and set it to GridBagConstraints.HORIZONTAL, which will tell GridBagLayout to fill all the component within the given row so that they meet the height of the row (which should be the height of highest component in the row)

If you just wish to align the components (to the top), you should change the anchor property to GridBagConstraints.NORTH

See How to use GridBagLayout for more details

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top