Recommended Java Swing layouts to use for two same-size tables plus buttons

Viewed 122

I'm experimenting with Swing, and I want to try to build a window that looks and behaves like the following: Drawing of desired layout:

enter image description here

Essentially, for anyone familiar with WinForms, it should be as if the left "table" were anchored top, left, and bottom, and the right table were anchored top, right, and bottom.

The first thing I tried was a FlowLayout, but the tables would not resize properly as the window was resized.

To create the two tables and central button between them, I experimented with BorderLayout, by putting the tables in EAST and WEST and the button in the CENTER region. That succeeded in having the two tables being appropriately sized and resized as the window is resized, however the button filled up the entire space between them, and the width changed as it was resized (I want it to stay constant).

Any ideas to get me started?

2 Answers

The following is a basic demo based on GridBagLayout:

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

public class SwingTestPane extends JPanel {

    public SwingTestPane() {

        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWeights = new double[]{1, 0.0, 1, 0.0};
        gridBagLayout.rowWeights = new double[]{1};
        setLayout(gridBagLayout);

        GridBagConstraints c1 = new GridBagConstraints();
        c1.fill = GridBagConstraints.BOTH;
        c1.gridx = 0;
        c1.gridy = 0;
        JPanel ltPane = new LeftTablePane();
        add(ltPane, c1);

        GridBagConstraints c2 = new GridBagConstraints();
        c2.fill = GridBagConstraints.VERTICAL;
        c2.gridx = 1;
        c2.gridy = 0;

        JPanel lbPane = new LeftButtonsPane();
        add(lbPane, c2);

        GridBagConstraints c3 = new GridBagConstraints();
        c3.fill = GridBagConstraints.BOTH;
        c3.gridx = 2;
        c3.gridy = 0;

        JPanel rtPane = new RightTablePane();
        add(rtPane, c3);

        GridBagConstraints c4 = new GridBagConstraints();
        c4.fill = GridBagConstraints.VERTICAL;
        c4.gridx = 3;
        c4.gridy = 0;

        JPanel rbPane = new RightButtonsPane();
        add(rbPane, c4);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.getContentPane().add(new SwingTestPane());
        frame.pack();
        frame.setVisible(true);
    }
}

class LeftTablePane extends JPanel {

    LeftTablePane() {
        setBackground(Color.CYAN);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 300);
    }
}

class LeftButtonsPane extends JPanel {

    LeftButtonsPane() {
        setBackground(Color.YELLOW);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(100, 300);
    }
}
    
class RightTablePane extends JPanel {

    RightTablePane() {
        setBackground(Color.CYAN);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 300);
    }
}

class RightButtonsPane extends JPanel {

    RightButtonsPane() {
        setBackground(Color.YELLOW);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(100, 300);
    }
}

The following is a basic demo based on BoxLayout:

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

public class SwingTestPane extends JPanel {

    public SwingTestPane() {

        BoxLayout boxLayout = new BoxLayout(this, BoxLayout.X_AXIS);
        setLayout(boxLayout);

        JPanel ltPane = new LeftTablePane();
        add(ltPane);

        JPanel lbPane = new LeftButtonsPane();
        add(lbPane);

        JPanel rtPane = new RightTablePane();
        add(rtPane);

        JPanel rbPane = new RightButtonsPane();
        add(rbPane);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.getContentPane().add(new SwingTestPane());
        frame.pack();
        frame.setVisible(true);
    }
}

class LeftTablePane extends JPanel {

    LeftTablePane() {
        setBackground(Color.CYAN);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 300);
    }
}

class LeftButtonsPane extends JPanel {

    LeftButtonsPane() {
        setBackground(Color.YELLOW);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(100, 300);
    }

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(getPreferredSize().width, 0);
    }

    @Override
    public Dimension getMaximumSize() {
        return new Dimension(getPreferredSize().width, Integer.MAX_VALUE);
    }
}


class RightTablePane extends JPanel {

    RightTablePane() {
        setBackground(Color.CYAN);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 300);
    }
}

class RightButtonsPane extends JPanel {

    RightButtonsPane() {
        setBackground(Color.YELLOW);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(100, 300);
    }

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(getPreferredSize().width, 0);
    }

    @Override
    public Dimension getMaximumSize() {
        return new Dimension(getPreferredSize().width, Integer.MAX_VALUE);
    }
}
Related