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

setMinimumSize() has no effect, presumably overridden by setPreferredSize()


    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 1.2.0
    • Fix Version/s: None
    • Component/s: client-libs


      Name: rl16235 Date: 06/15/2000

      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)

        JLabel.SetMinimumSize doesn't work.
        Run the test program. The window contains a JTextField, and a JLabel
        that shows a count of the number of characters in the field.
        The JLabel's minimum width is set to the width of two numeric
        characters, which comes to 14 in the Metal L&F. However, the actual
        width is determined by the number of characters in the field.
        if there are less than two characters in the label, it will be
        smaller than the minimum width, so setMinimumWidth had no effect.
        The second field successfully uses the workaround.
        Am I misunderstanding the intended effect of setMinimumSize()? If
        the minimum size can be overridden by the preferred size, the
        documentation should say so.

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

      public class MinSizeBug extends JPanel
        public static void main(String[] args)
          JFrame mf = new JFrame("JLabel Minimum Size Bug");
          WindowListener wl = new WindowAdapter()
            public void windowClosing(WindowEvent evt) { System.exit(0); }
          mf.setBounds(10, 10, 400, 100);
          System.out.println("Java version " + System.getProperty("java.version"));
          mf.getContentPane().add(new MinSizeBug(), BorderLayout.NORTH);

          super(new GridLayout(0, 1));
          JLabel countLabel = new JLabel("0");
          JLabel fixedLabel = new JLabel("0")
            public Dimension getPreferredSize()
              Dimension pSize = super.getPreferredSize();
              Dimension mSize = getMinimumSize();
              int wid, ht;
              wid = pSize.width < mSize.width ? mSize.width : pSize.width;
              ht = pSize.height < mSize.height ? mSize.height: pSize.height;
              return new Dimension(wid, ht);
        private void setUpField(JLabel lbl)
          JPanel topPnl = new JPanel(new BorderLayout());
          Dimension min = new
      Dimension(lbl.getFontMetrics(lbl.getFont()).stringWidth("00"), 0);
          lbl.setToolTipText("Character Count");
          topPnl.add(lbl, BorderLayout.WEST);
          JTextField txtFld = new JTextField();
          DocumentListener countEar = new Counter(lbl);
          topPnl.add(txtFld, BorderLayout.CENTER);
        private class Counter implements DocumentListener
          private JLabel myLabel;
          private Counter(JLabel lbl) { myLabel = lbl; }
          public void insertUpdate(DocumentEvent e) { doShow(e); }
          public void removeUpdate(DocumentEvent e) { doShow(e); }
          public void changedUpdate(DocumentEvent e) { doShow(e); }
          private void doShow(DocumentEvent evt)
            myLabel.setText("" + evt.getDocument().getLength());
      (Review ID: 103232)




            • Assignee:
              svioletsunw Scott Violet (Inactive)
              rlingaiaorcl Ranjit Lingaiah (Inactive)
            • Votes:
              0 Vote for this issue
              0 Start watching this issue


              • Created: