Search code examples
javaswingjbuttonlayout-manager

Can you bound a JComponent by the center instead of the upper left?


I'm trying to position some JButton objects, but I need the coordinates to position them in terms of the center of the button, instead of the upper left hand corner as default, is there a way to do this?

        //Button L1 (Left 1)
   buttonL1 = new JButton( "Button L1" ); 
   buttonL1.setBounds( 150, 140, (int) rWidth, (int) rHeight );
   c.add( buttonL1);

   //Button L2 (Left 2)
   buttonL2 = new JButton( "Button L2" ); 
   buttonL2.setBounds( 150, 340, (int) rWidth, (int) rHeight); 
   c.add( buttonL2 );

   //Button R3 (Right 3)
   buttonR3 = new JButton( "Button R3" ); 
   buttonR3.setBounds( 580, 140, (int) rWidth, (int) rHeight); 
   c.add( buttonR3 );

   //Button R4 (Right 4)
   buttonR4 = new JButton( "Button R4" ); 
   buttonR4.setBounds( 580, 340, 20, 20 ); 
   c.add( buttonR4 );

For example, the bottom right button is positioned in terms of the top left hand corner, I need the coordinates I've given all my buttons to be the centre of where they get positioned


Solution

  • This layout can be achieved using a series of containers with GridBagLayout to center the buttons – with each of those containers going inside a GridLayout to align the buttons into columns and rows.

    enter image description here enter image description here enter image description here

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    public class CenteredButtons {
    
        private JComponent ui = null;
    
        CenteredButtons() {
            initUI();
        }
    
        public void initUI() {
            if (ui!=null) return;
    
            // adjust numbers to need for minimum size/padding etc.
            ui = new JPanel(new GridLayout(0,2,40,10));
            ui.setBorder(new EmptyBorder(30,30,30,30));
            Insets margin = new Insets(10, 15, 10, 15);
    
            for (int i=1; i<5; i++) {
                JPanel p = new JPanel(new GridBagLayout());
                JButton btn = new JButton("Button " + i);
                btn.setMargin(margin);
                p.add(btn);
                ui.add(p);
            }
        }
    
        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) {
                    }
                    CenteredButtons o = new CenteredButtons();
    
                    JFrame f = new JFrame("Centered Buttons");
                    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);
        }
    }