Monday, July 21, 2008

Swing and a miss

Does this method Javadoc from the Swing API confuse you? It confuses me:
/**
* Sets the the vertical alignment.
*
* @param alignmentX the new vertical alignment
* @see #getAlignmentX
* @beaninfo
* description: The preferred horizontal alignment of the component.
*/
public void setAlignmentX(float alignmentX) {


As you can see, the method description and param seem to conflict directly with the beaninfo description. I was sure the first two are a typo, copied from the alignmentY equivalent, but let's have a look at setAlignmentY:
/**
* Sets the the horizontal alignment.
*
* @param alignmentY the new horizontal alignment
* @see #getAlignmentY
* @beaninfo
* description: The preferred vertical alignment of the component.
*/
public void setAlignmentY(float alignmentY) {


???

Also, using it is confusing and the docs don't seem very helpful. I created a JPanel using a vertical BoxLayout, so that as you .add(component)s they stack up beneath each other.
To the empty box panel I added a JLabel with the text "Log" and beneath that, a JTextArea which would hold the log output.

However, I noticed on running the program that the label was not left-aligned - instead, it was indented by about 40 pixels beyond the log textarea beneath. Worse still, when text appeared inside the JTextArea, the label above would jump horizontally right, bizarrely. So the most logical thing to do seemed to be to call label.setAlignmentX(Component.LEFT_ALIGNMENT) to keep the label aligned to the left side of its containing box.
Seemed to be the logical thing to do. But no, this caused no obvious change. Various combinations of this did nothing, until I made the same call on the textarea underneath.

So if you have a vertical box containing a label with a textarea underneath it and you want the LABEL to be left-aligned, you have to tell the TEXTAREA to left-align itself.

...WHAT?

No comments:

Post a Comment