Question

I'm creting a Java app using SWING for the UI. My choise for the LayoutManager goes to a GroupLayout, but now I got a problem with a Component.

enter image description here

The basic layout is made by three columns and two rows (there are nested rows, as you can see in the code below), and the second row contains only a JPane with a list of JCheckBoxes.

My problem is that I want to insert that JPanel in a way that it spans across columns, without affecting other columns size (i.e. the Canvas must be squared and not rectangular).

Is it possible or I must change LayoutManager?

Here's the code:

    //create and set LayoutManager
    GroupLayout gp = new GroupLayout(this.getContentPane());
    gp.setAutoCreateContainerGaps(true);
    gp.setAutoCreateGaps(true);
    this.setLayout(gp);
    //set alignment criteria
    GroupLayout.Alignment hAlign = GroupLayout.Alignment.TRAILING;
    GroupLayout.Alignment vAlign = GroupLayout.Alignment.BASELINE;

    //add component into layout
    //set horizontal group
    gp.setHorizontalGroup(gp.createSequentialGroup()
            .addGroup(gp.createParallelGroup(hAlign)
                    .addComponent(imageCanvas)
                    .addComponent(densitiesPanel))
            .addGroup(gp.createParallelGroup(hAlign)
                    .addComponent(projectPathField)
                    .addComponent(sourceDensityLabel)
                    .addComponent(sourceSizeLabel))
            .addGroup(gp.createParallelGroup(hAlign)
                    .addComponent(projectPathButton)
                    .addComponent(sourceDensityComboBox)
                    .addComponent(sourceSizeTextField))
                    );

    //set vertical group
    gp.setVerticalGroup(gp.createSequentialGroup()
            .addGroup(gp.createParallelGroup(vAlign)
                    .addComponent(imageCanvas)
            .addGroup(gp.createSequentialGroup()
                    .addGroup(gp.createParallelGroup(vAlign)
                            .addComponent(projectPathField)
                            .addComponent(projectPathButton))
                    .addGroup(gp.createParallelGroup(vAlign)
                            .addComponent(sourceDensityLabel)
                            .addComponent(sourceDensityComboBox))
                    .addGroup(gp.createParallelGroup(vAlign)
                            .addComponent(sourceSizeLabel)
                            .addComponent(sourceSizeTextField)))
                    )
                    .addComponent(densitiesPanel)
            );
Was it helpful?

Solution

Solved by myself adding a second JPanel: I called this one mainPanel and gave it the GroupLayout of the code above; then set the TopContainer with a BoxLayout and added the two panes separately, this is the result:

enter image description here

OTHER TIPS

I share with you an example of GroupLayout. I hope you love it :)

enter image description here

import static javax.swing.GroupLayout.Alignment.BASELINE;
import static javax.swing.GroupLayout.Alignment.CENTER;
import static javax.swing.GroupLayout.Alignment.LEADING;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

public class MyForm extends JFrame {

    public MyForm() {

        JLabel jlblTitle = new JLabel("Form Builder");

        JLabel jlblUsername = new JLabel("Username:");
        JLabel jlblGroup = new JLabel("Group:");
        JLabel jlblAddress = new JLabel("Address:");

        JComboBox jcmbxUsername = new JComboBox();
        JComboBox jcmbxGroup = new JComboBox();
        JComboBox jcmbxAddress = new JComboBox();

        JButton jbtnUsername = new JButton("New Username");
        JButton jbtnGroup = new JButton("New Group");
        JButton jbtnAddress = new JButton("New Address");

        JButton jbtnCreate = new JButton("Create");

        JPanel jpanUser = new JPanel();

        GroupLayout layout = new GroupLayout(jpanUser);

        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(CENTER)
                        .addComponent(jlblTitle)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(LEADING)
                                .addComponent(jlblUsername)
                                .addComponent(jlblGroup)
                                .addComponent(jlblAddress))
                        .addGroup(
                            layout.createParallelGroup(LEADING)
                                .addComponent(jcmbxUsername)
                                .addComponent(jcmbxGroup)
                                .addComponent(jcmbxAddress)
                                .addComponent(jbtnCreate, Alignment.CENTER))
                        .addGroup(
                            layout.createParallelGroup(LEADING)
                                .addComponent(jbtnUsername)
                                .addComponent(jbtnGroup)
                                .addComponent(jbtnAddress)))));

        layout.linkSize(SwingConstants.HORIZONTAL, jbtnUsername, jbtnGroup,
                jbtnAddress);

        layout.setVerticalGroup(layout
                .createSequentialGroup()
                .addGroup(
                        layout.createParallelGroup(BASELINE)
                        .addComponent(jlblTitle))
                .addGroup(
                        layout.createParallelGroup(LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(
                                    layout.createParallelGroup(BASELINE)
                                    .addComponent(jlblUsername)
                                    .addComponent(jcmbxUsername)
                                    .addComponent(jbtnUsername))
                            .addGroup(
                                    layout.createParallelGroup(BASELINE)
                                    .addComponent(jlblGroup)
                                    .addComponent(jcmbxGroup)
                                    .addComponent(jbtnGroup))
                            .addGroup(
                                    layout.createParallelGroup(BASELINE)
                                    .addComponent(jlblAddress)
                                    .addComponent(jcmbxAddress)
                                    .addComponent(jbtnAddress))
                            .addComponent(jbtnCreate))));

        jpanUser.setLayout(layout);

        BoxLayout layoutMain = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
        getContentPane().setLayout(layoutMain);

        add(jpanUser);

        setTitle("My form");
        pack();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager
                            .setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                    // "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                    // UIManager.getCrossPlatformLookAndFeelClassName());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                new MyForm().setVisible(true);
            }
        });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top