At the moment I have the following code which works fine.
label = new JLabel(panelLabel,SwingConstants.CENTER);
outputPanel.add(label,BorderLayout.CENTER);
I get the text in the center of the panel (in terms of the left-right position as well as in terms of the top-bottom).
Now I want to set the position to the bottom (and center in terms of “left-right”). I tried to use SOUTH instead of the CENTER in the first line. Compiler does not complains but during the execution i get IllegalArgumentException: HorizontalAlignment. What is that?
The JLabel constructor you are using is
JLabel(String label, int horizontalAlignement).As defined in the javadoc, this int should be only in certain values:
This value sets where your text is aligned, inside the JLabel. You can’t use “SOUTH” here, and this is why you get the “InvalidArgument” exception.
If you want to make your text on the bottom of the label, you have to use setVerticalAlignment(int), with
Swingconstants.BOTTOM. That supposes that your label is centered and will potentially take a wide height on your panel.If you use a BorderLayout for this panel, you might want to try to add your label with
BorderLayout.SOUTH, as hint for the layout manager. This will stick the label to the bottom part, and it will have a minimal height (the one necessary to display the text). In this case, the vertical alignment of the text doesn’t really matter.