This discussion is archived
4 Replies Latest reply: Oct 7, 2004 10:44 AM by 807587 RSS

TitledBorder Text is Truncated

807587 Newbie
Currently Being Moderated
Hello all,

This must be a common problem.

Here's a simple version of the problem: I have a JPanel using a FlowLayout. Within the JPanel I have a JLabel whose text is a single character in length. When I place a TitledBorder around the JPanel, the border's text is truncated because the FlowLayout layout manager does not consider the border text when it gets the size of the JPanel. Please see the sample code below.

Is there a way to make sure the border's title is taken into account by the layout manager?

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);
  }
}
  • 1. Re: TitledBorder Text is Truncated
    807587 Newbie
    Currently Being Moderated
    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,

    This must be a common problem.

    Here's a simple version of the problem: I have a
    JPanel using a FlowLayout. Within the JPanel I have a
    JLabel whose text is a single character in length.
    When I place a TitledBorder around the JPanel, the
    border's text is truncated because the FlowLayout
    layout manager does not consider the border text when
    it gets the size of the JPanel. Please see the sample
    code below.

    Is there a way to make sure the border's title is
    taken into account by the layout manager?

    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);
    }
    }
  • 2. Re: TitledBorder Text is Truncated
    807587 Newbie
    Currently Being Moderated
    This is not a bug - it might be called a deficiency of Swing borders. Borders can add insets to a component but are not able to change the preferred size completely.

    Sven
  • 3. Re: TitledBorder Text is Truncated
    807587 Newbie
    Currently Being Moderated
    I don't appear to success in using your work around.
    I tried
    panel.setPreferredSize((int)border.getMinimumSize(panel).getWidth(), (int)panel.getPreferredSize().getHeight());
    and also
    panel.setPreferredSize(border.getMinimumSize(panel))
    but it didn't work...
  • 4. Re: TitledBorder Text is Truncated
    807587 Newbie
    Currently Being Moderated
    Did anyone figure out the code necessary to get the workaround to work?