Search code examples
javaswingbuttonlayout-managerboxlayout

Positioning buttons in a specific way in a JPanel


I'm trying to position six buttons in a specific way but currently the output is almost right but the buttons are not in the exact positions that I want them to be in (with all buttons touching adjacent buttons with no gaps). This is show below:

button pic

Could someone have a look at my code to see why the positioning isn't exact?

public void createDirectionButtonPanel() {

    JButton northButton = new JButton("north");
    JButton  southButton = new JButton("south");
    JButton westButton = new JButton("west");
    JButton eastButton = new JButton("east");
    JButton upButton = new JButton("up");
    JButton downButton = new JButton("down");

    JPanel directionButtonPanel = new JPanel();
    // directionButtonPanel.setOpaque(false);
    directionButtonPanel.setLayout(new BoxLayout(directionButtonPanel, BoxLayout.Y_AXIS));

    JPanel directionRow_1 = new JPanel();
    // directionRow_1.setOpaque(false);
    directionRow_1.setLayout(new BoxLayout(directionRow_1, BoxLayout.X_AXIS));
    directionRow_1.add(Box.createRigidArea(northButton.getPreferredSize()));
    directionRow_1.add(northButton);
    directionRow_1.add(Box.createRigidArea(northButton.getPreferredSize()));
    directionRow_1.add(upButton);

    JPanel directionRow_2 = new JPanel();
    // directionRow_2.setOpaque(false);
    directionRow_2.setLayout(new BoxLayout(directionRow_2, BoxLayout.X_AXIS));
    directionRow_2.add(westButton);
    directionRow_2.add(Box.createRigidArea(northButton.getPreferredSize()));
    directionRow_2.add(eastButton);
    directionRow_2.add(Box.createRigidArea(northButton.getPreferredSize()));

    JPanel directionRow_3 = new JPanel();
    // directionRow_3.setOpaque(false);
    directionRow_3.setLayout(new BoxLayout(directionRow_3, BoxLayout.X_AXIS));
    directionRow_3.add(Box.createRigidArea(northButton.getPreferredSize()));
    directionRow_3.add(southButton);
    directionRow_3.add(Box.createRigidArea(northButton.getPreferredSize()));
    directionRow_3.add(downButton);

    upButton.setMaximumSize(southButton.getPreferredSize());
    northButton.setMaximumSize(southButton.getPreferredSize());
    westButton.setMaximumSize(southButton.getPreferredSize());
    southButton.setMaximumSize(southButton.getPreferredSize());
    downButton.setMaximumSize(southButton.getPreferredSize());
    eastButton.setMaximumSize(southButton.getPreferredSize());

    directionButtonPanel.add(directionRow_1);
    directionButtonPanel.add(directionRow_2);
    directionButtonPanel.add(directionRow_3);

}

Solution

  • Try this. GridLayout seems appropriate, as JavaDoc says: Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size. Also saves you the trouble of having to arrange the rows yourself. Just add everything to your directionButtonPanel.

        JButton northButton = new JButton("north");
        JButton southButton = new JButton("south");
        JButton westButton = new JButton("west");
        JButton eastButton = new JButton("east");
        JButton upButton = new JButton("up");
        JButton downButton = new JButton("down");
    
        JPanel directionButtonPanel = new JPanel();
        directionButtonPanel.setLayout(new GridLayout(3,4));
    
        // row 1
        directionButtonPanel.add(new JPanel());
        directionButtonPanel.add(northButton);
        directionButtonPanel.add(new JPanel());
        directionButtonPanel.add(upButton);
    
        // row 2
        directionButtonPanel.add(westButton);
        directionButtonPanel.add(new JPanel());
        directionButtonPanel.add(eastButton);
        directionButtonPanel.add(new JPanel());
    
        // row 3
        directionButtonPanel.add(new JPanel());
        directionButtonPanel.add(southButton);
        directionButtonPanel.add(new JPanel());
        directionButtonPanel.add(downButton);