Search code examples
javaswinganchorgridbaglayout

GridBagLayout not anchoring button to PAGE_END


I followed a different answer from this site where the guy had the same problem, but my button is still not anchoring down to the bottom. Any ideas ?

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;

public class InstructionsPanel {



 JComponent InstructionsPanel() throws IOException {

        JComponent iPanel = new JLabel(new ImageIcon(ImageIO.read(new File("res/FinalBG.png"))));   

        iPanel.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        BufferedImage button1icon = ImageIO.read(new File("res/MainMenu.png"));
        JButton button1 = new JButton("",new ImageIcon(button1icon));

        gbc = new GridBagConstraints();
        iPanel.add(button1);
        gbc.weighty = 1.0;
        gbc.gridx = 0; 
        gbc.anchor = GridBagConstraints.PAGE_END;
        button1.setBorder(BorderFactory.createEmptyBorder());
        //button1.setContentAreaFilled(false);

        return iPanel;  

    }

}  

Any help would be greatly appreciated, thanks !


Solution

  • Use the GridBagConstraints gbc when adding the JButton and after they have been defined

    iPanel.add(button1, gbc);
    

    Read How to Use GridBagLayout