I’m using a FlowLayout JPanel. The panel looks ugly when child component heights are different. I’m looking for a solution to make them top-align (similar to valign="top" with table cells in HTML).
I’m using a FlowLayout JPanel . The panel looks ugly when child component heights
Share
I realize this question was asked over a year ago, but like me, I thought many would stumble across this forum post and be left attempting to make a workaround like that one suggested in the bug report (failed to work for me just fyi).
Either way there is a better answer since JDK 1.6. Flowlayout has the following method:
If you use this method on your flowlayout and set it to true, then when flowlayout lays out the components it will check each component’s baseline and align the component along this baseline.
But that’s not all you need to do.
The component in question must override the following two methods in this way:
They are methods in JComponent and layouts and layoutmanagers use these methods to determine how to layout the component.
If you take the steps mentioned above all the components will align themselves along the top of each row. Of course if you just want to use a component like JButton you will obviously have to extend it in order to achieve your desired goal… but it’s not as much work as overriding layoutcontainer with a workaround that you have to debug. At least I think so.
Good luck,
-Asaf