Skip to main content

(Not So) Stupid Questions 6: Comparability of Minimum, Maximum Dimensions

November 24, 2005


(Not So) Stupid Questions

Comparability of Minimum, Maximum Dimensions





Editor's note: Sometimes, the most interesting discussions begin when someone says, "This may be a stupid question, but ...." If the person asking the question has taken the time to think about the problem before asking, the question is often not stupid at all. Uncertainty points out an ambiguity in specs, holes in the docs, or a search for how more experienced programmers might address a particular problem. From time to time, we will publish one of the "(Not So) Stupid Questions" we receive and invite our readers to answer the question in the feedback section.

Remember that new people are joining the Java community all the time and may be looking for help from those with more experience. Also, those who began with Java as their first language can benefit from those coming to the community with experience in other languages. As always, answer the questions with kindness. You are also welcome to submit your questions to

This may be a stupid question, but ... "How can you justify Dimension java.awt.Component.getMinimumSize() when Dimension does not implement Comparable<Dimension>? "

First thoughts:

Surely if Dimension is not Comparable then a minimum Dimension is undefined.

If you put a one-line JTextArea inside of a JScrollPane, then for the scroll pane, getMinimumSize().height is greater than getPreferredSize().height.

Thus, the following code:

[prettify]import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class JScrollPaneSize {
    public static void main(String[] args) {
        JTextArea notes = new JTextArea(0, 20);
        JScrollPane notesPane =
            new JScrollPane(notes,
                // either of the next two values exhibit
                // the 'odd' behaviour
                // JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.VERTICAL_SCROLLBAR_NEVER,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        notes.setText("only 1 line");
        System.out.format("JScrollPane preferred size=%s%n
            minimum size=%s%n",
            notesPane.getPreferredSize(),
            notesPane.getMinimumSize());
    }
}
[/prettify]

produces the following output:

JScrollPane preferred size=java.awt.Dimension[width=244,height=20]
            minimum size=java.awt.Dimension[width=8,height=23]

As you can see, the preferred height is less than the minimum height. And that leaves me wondering:

  • What really is the meaning of a Component's maximum, minimum, and preferred sizes?
  • What are you supposed to do with the returned Dimension, given that it doesn't implement Comparable?

"(Not So) Stupid Questions" is where we feature the questions you want to ask, but aren't sure how.

Related Topics >> GUI   |   Programming   |