Search code examples
javaswingjlabellayout-manager

Put 4 JLabel at corners of a JFrame


As the title said, I'm trying to put 4 different JLabels at each of the corners of a JFrame. I want them to stay there forever even if I try to resize the JFrame

I've tried using a layout manager but I just can't get it right.

    ImageIcon icon;
    JLabel labelNW = new JLabel();
    JLabel labelNE = new JLabel();
    JLabel labelSW = new JLabel();
    JLabel labelSE = new JLabel();
    URL buttonURL = InputOutputTest.class.getResource("images/square_dot.gif");
    if(buttonURL != null){
        icon = new ImageIcon(buttonURL);
        labelNW.setIcon(icon);
        labelNE.setIcon(icon);
        labelSW.setIcon(icon);
        labelSE.setIcon(icon);
    }
    window.add(labelNW, BorderLayout.NORTH);
    //window.add(labelNE, BorderLayout.EAST);
    //window.add(labelSW, BorderLayout.WEST);
    window.add(labelSE, BorderLayout.SOUTH);

This code takes care of the north and south of the left side. I'm probably approaching this wrong though.

I also tried GridLayout (2,2) but they weren't at the corners and there's a huge gap on the right side.


Solution

  • You will want to nest JPanels each using its own layout. In fact you could do this by nesting JPanels that all use BorderLayout.

    Going to check if GridBagLayout can do it in one shot.... hang on...

    Yep GridBagLayout does it too:

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import javax.swing.*;
    
    public class GridBagExample {
       public static void main(String[] args) {
          JPanel mainPanel = new JPanel(new GridBagLayout());
    
          GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
                GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(
                      0, 0, 0, 0), 0, 0);
          mainPanel.add(new JLabel("Left Upper"), gbc);
    
          gbc.gridx = 1;
          gbc.gridy = 0;
          gbc.anchor = GridBagConstraints.NORTHEAST;
          mainPanel.add(new JLabel("Right Upper"), gbc);
    
          gbc.gridx = 0;
          gbc.gridy = 1;
          gbc.anchor = GridBagConstraints.SOUTHWEST;
          mainPanel.add(new JLabel("Left Lower"), gbc);
    
          gbc.gridx = 1;
          gbc.gridy = 1;
          gbc.anchor = GridBagConstraints.SOUTHEAST;
          mainPanel.add(new JLabel("Right Lower"), gbc);
    
          JFrame frame = new JFrame("Test");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.setLocationRelativeTo(null);
          frame.pack();
          frame.setVisible(true);
       }
    }
    

    Edit
    Now for the BorderLayout example:

    import java.awt.BorderLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import javax.swing.*;
    
    public class BorderLayoutExample {
       public static void main(String[] args) {
          JPanel northPanel = new JPanel(new BorderLayout());      
          northPanel.add(new JLabel("North East"), BorderLayout.EAST);
          northPanel.add(new JLabel("North West"), BorderLayout.WEST);
    
          JPanel southPanel = new JPanel(new BorderLayout());
          southPanel.add(new JLabel("South East"), BorderLayout.EAST);
          southPanel.add(new JLabel("South West"), BorderLayout.WEST);
    
    
          JPanel mainPanel = new JPanel(new BorderLayout());
          mainPanel.add(northPanel, BorderLayout.NORTH);
          mainPanel.add(southPanel, BorderLayout.SOUTH);
    
          JFrame frame = new JFrame("BorderLayout Test");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    }