Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: two labels in sequence, how hard can it be?

17 views
Skip to first unread message
Message has been deleted
Message has been deleted

Daniele Futtorovic

unread,
May 7, 2013, 6:12:21 PM5/7/13
to
On 07/05/2013 22:44, Stefan Ram allegedly wrote:
> I wanted to have a box starting with two labels, flush left, like:
>
> ..------------------------------------
> |[label alpha]
> |[label beta]
> |...
>
> . Turned out, to add such a label, I had to do all of the following:
>
> final javax.swing.Box vbox = javax.swing.Box.createVerticalBox();
> { final javax.swing.Box hbox = javax.swing.Box.createHorizontalBox();
> final javax.swing.JLabel label = new javax.swing.JLabel( "alpha" );
> hbox.add( label );
> hbox.add( javax.swing.Box.createGlue() );
> hbox.setMaximumSize
> ( new java.awt.Dimension
> ( java.lang.Integer.MAX_VALUE, label.getHeight() ));
> vbox.add( hbox ); }
> ....
>
> When I add the label to the vbox directly (without an hbox),
> it is not precisely aligned left, even when left alignment
> is request. There still seemed to be some �glue� on the left
> of it, which I did not add there.
>
> When I do not setMaximumSize, the height of the hbox is much
> larger than the height of the label, that is: too large.
>
> But maybe my solution is too complicated? Is there a more
> simple solution that I am not aware of?
>

Never liked Box'es much...

- GridBagLayout
- weightx = 1; weighty = 0; anchor = EAST; gridx = 0; gridy = 0;
- add first label;
- ++gridy;
- add second label;

If the Panel is bigger than those two labels and you want them pushed to
the top:
- ++gridy; weighty = 1
- add Box.createGlue();

--
DF.

FredK

unread,
May 8, 2013, 4:20:54 PM5/8/13
to
On Tuesday, May 7, 2013 3:12:21 PM UTC-7, Daniele Futtorovic wrote:
> On 07/05/2013 22:44, Stefan Ram allegedly wrote: > I wanted to have a box starting with two labels, flush left, like: > > ..------------------------------------ > |[label alpha] > |[label beta] > |... > > . Turned out, to add such a label, I had to do all of the following: > > final javax.swing.Box vbox = javax.swing.Box.createVerticalBox(); > { final javax.swing.Box hbox = javax.swing.Box.createHorizontalBox(); > final javax.swing.JLabel label = new javax.swing.JLabel( "alpha" ); > hbox.add( label ); > hbox.add( javax.swing.Box.createGlue() ); > hbox.setMaximumSize > ( new java.awt.Dimension > ( java.lang.Integer.MAX_VALUE, label.getHeight() )); > vbox.add( hbox ); } > .... > > When I add the label to the vbox directly (without an hbox), > it is not precisely aligned left, even when left alignment > is request. There still seemed to be some �glue� on the left > of it, which I did not add there. > > When I do not setMaximumSize, the height of the hbox is much > larger than the height of the label, that is: too large. > > But maybe my solution is too complicated? Is there a more > simple solution that I am not aware of? > Never liked Box'es much... - GridBagLayout - weightx = 1; weighty = 0; anchor = EAST; gridx = 0; gridy = 0; - add first label; - ++gridy; - add second label; If the Panel is bigger than those two labels and you want them pushed to the top: - ++gridy; weighty = 1 - add Box.createGlue(); -- DF.

Or use a BorderLayout with one label in NORTH and one in SOUTH
--
Fred K

Roedy Green

unread,
May 29, 2013, 3:18:51 AM5/29/13
to
On 7 May 2013 20:44:55 GMT, r...@zedat.fu-berlin.de (Stefan Ram) wrote,
quoted or indirectly quoted someone who said :

> But maybe my solution is too complicated? Is there a more
> simple solution that I am not aware of?

Have a look at some of the other layout managers. If you do this
often, consider rolling your own. It is much easier than you might
think.

see http://mindprod.com/jgloss/layoutmanager.html
--
Roedy Green Canadian Mind Products http://mindprod.com
Technological possibilities are irresistible to man.
If man can go to the moon, he will.
If he can control the climate, he will.
~ John von Neumann (born: 1903-12-28 died: 1957-02-08 at age: 53)
0 new messages