2

There are several different commands to explicitly align elements in Java Swing. It appears that these commands only work under some extremely specific constraints and these constraints are not documented anywhere. Most of the time that I want to align elements, these commands don't do anything at all. So I'm wondering why do these commands not do what the documentation says that they do, and also, how to align elements in Swing?

For reference, here is a SSCCE with an OK button that aligns to the left when we explicitly set horizontal alignment to center.

import javax.swing.*;
import java.awt.*;

public class E {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel notificationView = new JPanel();
    notificationView.setPreferredSize(new Dimension(300, 145));
    notificationView.setLayout(new GridBagLayout());
    JPanel content = new JPanel();
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

    JLabel notificationText = new JLabel("Here is some text to display to the user and below this is an ok button.");
    content.add(notificationText);
    JButton buttonOkNotification = new JButton("OK");
    //buttonOkNotification.setHorizontalAlignment(JLabel.CENTER); // does not do anything
    //buttonOkNotification.setAlignmentX(Component.CENTER_ALIGNMENT); // does not do anything
    content.add(buttonOkNotification);

    notificationView.add(content);
    frame.add(notificationView);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}
}
Atte Juvonen
  • 4,922
  • 7
  • 46
  • 89
  • Those component properties are irrelevant for layout, they are used only to hint the component how to use its area. They can be used to e.g. control text alignment in a JTextField. They dont change how the component is layouted within its container. For layout managers, see https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html – Durandal Jan 19 '17 at 13:16
  • 2
    1) *"For reference, here is a SSCCE.."* No, that's an uncompilable code snippet. Please carefully **read** the documents on [MCVE] & [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 3) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 4) Note that the methods being called on the button are to align the **content of the button**, not the button itself. – Andrew Thompson Jan 19 '17 at 13:22
  • 1) I did not include the class declaration to keep it minimal, but I edited it back into the post now. 2) I am not using any of the methods which your link is referencing. 3) Ok I will make a separate question about how to achieve a specific look for my specific inteded GUI. 4) That actually answers the generic question I posted here, thank you. – Atte Juvonen Jan 19 '17 at 13:38

1 Answers1

4
JButton buttonOkNotification = new JButton("OK");
//buttonOkNotification.setHorizontalAlignment(JLabel.CENTER); // does not do anything
//buttonOkNotification.setAlignmentX(Component.CENTER_ALIGNMENT); // does not do anything

Note that the methods being called on the button are to align the content of the button, not the button itself. These constraints usually only become relevant or noticeable if the component is stretched larger then its preferred size. That stretching is usually as a result of the layout and constraint of the container that it resides in. A good example is a BorderLayout:

  • To stretch a component horizontally, but not vertically, add it to the PAGE_START or PAGE_END.
  • To stretch a component vertically, but not horizontally, add it to the LINE_START or LINE_END.
  • To stretch a component both vertically & horizontally, add it to the CENTER.

Contrast that with a FlowLayout. The flow layout does not stretch components vertically or horizontally, instead always keeping them at their preferred size.


To align the button component within the panel, it is necessary to use constraints to the layout. That can be done on construction, e.g.:

new FlowLayout(FlowLayout.RIGHT); // aligns all child components to the right hand side

But is more commonly done when adding a component to a container. E.G. on adding a component to a GridBagLayout, it might look like this:

gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
panel.add(new JLabel("North West"), gridBagConstraints);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433