Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

JFrame does not automatically resizes to JLabel's length

Thread view: 
Viet Yen Nguyen - 24 Dec 2003 19:59 GMT
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 - 24 Dec 2003 20:33 GMT
> Hi,
>
[quoted text clipped - 13 lines]
> I have tried to invalidate the JLabel, but that doesn't seem to do it.
> Neither changing the LayoutManager, which gives bizarre results.

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.

Signature

Mike W

Viet Yen Nguyen - 24 Dec 2003 21:36 GMT
<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.
[quoted text clipped - 5 lines]
> 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 - 24 Dec 2003 22:57 GMT
> <snip, whole story about the resizing mess>
>
[quoted text clipped - 14 lines]
> 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?

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

--
Mike W
Andrew Thompson - 25 Dec 2003 04:11 GMT
...
> 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 - 25 Dec 2003 08:01 GMT
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 - 25 Dec 2003 15:16 GMT
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!" );
}
}

--
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 - 25 Dec 2003 16:23 GMT
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 - 30 Dec 2003 13:33 GMT
>Hi,
>
[quoted text clipped - 5 lines]
>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
Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.