Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4201045

Swing TitledBorder needs option to prevent "text cropping"

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Unresolved
    • P4
    • None
    • 1.2.0
    • client-libs
    • Fix Understood
    • generic
    • generic

    Description



      Name: dbT83986 Date: 01/06/99


      If the width of the text string in TitledBorder object exceeds the width
      of the contained component, then the text will get "cropped" off. I feel
      that the class should be changed so that the component would be resized
      to preserve the text. It makes no sense to have a border title with its text
      cropped, so forcing a resize of the contained component is a very natural
      thing to do(since I would have to add the extra code to get it
      done anyway). Basically you treat the text string as an object that
      has its own geometry data that the layout manager should honor. As it is
      now the layout manager ignores the width of the text which is why it is
      getting cropped.

      Here is the code to demonstrate the default behavior and the extra code
      that is needed to prevent the cropping:

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

      class tstTitleCrop {
          public static void main (String args[]) {
              JFrame frame = new JFrame();
              frame.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {System.exit(0);}
              });

              JPanel pane = new JPanel();
              TitledBorder tb;
              //
              // LABEL1 - title border text gets cropped
              //
              JLabel label1 = new JLabel("This is label #1");
              tb = new TitledBorder("This is a very wide border title");
              label1.setBorder(tb);

              //
              // LABEL2 - jump through hoops to prevent title cropping
              //
              JLabel label2 = new JLabel("This is label #2");
              tb = new TitledBorder("This is a very wide border title");
              label2.setBorder(tb);

              // --- Start of extra stuff needed to prevent title cropping
              Dimension ldim = label2.getPreferredSize();
              int h = ldim.height;
              Insets insets = tb.getBorderInsets(label2);
              Dimension dim = tb.getMinimumSize(label2);
              int w = dim.width;
              w += insets.left; // This is needed to preserve pad on right side of title
              label2.setPreferredSize(new Dimension(w,h));
              // --- End of extra stuff

              pane.add(label1);
              pane.add(label2);

              // Then add the components to the frame
              frame.getContentPane().add(pane, BorderLayout.CENTER);

              frame.pack();
              frame.setVisible(true);
          }
      }
      (Review ID: 49113)
      ======================================================================

      Attachments

        Activity

          People

            Unassigned Unassigned
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Imported:
              Indexed: