How to get this Java Layout?

148 views Asked by At

In an Java BorderLayout the NORTH part is normally like this:

enter image description here

So what I want to have is, that the NORTH and SOUTH part have the same width as the CENTER. But the EAST and WEST part should have the height of the CENTER. This means the corners should be empty. I don't want to use a GridLayout for this, because I don't want the NORTH to have the same height as the CENTER.

How do I get this layout?

2

There are 2 answers

2
Hovercraft Full Of Eels On

Simply nest two BorderLayout using JPanels. In the inner one, add your north and south panels. Place the inner one in the outer one's BorderLayout.CENTER position. Done.

enter image description here

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

public class SimpleLayout extends JPanel {
   public SimpleLayout() {
      JPanel innerPanel = new JPanel(new BorderLayout());
      innerPanel.add(createLabeledPanel("Center"), BorderLayout.CENTER);
      innerPanel.add(createLabeledPanel("North"), BorderLayout.PAGE_START);
      innerPanel.add(createLabeledPanel("South"), BorderLayout.PAGE_END);

      setLayout(new BorderLayout());
      add(innerPanel, BorderLayout.CENTER);
      add(createLabeledPanel("East"), BorderLayout.LINE_END);
      add(createLabeledPanel("West"), BorderLayout.LINE_START);
   }

   private JComponent createLabeledPanel(String text) {
      JLabel label = new JLabel(text, SwingConstants.CENTER);
      JPanel panel = new JPanel();
      panel.setBorder(BorderFactory.createLineBorder(Color.black));
      panel.add(label);
      return panel;
   }

   private static void createAndShowGui() {
      SimpleLayout mainPanel = new SimpleLayout();

      JFrame frame = new JFrame("SimpleLayout");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
0
Andrew Thompson On

As mentioned in a comment, we can get this 'empty corners' layout quite easily using GridBagLayout. The yellow parts are simply a (panel with a) label that has no text.

enter image description here enter image description here

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

public class EmptyCornerLayout {

    private JComponent ui = null;
    GridBagConstraints gbc = new GridBagConstraints();

    EmptyCornerLayout() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new GridBagLayout());
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        gbc.weightx = 0.5;
        gbc.weighty = 0.5;
        gbc.fill = GridBagConstraints.BOTH;

        // first row
        addComponentToLayout(0, 0, 0d, 0d, Color.YELLOW, "");
        addComponentToLayout(0, 1, 1d, 0d, Color.CYAN, "NORTH");
        addComponentToLayout(0, 2, 0d, 0d, Color.YELLOW, "");

        // 2nd row
        addComponentToLayout(1, 0, 0d, 1d, Color.CYAN, "WEST");
        addComponentToLayout(1, 1, 1d, 1d, Color.RED, "CENTER");
        addComponentToLayout(1, 2, 0d, 1d, Color.CYAN, "EAST");

        // last row
        addComponentToLayout(2, 0, 0d, 0d, Color.YELLOW, "");
        addComponentToLayout(2, 1, 1d, 0d, Color.CYAN, "SOUTH");
        addComponentToLayout(2, 2, 0d, 0d, Color.YELLOW, "");
    }

    private final void addComponentToLayout(
            int row, int col, 
            double weightx, double weighty,
            Color color, String text) {
        JPanel panel = new JPanel();
        panel.setBackground(color);
        panel.add(new JLabel(text));

        gbc.gridx = col;
        gbc.gridy = row;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        ui.add(panel, gbc);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                EmptyCornerLayout o = new EmptyCornerLayout();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}