Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Top alignment for FlowLayout

Tags:

java

layout

swing

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).

like image 698
mrpaint Avatar asked Apr 30 '10 08:04

mrpaint


People also ask

Which method is used to set the alignment in FlowLayout?

Flow layouts are typically used to arrange buttons in a panel. It arranges buttons horizontally until no more buttons fit on the same line. The line alignment is determined by the align property.

What is the default orientation of FlowLayout?

When the FlowLayout object controls a container with a left-to right component orientation (the default), the LEADING value specifies the components to be left-aligned and the TRAILING value specifies the components to be right-aligned.

What is a FlowLayout good for?

A flow layout arranges components in a left-to-right flow, much like lines of text in a paragraph. Flow layouts are typically used to arrange buttons in a panel. It will arrange buttons left to right until no more buttons fit on the same line.

What pattern is FlowLayout manager?

FlowLayout is a simple layout manager that tries to arrange components at their preferred sizes, from left to right and top to bottom in the container. A FlowLayout can have a specified row justification of LEFT , CENTER , or RIGHT and a fixed horizontal and vertical padding.


2 Answers

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:

public void setAlignOnBaseline(boolean alignOnBaseline)

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:

@Override
public Component.BaselineResizeBehavior getBaselineResizeBehavior() {
    return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
}

@Override
public int getBaseline(int width, int height) {
    return 0;
}

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

like image 105
Asaf Avatar answered Oct 15 '22 22:10

Asaf


Someone else has wished for this, in the form of a bug-report (which also lists a workaround).

Have a look at

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4295966

like image 28
aioobe Avatar answered Oct 15 '22 23:10

aioobe