Centering JLabel using GroupLayout

112 views Asked by At

I want to group a couple check-boxes together and place some text above it (think of it like a section in a form where there's a description of what this part of the form entails and a series of check-boxes)

See attached picture for visual representation:

https://i.stack.imgur.com/iHELj.jpg

My goal is to center the word 'Miscellaneous' so it sits in the center rather than on the far left.

I've tried changing the horizontal alignment of the JLabel like so:

miscellaneous = new JLabel("Miscellaneous");
miscellaneous.setHorizontalAlignment(JLabel.CENTER);

But it hasn't seem to have helped. I have also tried this:

miscellaneous = new JLabel("Miscellaneous", SwingConstants.CENTER);

And again with no success.

Here's the full snippet of code that generates the above attached picture:

    miscellaneous = new JLabel("Miscellaneous");
    movement = new JCheckBox("Movement");
    consumables = new JCheckBox("Consumables");
    layoutM = new GroupLayout(miscGroup);
    miscGroup.setLayout(layoutM);

    layoutM.setAutoCreateGaps(true);
    layoutM.setAutoCreateContainerGaps(true);

    layoutM.setHorizontalGroup(layoutM.createSequentialGroup()
             .addGroup(layoutM.createParallelGroup(GroupLayout.Alignment.LEADING)
                 .addComponent(miscellaneous)
                 .addGroup(layoutM.createSequentialGroup()
                      .addGroup(layoutM.createParallelGroup(GroupLayout.Alignment.LEADING)
                              .addComponent(movement))
                      .addGroup(layoutM.createParallelGroup(GroupLayout.Alignment.LEADING)
                              .addComponent(consumables))))
            );

    layoutM.setVerticalGroup(layoutM.createSequentialGroup()
            .addGroup(layoutM.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(miscellaneous))
                .addGroup(layoutM.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layoutM.createSequentialGroup()
                                .addGroup(layoutM.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(movement)
                                        .addComponent(consumables))))
            );

Any help/hints would be appreciated, thanks.

0

There are 0 answers