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

JFrame does not automatically resizes to JLabel's length

1,229 views
Skip to first unread message

Viet Yen Nguyen

unread,
Dec 24, 2003, 2:59:14 PM12/24/03
to
Hi,

I am using three JLabel's on a row using the BorderLayout. The texts of the
JLabel's varies by size, which depends on user input. Unfortunaly, when the
JLabel in the center of the BorderLayout (BorderLayout.CENTER) gets too
large, it gets truncated and appended by three dots (...).

It seems that running pack on JFrame after every JLabel.setText() does solve
the problem, however, it isn't a really clean way because in that case the
JLabel must be aware of the JFrame, which shouldn't be necessary.

So to make a long story short, I would like a way so that the JFrame gets
automatically resized when there is a shortage of space for the components,
without having to call pack().

I have tried to invalidate the JLabel, but that doesn't seem to do it.
Neither changing the LayoutManager, which gives bizarre results.

Thanks in advance,

Viet Yen Nguyen


VisionSet

unread,
Dec 24, 2003, 3:33:45 PM12/24/03
to

"Viet Yen Nguyen" <vietyen at quicknet dot nl> wrote in message
news:48a26$3fe9f013$d549b724$21...@news.multikabel.nl...

I haven't tried this, let us know if it works.

Use a subclass of JLabel, override it's getPreferredSize() method to call
setSize() on the parent frame with a function of the dimension it returns.

myJFrame.getSize().width + super.getPreferredSize().width // or something
less crude!

Then when you call validate, getPreferredSize() will be called which so long
as you call super.getPreferredSize() within it should allow for the labels
text.

--
Mike W


Viet Yen Nguyen

unread,
Dec 24, 2003, 4:36:13 PM12/24/03
to

"VisionSet" <sp...@ntlworld.com> wrote in message
news:4GmGb.10101$FN....@newsfep4-winn.server.ntli.net...

<snip, whole story about the resizing mess>

> Use a subclass of JLabel, override it's getPreferredSize() method to call
> setSize() on the parent frame with a function of the dimension it returns.
>
> myJFrame.getSize().width + super.getPreferredSize().width // or something
> less crude!
>
> Then when you call validate, getPreferredSize() will be called which so
long
> as you call super.getPreferredSize() within it should allow for the labels
> text.

I've tried to call setSize() on the panel on which the JLabels are residing.
I have also outputted the preferred sizes of both the label and the panel,
and it seems that those values are correctly updated according to the length
of the String. However, it seems that the actual sizes are not being changed
using these preferable values. Weird, could this be a LayoutManager problem?

Viet Yen Nguyen


VisionSet

unread,
Dec 24, 2003, 5:57:12 PM12/24/03
to
"Viet Yen Nguyen" <vietyen at quicknet dot nl> wrote in message
news:7dd1c$3fea06cf$d549b724$27...@news.multikabel.nl...

Post the simplest bit of *compilable* code you can to illustrate the problem

--
Mike W


Andrew Thompson

unread,
Dec 24, 2003, 11:11:54 PM12/24/03
to
"Viet Yen Nguyen" <vietyen at quicknet dot nl> wrote in message
news:48a26$3fe9f013$d549b724$21...@news.multikabel.nl...
...

> I am using three JLabel's on a row using the BorderLayout. The texts of
the
> JLabel's varies by size, which depends on user input. Unfortunaly, when
the
> JLabel in the center of the BorderLayout (BorderLayout.CENTER) gets too
> large, it gets truncated and appended by three dots (...).
>
> It seems that running pack on JFrame after every JLabel.setText()

Changing the size of the frame in order to show
a message is usually not the best strategy.

Give us a _short_, self contained, compileable
example of what you have, and we may be able
to suggest better ways to go about it.

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site


Viet Yen Nguyen

unread,
Dec 25, 2003, 3:01:06 AM12/25/03
to

"Viet Yen Nguyen" <vietyen at quicknet dot nl> wrote in message
news:48a26$3fe9f013$d549b724$21...@news.multikabel.nl...

Hi,

