Resizing a JLabel

C

CD1

Hi guys!

There's a situation in my program that I want to resize a JLabel:

I have a JDialog with a JPanel on it (with FlowLayout - it's one of the
panels... not the only one). There's a JTextField, where the user can
type a numeric code and after it, there's JLabel INITIALLY
[height=0;width=0]. When the user types a valid code, I wanna retrieve
the name of the person associated with it. That's simple and it's
already done. I SELECT it from the database and
mylabel.setText(myname). The JLabel shows it. BUT (here's the "but"!)
if the name is longer than the JDialog width, the JLabel doesn't show
nothing of the string! If i resize to the necessary width, it pops up
the name, suddenly. I wanted it to always show the part of the name
that fits in the JDialog, with a "..." in the end, like "I have a big
name and ..." (if the screen ended up here). Got it?

I tried to calculate the available width, but it seems complicated... I
had to calculate the JDialog's width minus the JTextField's width minus
the JLabel's (associated with the JTextField) width minus the gaps
between them... I think that'd be a simpler way to do it.

Thank you, anyway

Crístian Deives
 
K

Kari Ikonen

CD1 said:
I have a JDialog with a JPanel on it (with FlowLayout - it's one of the
...
if the name is longer than the JDialog width, the JLabel doesn't show

Yes, that is behaviour of FlowLayout. You could try using GridBagLayout
instead.

I tried to calculate the available width, but it seems complicated...

Alternative: Derive you own FixedFlowLayout from the FlowLayout, and perform
necessary size calculation in layoutContainer. It is actually rather simple
trick to do. You propably just need to preferred size of label to be
appropriate one, and then call super.layoutContainer()
 
I

IchBin

Kari said:
CD1 wrote:




Yes, that is behaviour of FlowLayout. You could try using GridBagLayout
instead.





Alternative: Derive you own FixedFlowLayout from the FlowLayout, and perform
necessary size calculation in layoutContainer. It is actually rather simple
trick to do. You propably just need to preferred size of label to be
appropriate one, and then call super.layoutContainer()


I find that the layout manager from JGoodies (Free) is much nicer. It is
more powerful and can build much faster than GridBagLayout. Examples
where I use JGoodies LAF and Forms manager is at:
http://home.ptd.net/~weconsul/

For more information go to:
https://jgoodies.dev.java.net/
or
http://www.jgoodies.com/freeware/index.html


Hope this Helps...
IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances:
if there is any reaction, both are transformed.'
- Carl Gustav Jung, (1875-1961), psychiatrist and psychologist
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,796
Messages
2,569,645
Members
45,369
Latest member
Carmen32T6

Latest Threads

Top