Search code examples
javaswinglayout-managergrouplayout

GroupLayout and component size span


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)
            );

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