Search code examples
javaswingjtablejbuttongridbaglayout

centering jButton horizontally in relation to JTable with GridBagLayout


I hava a JTable and I have placed 3 JButtons below it. I've manage to put the left button on the left corner and the right button on the right corner but I can't figure out how to place the middle button in the center. I have tried using .weightx and .anchor without any results.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class AccessView extends JPanel{
  String[] columnNames = {"Date","Time","Type of access"};
  DefaultTableModel tableModel;
  JTable tAccesses;
  JScrollPane scrollPane;
  JButton bAdd, bMod, bDel;

  public AccessView(){
    createGUI();
  }

   public void createGUI(){
    this.setBorder(BorderFactory.createTitledBorder("Accesses"));
    this.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    bAdd = new JButton("Add");
    bMod = new JButton("Mod");
    bDel = new JButton("Del");

    tableModel = new DefaultTableModel(25,columnNames.length);
    tableModel.setColumnIdentifiers(columnNames);   
    tAccesses = new JTable(tableModel);
    scrollPane = new JScrollPane(tAccesses);

    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 3;
    this.add(scrollPane, c);

    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    c.anchor = GridBagConstraints.LAST_LINE_START;
    this.add(bAdd, c);

    c.gridx = 1;
    c.gridy = 1;
    this.add(bMod, c);

    c.gridx = 2;
    c.gridy = 1;
    c.anchor = GridBagConstraints.LAST_LINE_END;
    this.add(bDel, c);
  }  
}

Solution

  • As suggested in the comments above, you can use another JPanel for the 3 JButtons.

    Using BoxLayout for JPanel which will contain the 3 JButtons and Box.createHorizontalGlue() between each JButton you can get the following output:

    enter image description here

    In this case:

    1. Create a new JPanel

      JPanel buttonsPane;
      
    2. Initialize it and set its layout to BoxLayout

      buttonsPane = new JPanel();
      buttonsPane.setLayout(new BoxLayout(buttonsPane, BoxLayout.LINE_AXIS));
      
    3. Add the buttons along with Box.createHorizontalGlue() between them:

      buttonsPane.add(bAdd);
      buttonsPane.add(Box.createHorizontalGlue());
      buttonsPane.add(bMod);
      buttonsPane.add(Box.createHorizontalGlue());
      buttonsPane.add(bDel);
      
    4. Add the buttonsPane to your JFrame's SOUTH alignment (or add it along with your current JPanel and edit its constraints (I used the former)):

      frame.add(this);
      frame.add(buttonsPane, BorderLayout.SOUTH);
      

    Another way you could achieve a similar result (as per @MadProgrammer comment below) would be changing your constraints and adding "empty" components between your JButtons:

    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    c.weightx = 1;
    this.add(bAdd, c);
    
    c.gridx = 1;
    this.add(new JLabel(""), c); //Empty component just for the extra space
    
    c.gridx = 2;
    this.add(bMod, c);
    
    c.gridx = 3;
    this.add(new JLabel(""), c); //Empty component just for the extra space
    
    c.gridx = 4;
    this.add(bDel, c);
    

    Which produces:

    enter image description here