> I am using three JLabel's on a row using the BorderLayout. The texts of
the
> JLabel's varies by size, which depends on user input. Unfortunaly, when
the
> JLabel in the center of the BorderLayout (BorderLayout.CENTER) gets too
> large, it gets truncated and appended by three dots (...).

Weird, I have posted a message six hours ago but it did not got seen. Oh
well, here is the example code again:

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class PanelTest extends JFrame {

public PanelTest() {
// this string is displayed correctly
JLabel label = new JLabel("testdkfjsldfjlsjdfsdkfjkdfjdf");
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(label);
this.pack();
this.setVisible(true);

/* the following string will be truncated by three dots (...)
but i want the JFrame automatically resized instead of a truncated
JLabel.
*/
label.setText("testdkfjsldfjlsjdfsdkfjkdfjdf" + "dkfjdfd");
}
public static void main(String[] args) {
new PanelTest();
}
}

Viet Yen


Andrew Thompson

unread,
Dec 25, 2003, 10:16:17 AM12/25/03
to
"Viet Yen Nguyen" <vietyen at quicknet dot nl> wrote in message
news:b617d$3fea9942$d549b724$27...@news.multikabel.nl...

I still think it's a damn fool idea to resize the UI
just because you have a message, (there are other
stategies you can use depending on what is most
appropriate for the circumstances)

Still..
__________________________________
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LabelTestFrame extends JFrame
implements ActionListener
{

JLabel label = new JLabel("testdkfjsldfjlsjdfsdkfjkdfjdf");

JButton b = new JButton("Resize");

public LabelTestFrame() {


// this string is displayed correctly

this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(label, BorderLayout.SOUTH);
this.getContentPane().add(b, BorderLayout.NORTH);
b.addActionListener(this);
this.pack();
this.setVisible(true);
}

public void setMessage(String s)
{
label.setText(s);
pack();
setSize( getPreferredSize() );
}

public static void main(String[] args) {

new LabelTestFrame();
}
public void actionPerformed(ActionEvent ae)
{
setMessage( label.getText() + " blah!" );

Viet Yen Nguyen

unread,
Dec 25, 2003, 11:23:41 AM12/25/03
to
"Andrew Thompson" <andr...@bigNOSPAMpond.com> wrote in message
news:5bDGb.64861$aT.3...@news-server.bigpond.net.au...

Hi,

> I still think it's a damn fool idea to resize the UI
> just because you have a message, (there are other
> stategies you can use depending on what is most
> appropriate for the circumstances)

I agree with you that resizing the UI is bad in almost any situation,
however in my case it is even worse to keep the label truncated.

With your example it requires to have the JLabel in reach of the JFrame.
This is true in the example I have given, but unfortunaly not in the
application I am building. However, I have found a static method
JFrame.getFrames() which returns an array of running frames. Since I have
only one frame this shouldn't give any problems, but to my intuition it
isn't a really neat solution.

Thank you and VisionSet for your help and have a merry Christmas to you all.

Viet Yen Nguyen

Bent C Dalager

unread,
Dec 30, 2003, 8:33:37 AM12/30/03
to
In article <26818$3feb0f0d$d549b724$22...@news.multikabel.nl>,

Viet Yen Nguyen <vietyen at quicknet dot nl> wrote:
>"Andrew Thompson" <andr...@bigNOSPAMpond.com> wrote in message
>news:5bDGb.64861$aT.3...@news-server.bigpond.net.au...
>
>Hi,
>
>> I still think it's a damn fool idea to resize the UI
>> just because you have a message, (there are other
>> stategies you can use depending on what is most
>> appropriate for the circumstances)
>
>I agree with you that resizing the UI is bad in almost any situation,
>however in my case it is even worse to keep the label truncated.

You should probably make sure the frame is initially opened large
enough to contain whatever it may later want to display.

Alternatively, you might make it initially large enough that the need
for subsequent resizing is minimized.

Cheers
Bent D
--
Bent Dalager - b...@pvv.org - http://www.pvv.org/~bcd
powered by emacs

0 new messages