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:
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());
}
}
What does Comparable mean for a Dimension anyway?
2005-11-27 17:05:31 rickcarson
[Reply | View]
The point of the Comparable interface is to allow sorting. Component X is before Component Y.
What what does 'before' mean in this context? The original question seems to hint that they think 'before' should mean 'smaller than'. So we would be comparing areas.
I was then going to talk about the possibility of using 'leftmost' or 'topmost' as equally valid alternatives. When I look at Dimension though it seems that if it does actually describe an area (which it doesn't really - instead it defines a point on a Cartesian plane), then the 'missing info' would be that implicitly it was measuring from 0.0 to the diagonally opposite corner.
So then you have a member describing 'size' which mathematically speaking has no area at all (points don't have area)!
But wait! It gets even better! Because Dimension has three constructors - the no args one (for all your beany goodness), a copy constructor (which takes another dimension) and one which takes width and height - as ints.
And when I ask for the width or height back... it gives them to me as a double! Magic!
So even if it had individual setters for width and height (which it doesn't) it would fail to be a bean since it would be either reading or writing the wrong data type...
Anyway, my feeling is this - if you were going to change the Dimension, then <Comparable> would be a rather strange thing to try to add to it (but see also R-Trees I think it is which sort in two dimesnions). I would much rather see Dimension 'properly beanified'....
eg: add a constructor which takes doubles*, and add setters for width and height which also take doubles.
*Not required for beaning I know, but fulfills my sense of 'completeness'.
No need to compare Dimension
2005-11-27 11:03:36 rivasdiaz
[Reply | View]
What are you supposed to do with the returned Dimension, given that it doesn't implement Comparable?
You don't need to compare two dimensions because the layout is done as two diferent operations, one for the X axis and another for the Y axis
This is Swing not Algebra
2005-11-25 23:08:15 vtec
[Reply | View]
Minimum, Prefered and Maximum sizes are just hints.
You can configure your components with whathever values of min, pref and max size you want.
Your actual container widget and its size and layout decides whether to use these values, which of them, how to you them or whather to ignore them completely.
Each Swing Layout Manager IS DIFFERENT in a way how it handles min / pref /max size.
Just compare SpringLayout and BoxLayout.....
Text component layout
2005-11-24 08:57:43 tackline
[Reply | View]
Yes. It is "minimum size" in a rather awkward sense. Not that minimum size is often useful.
The preferred size of text components can also change depending upon its current size, IIRC. The AWT layout system isn't really up to laying out flowing text. What tends to happen is that the layout is calculated and displayed, then as widths change it all happens a second time shortly after. That layout seems to turn out alright in the end, it just doesn't look very professional as it shudders. If you write your own text-like components, calling revalidate when width changes effect the preferred height should give the same effect.