title: Java GridBagLayout Aligning buttons
body: <p>I have a problem with GridbagLayout; I've 5 buttons and I want to have them in this way:
  <img src="https://i.stack.imgur.com/5EWcH.png" alt="Target ButtonDisposition"></p>
  
  <p>I've already tried different approaches but no one works in the correct way.</p>
  
  <p>For example:</p>
  
  <pre><code>import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  
  import javax.swing.Box;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.SwingUtilities;
  import javax.swing.UIManager;
  import javax.swing.UnsupportedLookAndFeelException;
  
  public class TestGridBagLayout {
  
      protected void initUI() {
          JFrame frame = new JFrame("Test");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          JPanel southPanel = new JPanel(new GridBagLayout());
          GridBagConstraints gbc = new GridBagConstraints();
  
          gbc.gridwidth = 2;
          gbc.gridy = 0;
          JButton enterRoom = new JButton("Enter room");
          JButton exitRoom = new JButton("Exit room");
          JButton login = new JButton("Login");
          JButton logout = new JButton("Logout");
          JButton whoIsIn = new JButton("Who is in");
  
          gbc.gridx = 1;
          southPanel.add(enterRoom, gbc);
  
          gbc.gridx = 5;
          southPanel.add(exitRoom, gbc);
  
          gbc.gridy = 1;
  
          gbc.gridx = 0;
          southPanel.add(login, gbc);
  
          gbc.gridx = 3;
          southPanel.add(logout, gbc);
  
          gbc.gridx = 6;
          southPanel.add(whoIsIn, gbc);
  
          frame.add(southPanel);
          frame.pack();
          frame.setVisible(true);
      }
  
      public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
              UnsupportedLookAndFeelException {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          SwingUtilities.invokeLater(new Runnable() {
  
              @Override
              public void run() {
                  new TestGridBagLayout().initUI();
              }
          });
      }
  }
  </code></pre>
  
  <p>Appears:
  <img src="https://i.stack.imgur.com/F0wEl.png" alt="wrong buttons aligning">
  I'm not interested in other approaches (such as GridLayout), I'd like to know what I'm missing.</p>