How to make button expand Java

Go To


I`m trying to create a Java Applet, I was looking around on the Java website and I found an nice example which I edited.

In this example there are 3 buttons, One that will be like a header, one that will be like a footer and one that will be like the body. My problem is that, the middle (body) button will not expand fully.

If you run this code, you will see what I mean.

import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;

public class main {
    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container pane) {
        if (RIGHT_TO_LEFT) {

        JButton button;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    if (shouldFill) {
    //natural height, maximum width
    c.fill = GridBagConstraints.HORIZONTAL;

    button = new JButton("hi");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 0;
    c.ipady = 40;
    pane.add(button, c);

    button = new JButton("Hello, i will not expand");
    c.fill = GridBagConstraints.BOTH;
    c.weighty = 1.0;   //request any extra vertical space
    c.anchor = GridBagConstraints.CENTER; //bottom of space
    c.insets = new Insets(10,0,0,0);  //top padding
    c.gridx = 1;       //aligned with button 2
    c.gridy = 1;       //third row
    pane.add(button, c);

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

     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("main");

        //Set up the content pane.

        //Display the window.

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {

enter image description here

2012-04-04 21:00
by user1294188


Looks like it's not the middle button's fault, but the bottom button that is not expanding, if you set the bottom button as so c.fill = GridBagConstraints.BOTH; then it takes up the empty space below.

Do you want the middle to be bigger? You might want to change the weight of the bottom button to .5 c.weighty = .5; //request any extra vertical space
and see if that is what you are looking for?

If you want the middle to be much bigger then the top and bottom, you may need to make more grid spaces, so rather then 1,1,1, try 1,3,1 etc

2012-04-04 21:27
Ah nice, i first tried your answer but it resulted the same, so then I tried c.weighty =0; and it worked, thank - user1294188 2012-04-04 21:29
Yeah wasn't sure exactly how big/small you wanted it but thought it would be that bit which was the culprit, glad I could help - 2012-04-04 21:30


I was looking around on the Java website and I found an nice example which I edited.

So I take it you don't have to use a GridBagLayout?

Use a BorderLayout, make the expandable button at position BorderLayout.CENTER.

2012-04-04 21:30
by Hedja