0

Here is how i add elements in layout

GroupLayout layout = new GroupLayout(panel);
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);
    panel.setLayout(layout);

    layout.setHorizontalGroup(
        layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING))
                .addComponent(socSecIconLabel)
                .addComponent(creditCardIconLabel)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING))
                .addComponent(socSecLabel)
                .addComponent(creditCardLabel)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))
                .addComponent(socSecCheck)
                .addComponent(creditCardCheck)
    );

    layout.setVerticalGroup(
        layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE))
                .addComponent(socSecIconLabel)
                .addComponent(socSecLabel)
                .addComponent(socSecCheck)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE))
                .addComponent(creditCardIconLabel)
                .addComponent(creditCardLabel)
                .addComponent(creditCardCheck)
    );

My goal is icon label::label::checkbox in each row, but with this code i get only mess:

not in the row

How to align elements in each row?

  • Don't use GroupLayout. It is probably the most complicated layout manager and is generally only used by IDE's for generated code. If you want components in a row you can use a FlowLayout, or a BoxLayout or a GridBagLayout. You can also nest panels with different layout manager to achieve your desired layout. Read the section from the Swing tutorial on [Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for more information and working examples. I would suggest the GridBagLayout is probably what you want in this case. – camickr Mar 13 '18 at 16:06
  • Here is what i want [enter link description here](https://stackoverflow.com/questions/16148958/building-gui-using-grouplayout-in-java) – Anton Chertash Mar 14 '18 at 10:28
  • As I suggested use a `GridBagLayout`. You have the tutorial link. Read the tutorial, download the demo code and make changes. If you still want to use the GroupLayout well, the tutorial also has a demo for that and you can download and change the demo code. – camickr Mar 14 '18 at 15:00

2 Answers2

0

Here is an example that uses GridLayout, two rows and four columns:

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

public class GridLayoutExample extends JFrame {

    public void addComponents(final Container pane) {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2,4)); //2 rows, 4 columns

        panel.add(new JLabel("Social security"));
        panel.add(new JButton("Click 1"));
        panel.add(new JCheckBox(""));
        panel.add(new JTextField());

        panel.add(new JLabel("Credit card"));
        panel.add(new JButton("Click 2"));
        panel.add(new JCheckBox(""));
        panel.add(new JTextField());

        pane.add(panel);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                GridLayoutExample frame = new GridLayoutExample();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.addComponents(frame.getContentPane());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

enter image description here

Edwin Torres
  • 2,774
  • 1
  • 13
  • 15
0

You have the right idea, but I think the problem is the extra ) at the end of each addGroup. Instead of

.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING))
            .addComponent(socSecIconLabel)

try

.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
            .addComponent(socSecIconLabel)

Your code is adding the components to the "generic" group instead of the specific parallel group that you created.

ogem
  • 1
  • 2