Search code examples
javaswingx

GridBagLayout Stacking Buttons Horizontally rather than vertically, despite ascending gridy value


I'm trying to get these three buttons to stack vertically, and I'm using gridy to ascend the value each time, however the buttons are appearing side by side, as shown below:

Buttons side by side rather than stacked vertiacally

public class AdminAreaMainPanel extends JPanel {

    StandardButton outputXMLButton, setUpRaceWeekendButton, editRaces;

    public AdminAreaMainPanel(ActionListener parentListener) {

        super(new GridBagLayout());

        GridBagConstraints gc = new GridBagConstraints();

        gc.gridx = 0;
        gc.gridy = 0;
        gc.ipady = 5;
        gc.ipadx = 5;
        gc.anchor = GridBagConstraints.NORTH;
        outputXMLButton = new StandardButton("Output XML File", Color.white, Color.green, MyLapsCompanionPreferences.retrieveMainFont(),
                (LineBorder) BorderFactory.createLineBorder(MyLapsCompanionColorPreferences.retrieveAccentBackgroundColor(), 2), 200, 30, parentListener);
        add(outputXMLButton);

//        gc.anchor = GridBagConstraints.LAST_LINE_END;
        gc.gridy = 1;
        setUpRaceWeekendButton = new StandardButton("Set Up - Previous Timings", Color.white, Color.green, MyLapsCompanionPreferences.retrieveMainFont(),
                (LineBorder) BorderFactory.createLineBorder(MyLapsCompanionColorPreferences.retrieveAccentBackgroundColor(), 2), 200, 30, parentListener);
        setUpRaceWeekendButton.setToolTipText("Set up race weekend using the race timings and names from previously");
        add(setUpRaceWeekendButton);

        gc.gridy = 2;
        editRaces = new StandardButton("Edit Races", Color.white, Color.green, MyLapsCompanionPreferences.retrieveMainFont(),
                (LineBorder) BorderFactory.createLineBorder(MyLapsCompanionColorPreferences.retrieveAccentBackgroundColor(), 2), 200, 30, parentListener);
        add(editRaces);

    }

}

Here is my StandardButton Class I'm using:

        
    ActionListener listener;

    public StandardButton(String text,Color backgroundColor, Color foregroundColor, Font font, LineBorder border, Integer width, Integer height, ActionListener listener) {
        super(text);
        this.actionListener = listener;
        
        setBackground(backgroundColor);
        setForeground(foregroundColor);
        setFont(font);
        setBorder(border);
        setPreferredSize(new Dimension(width,height));
        addActionListener(listener);
    }
    
   
    public StandardButton(String text,Color color, Integer height, ActionListener listener) {
        super(text);
        setBackground(MyLapsCompanionColorPreferences.retrieveMainForegroundColor());
        setForeground(MyLapsCompanionColorPreferences.retrieveAccentForegroundColor());
        setFont(MyLapsCompanionPreferences.retrieveMainFont());
        setBorder(BorderFactory.createLineBorder(color,2));
        Dimension dim = new Dimension();
        dim.height = height;
        setPreferredSize(dim);
        addActionListener(listener);
    }
    
}

I've tried just about every anchor setting, but this one seems to be working on my other classes in my program, so I'm not sure what the issue is here :(. Please could someone help


Solution

  • You’re setting the properties gc but you’re not actually using gc anywhere.

    Change these:

    add(outputXMLButton);
    add(setUpRaceWeekendButton);
    add(editRaces);
    

    to this:

    add(outputXMLButton, gc);
    add(setUpRaceWeekendButton, gc);
    add(editRaces, gc);