Question

I'm trying to move my buttons and label. I want the buttons down in the bottom right corner and the label right above them, but no matter how I change the layout system, they're all stuck on the same line and won't move at all. How to get them to move?

        setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();

        gc.anchor = GridBagConstraints.CENTER;
        gc.weighty = 1;
        gc.gridx = 0;
        gc.gridy = 0;
        gc.ipadx = 5;
        pane.add(explain_lbl, gc);
        gc.ipadx = 1;
        gc.gridy = 1;
        gc.anchor = GridBagConstraints.PAGE_END; //bottom of space
        pane.add(btn_play_normal, gc);
        gc.ipadx = 6;
        gc.gridy = 1;;
        gc.anchor = GridBagConstraints.PAGE_END; //bottom of space

        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.ipady = 0;       //reset to default
        gc.weighty = 1.0;   //request any extra vertical space
        gc.anchor = GridBagConstraints.PAGE_END; //bottom of space
        gc.insets = new Insets(10,0,0,0);  //top padding
        gc.gridx = 1;       //aligned with button 2
        gc.gridwidth = 2;   //2 columns wide
        gc.gridy = 2;       //third row
        pane.add(btn_play_ptr, gc);
Was it helpful?

Solution

You are not setting the layout of pane the container that is accepting your GridBag components. You need to set its layout to GridBagLayout for the layout to work.

Specifically:

setLayout(new GridBagLayout());  // (A)

//  ....

pane.add(btn_play_normal, gc);  // (B)

// ...

pane.add(btn_play_ptr, gc);    // (C)

You're adding your components to pane using GridBagConstraints, gc, in (B) and (C), but you appear to be setting the layout of the this container (whatever it is) in (A), but not that of the pane container.

A solution: set pane's layout:

pane.setLayout(new GridBagLayout());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top