Home > Software design >  How top-align side-by-side panels, Java swing
How top-align side-by-side panels, Java swing

Time:03-23

I want my dissimilar-height panels to be top-aligned within their containing panel.

I've tried BorderLayout with NORTH, I've tried panel.setAlignmentY(TOP_ALIGNMENT). No luck with any approach so far.

What I get (and don't want) What I want

(To post, I'm being asked to give more description. The remaining text (other than code) is my attempt to satisfy that requirement.) You can see the small panels containing the "label x" names, each stacked vertically. I definitely want vertical stacking of components within the panels, and I definitely will have different-height panels; don't ask me to make the panels the same height--it's my art choice that their bottoms will not line up.

    package view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GUI2 extends JFrame {
    private static final long serialVersionUID = 1L;


    public GUI2() {
        setTitle("Paddle Events");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 200);
        setLocation(100, 100);
        
        // nesting enables me to see the border of the panel
        getContentPane().add(getMainPanel());

        setVisible(true);
    }


    private Component getMainPanel() {
        JPanel panel = new JPanel();
        panel.add(getGreenPanel());
        panel.add(getDarkGrayPanel());
        return panel;
    }


    private JPanel getGreenPanel() {
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.green));
        panel.setPreferredSize(new Dimension(175, 100));
        
        panel.add(getWrapperA());
        panel.add(getWrapperB());
        panel.add(getWrapperC());

        return panel;
    }


    private JPanel getDarkGrayPanel() {
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.darkGray));
        panel.setPreferredSize(new Dimension(150, 70));
        
        panel.add(getWrapperA());
        panel.add(getWrapperB());
        panel.add(getWrapperC());

        return panel;
    }


    private Component getWrapperA() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBorder(BorderFactory.createLineBorder(Color.black));

        panel.add(new JLabel("label 1"));
        panel.add(new JLabel("label 2"));
        panel.add(new JLabel("label 3"));

        return panel;
    }


    private Component getWrapperB() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBorder(BorderFactory.createLineBorder(Color.blue));
        panel.setAlignmentY(TOP_ALIGNMENT); // pointless

        panel.add(new JLabel("label 4"));
        panel.add(new JLabel("label 5"));

        return panel;
    }


    private Component getWrapperC() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBorder(BorderFactory.createLineBorder(Color.red));

        panel.add(new JLabel("label 6"));

        return panel;
    }


    public static void main(String[] args) {
        new GUI2();
    }
}

CodePudding user response:

There might be a differnet way to achieve the same thing, but for me, use a GridBagLayout, see enter image description here

import java.awt.Color;
import java.awt.Component;
import static java.awt.Component.TOP_ALIGNMENT;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main extends JFrame {
    private static final long serialVersionUID = 1L;

    public Main() {
        setTitle("Paddle Events");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 200);
        setLocation(100, 100);

        // nesting enables me to see the border of the panel
        getContentPane().add(getMainPanel());

        setVisible(true);
    }

    private Component getMainPanel() {
        JPanel panel = new JPanel();
        panel.add(getGreenPanel());
        panel.add(getDarkGrayPanel());
        return panel;
    }

    private JPanel getGreenPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weighty = 1;
        gbc.anchor = gbc.PAGE_START;
        gbc.insets = new Insets(4, 2, 4, 2);
        panel.setBorder(BorderFactory.createLineBorder(Color.green));
        panel.setPreferredSize(new Dimension(175, 100));

        panel.add(getWrapperA(), gbc);
        panel.add(getWrapperB(), gbc);
        panel.add(getWrapperC(), gbc);

        return panel;
    }

    private JPanel getDarkGrayPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weighty = 1;
        gbc.anchor = gbc.PAGE_START;
        gbc.insets = new Insets(4, 2, 4, 2);
        panel.setBorder(BorderFactory.createLineBorder(Color.darkGray));
        panel.setPreferredSize(new Dimension(150, 70));

        panel.add(getWrapperA(), gbc);
        panel.add(getWrapperB(), gbc);
        panel.add(getWrapperC(), gbc);

        return panel;
    }

    private Component getWrapperA() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBorder(BorderFactory.createLineBorder(Color.black));

        panel.add(new JLabel("label 1"));
        panel.add(new JLabel("label 2"));
        panel.add(new JLabel("label 3"));

        return panel;
    }

    private Component getWrapperB() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBorder(BorderFactory.createLineBorder(Color.blue));
        panel.setAlignmentY(TOP_ALIGNMENT); // pointless

        panel.add(new JLabel("label 4"));
        panel.add(new JLabel("label 5"));

        return panel;
    }

    private Component getWrapperC() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBorder(BorderFactory.createLineBorder(Color.red));

        panel.add(new JLabel("label 6"));

        return panel;
    }

    public static void main(String[] args) {
        new Main();
    }
}
  • Related