I'm building a PropertyPanel. Currently I'm using a GridLayout
to manage the JLabels
and their corresponding fields where I can specify the value. But the problem is that the GridLayout
automatically manages the size of the columns: it makes them the same width.
This means when I'm having a big value field, the colum, is getting bigger (which is good), but the other column (with all my JLabel
s) is getting bigger as well. Here is a screenshot:
< BAD
As you can see, the image
property has a huge value, which makes both columns bigger, and I'm having a lot of space after the JLabel
s.
So, I'm searching for a LayoutManager
which makes each column as big as necessary.
I want a layout like this (it's edited with Gimp):
< GOOD
Thanks
Tips on Choosing a Layout Manager Also keep in mind that flexible layout managers such as GridBagLayout and SpringLayout can fulfill many layout needs. Scenario: You need to display a component in as much space as it can get. If it's the only component in its container, use GridLayout or BorderLayout .
Constructors of GridLayout classGridLayout(int rows, int columns): creates a grid layout with the given rows and columns but no gaps between the components. GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the given rows and columns along with given horizontal and vertical gaps.
setSize will resize the component to the specified size. setPreferredSize sets the preferred size. The component may not actually be this size depending on the size of the container it's in, or if the user re-sized the component manually.
The FlowLayout is the default layout. It layout the components in a directional flow. The GridLayout manages the components in the form of a rectangular grid. This is the most flexible layout manager class.
You can use SpringLayout for this. See How to Use SpringLayout.
Example layout:
Remember that you also can nest layouts.
SpringLayout
is what I typically use for forms like this. Although I think GridBagLayout
would also work nicely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With