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

setMinimumSize & setMaximumSize don't affect on the component (JButton) size if FlowLayout is used

XMLWordPrintable

      javax.swing.JComponent and all subclasses have following read-write properties:
      minimumSize, maximumSize and preferredSize. It is not fully specified is there any relations between such components or not in the javadoc specification.
      In the class JComponent description there is a link to the JComponent class tutorial where we can read that such properties are just hints and might be ingnored by certain layout managers. It is vague.
      It seems the properties should work at least on FlowLayout. But it doesn't. You can reproduce it by running following program on Windows XP:
      ------
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      public class Test2 {
          public static void main(String[] args) {
              final JFrame frame = new JFrame();
              final JButton btn = new JButton("OK");
              btn.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent e) {
                      System.out.println("width: " + btn.getWidth());
                      System.out.println("height: " + btn.getHeight());
                      System.out.println("min size: " + btn.getMinimumSize());
                  }
              });

              btn.setMinimumSize(new Dimension(100, 100));

              frame.getContentPane().setLayout(new FlowLayout());
              frame.add(btn);

              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(200, 200);
              frame.setVisible(true);
          }
      }
      ------
      On our system when I click on the Button I see:
      width: 51
      height: 26
      min size: java.awt.Dimension[width=100,height=100]

      It seems to be strange. I have questions:
      1) Why minimumSize is greater than current size of the component?
      2) Could you please specify a LayoutManager where minimumSize works?
      3) Are there relations between minimumSize and MaximumSize (as well as preferredSize)? So if I set minimumSize greater than maximumSize for the Button in the code above I can see no exceptions, corresponding getters return set values and (as usual) the button size is not changed anyway.




      ###@###.### 2005-04-01 14:43:29 GMT

            Unassigned Unassigned
            ydanilev Yury Danilevich (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: