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

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

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.2.0
    • 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.addWindowListener(wl);
          mf.setBounds(10, 10, 400, 100);
          System.out.println("Java version " + System.getProperty("java.version"));
          mf.getContentPane().add(new MinSizeBug(), BorderLayout.NORTH);
          mf.show();
        }

        MinSizeBug()
        {
          super(new GridLayout(0, 1));
          JLabel countLabel = new JLabel("0");
          setUpField(countLabel);
          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);
            }
          };
          setUpField(fixedLabel);
        }
        
        private void setUpField(JLabel lbl)
        {
          JPanel topPnl = new JPanel(new BorderLayout());
          Dimension min = new
      Dimension(lbl.getFontMetrics(lbl.getFont()).stringWidth("00"), 0);
          lbl.setMinimumSize(min);
          lbl.setHorizontalAlignment(JLabel.RIGHT);
          lbl.setToolTipText("Character Count");
          topPnl.add(lbl, BorderLayout.WEST);
          
          JTextField txtFld = new JTextField();
          DocumentListener countEar = new Counter(lbl);
          txtFld.getDocument().addDocumentListener(countEar);
          topPnl.add(txtFld, BorderLayout.CENTER);
          add(topPnl);
        }
        
        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)
      ======================================================================

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: