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 / January 2004

Tip: Looking for answers? Try searching our database.

TitledBorder Text is Truncated

Thread view: 
Tony LaPaso - 26 Jan 2004 03:59 GMT
Hello all,

This must be a common problem. Please tell me it is.

Here's a simple version of the problem: I have a JPanel using a FlowLayout.
Within the JPanel I have one component: a JLabel whose text is a single
character in length. The JPanel has a TitledBorder whose text is much longer
than the singled character JLabel. When the JPanel is displayed its border's
text is truncated because FlowLayout does not respect the text in the
TitledBorder. Actually, *no* layout manager is going to repsect the size of
the border's text, so this isn't a FlowLayout problem per se.

The code sample below shows the exact problem.

I'm hoping to find a (simple) way of making the FlowLayout take into account
the text in the TitledBorder.

Yes, I know I can get the size of the border's text, convert it to pixels,
override the panel's getPreferredSize() method, etc. but that's so ugly.

import java.awt.*;
import javax.swing.*;

public class Tester2 {
 public static void main(String[] args) {
   JFrame f = new JFrame();
   f.getContentPane().setLayout(new FlowLayout());

   JPanel panel = new JPanel();
   panel.setBackground(Color.pink);
   panel.add(new JLabel("X"));
   panel.setBorder(BorderFactory.createTitledBorder("Long TitledBorder
Text"));

   f.getContentPane().add(panel);

   f.pack();
   f.setVisible(true);
 }
}
Tony LaPaso - 26 Jan 2004 05:28 GMT
I think I know the answer to my own question....

First, let me say I think this is a Swing bug (perhaps a FlowLayout bug)
because the preferred size of the panel *does* take into account the
border's height when using a TitledBorder but not the width. The preferred
size of a component *should* take into account both the height *and* width
of a border (if one is in use). If you run the sample program you'll see the
preferred height certainly does take the border's height into account but
not the width.

Second, there is a method in javax.swing.border.TitledBorder:
getMinimumSize(Component c)

This method allows a workaround for the truncation of the border's text. I
simple set the panel's preferred width to that returned by getMinimumSize()
and I'm good to go.

I'm not 100% sure about all this, I'll investigate more tomorrow and post a
bug w/Sun if warranted.

Any comments or suggestions are still very welcome, of course.

> Hello all,
>
[quoted text clipped - 36 lines]
>   }
> }
Andrew Thompson - 26 Jan 2004 05:41 GMT
| I think I know the answer to my own question....
...
| Any comments or suggestions are still very welcome, of course.

(shrugs) It was a very nice example.

I had it running, and saw the behaviour
you described, witthin moments.

Since I had no idea of a fix, thought I'd
wait and see what more constructive
suggestions came before I made a
comment.   ;-)

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
ak - 26 Jan 2004 09:01 GMT
> text is truncated because FlowLayout does not respect the text in the
> TitledBorder. Actually, *no* layout manager is going to repsect the size of
> the border's text, so this isn't a FlowLayout problem per se.

yes, but it is not a bug, because component _must_not_ ask_his_border_ while
computing preferredSize.
Border doesn't have width or height, but only Insets.

____________

http://reader.imagero.com the best java image reader.
hiwa - 26 Jan 2004 09:45 GMT
> Hello all,
>
[quoted text clipped - 36 lines]
>   }
> }

Enforce setBounds() based on the calculation of the text length.


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.