dwwilson66
dwwilson66

Reputation: 7066

GridBagLayout is not displaying as expected

This is my first foray into GridBagLayout, have reviewed the Java documentation online (as well as a lot of SO Q&As) and I've creating a panel that I'm expecting to display as such:

+--------------------------+
|      choose a timer      |
+--------+--------+--------+
|  5min  |  25min | 30min  |
+--------+--------+--------+
|         00:00:00         |
+--------+--------+--------+
|  Start |  Pause |  Quit  |
+--------+--------+--------+

and here's the GridBagLayout I'm using.

public class ButtonSel extends JFrame implements ActionListener {
    JLabel buttonSelLabel = new JLabel("Choose Which Timer To Run");
    JButton pomoButton    = new JButton("00:25:00");
    JButton shrtButton    = new JButton("00:05:00");
    JButton longButton    = new JButton("00:30:00");
    JButton startButton   = new JButton("Go");
    JButton pauseButton   = new JButton("Pause");
    JButton quitButton    = new JButton("Quit");
    JLabel textDisplay    = new JLabel("00:00:00");
     //
    JPanel timerPanel     = new JPanel();
     //

public ButtonSel() {
    super("ButtonTest");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

            add(buttonSelLabel, c);    // line 1
             c.fill = GridBagConstraints.HORIZONTAL;
             c.gridx = 1;
             c.gridy = 1;
             c.gridwidth = 3;

            add(pomoButton, c);        // line 2
             c.gridx = 1;
             c.gridy = 2;
             c.gridwidth = 1;

            add(shrtButton, c);
             c.gridx = 2;
             c.gridy = 2;
             c.gridwidth = 1;

            add(longButton, c);
             c.gridx = 3;
             c.gridy = 2;
             c.gridwidth = 1;

            add(textDisplay, c);       // line 3
             c.fill = GridBagConstraints.HORIZONTAL;
             c.gridx=1;
             c.gridy=3;
             c.gridwidth=3;

            add(startButton, c);       // line 4
             c.gridx = 1;
             c.gridy = 4;
             c.gridwidth = 1;

            add(pauseButton, c);
             c.gridx = 2;
             c.gridy = 4;
             c.gridwidth = 1;

            add(quitButton, c);
             c.gridx = 2;
             c.gridy = 4;
             c.gridwidth = 1;

    pomoButton.addActionListener(this);
    shrtButton.addActionListener(this);
    longButton.addActionListener(this);
    startButton.addActionListener(this);
    pauseButton.addActionListener(this);
    quitButton.addActionListener(this);
}
public void actionPerformed(ActionEvent radioSelect) {
    Object source = radioSelect.getSource();
    if (source == pomoButton)
        textDisplay.setText("00:25:00");
    else
    if (source == shrtButton)
        textDisplay.setText("00:05:00");
    else
    if (source == longButton)
        textDisplay.setText("00:30:00");
    else
    if (source == startButton)
        textDisplay.setText("Started");
    else
    if (source == pauseButton)
        textDisplay.setText("Paused");
    else
    if (source == quitButton)
        textDisplay.setText("Quit");
    else
        textDisplay.setText("00:00:00");
}
}

I was getting this output,

enter image description here

and taking a cue about horizontal alignment from this question I added the HORIZONTAL constraints to the the fields. Now I'm getting:

enter image description here

I've used (0,0) and (1,1) as my starting (x,y) coordinates, interestingly enough, with the same results.

Can anyone see something that I'm missing?

Upvotes: 2

Views: 176

Answers (2)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

I had to add a few lines to make your code really runnable.

You were setting the GridBagConstraints after you add them to the component. You must set them before the add.

The trick was to set the setHorizontalAlignment method of the JLabels that you wanted to center to a center alignment.

I formatted your code and added some Insets to make your GUI more visually appealing.

Here's a picture of the GUI.

Button Test GUI

And here's the code.

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class ButtonSel extends JFrame implements ActionListener {
    JLabel  buttonSelLabel  = new JLabel("Choose Which Timer To Run");
    JButton pomoButton      = new JButton("00:25:00");
    JButton shrtButton      = new JButton("00:05:00");
    JButton longButton      = new JButton("00:30:00");
    JButton startButton     = new JButton("Go");
    JButton pauseButton     = new JButton("Pause");
    JButton quitButton      = new JButton("Quit");
    JLabel  textDisplay     = new JLabel("00:00:00");
    JPanel  timerPanel      = new JPanel();


    public ButtonSel() {
        super("ButtonTest");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        timerPanel = new JPanel();
        timerPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.CENTER;
        c.weightx = 1.0D;
        c.weighty = 1.0D;
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.insets = new Insets(10, 10, 0, 0);
        c.ipadx = 0;
        c.ipady = 0;
        buttonSelLabel.setHorizontalAlignment(SwingConstants.CENTER);
        timerPanel.add(buttonSelLabel, c); // line 1

        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 1;
        timerPanel.add(pomoButton, c); // line 2

        c.gridx = 2;
        c.gridy = 2;
        c.gridwidth = 1;
        timerPanel.add(shrtButton, c);

        c.gridx = 3;
        c.gridy = 2;
        c.gridwidth = 1;
        c.insets = new Insets(10, 10, 0, 10);
        timerPanel.add(longButton, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 3;
        c.gridwidth = 3;
        c.insets = new Insets(10, 10, 0, 0);
        textDisplay.setHorizontalAlignment(SwingConstants.CENTER);
        timerPanel.add(textDisplay, c); // line 3

        c.gridx = 1;
        c.gridy = 4;
        c.gridwidth = 1;
        c.insets = new Insets(10, 10, 10, 0);
        timerPanel.add(startButton, c); // line 4

        c.gridx = 2;
        c.gridy = 4;
        c.gridwidth = 1;
        timerPanel.add(pauseButton, c);

        c.gridx = 3;
        c.gridy = 4;
        c.gridwidth = 1;
        c.insets = new Insets(10, 10, 10, 10);
        timerPanel.add(quitButton, c);

        pomoButton.addActionListener(this);
        shrtButton.addActionListener(this);
        longButton.addActionListener(this);
        startButton.addActionListener(this);
        pauseButton.addActionListener(this);
        quitButton.addActionListener(this);

        this.add(timerPanel);
        this.pack();
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent radioSelect) {
        Object source = radioSelect.getSource();
        if (source == pomoButton)
            textDisplay.setText("00:25:00");
        else if (source == shrtButton)
            textDisplay.setText("00:05:00");
        else if (source == longButton)
            textDisplay.setText("00:30:00");
        else if (source == startButton)
            textDisplay.setText("Started");
        else if (source == pauseButton)
            textDisplay.setText("Paused");
        else if (source == quitButton)
            textDisplay.setText("Quit");
        else
            textDisplay.setText("00:00:00");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ButtonSel();            
            }
        });
    }

}

Upvotes: 4

DRastislav
DRastislav

Reputation: 1882

I think one solution is BoxLayout(). U can divide your panel at 4 independent panels.

1st - choose the time - as one panel 2nd - 5, 25,30 min 3rd - 00:00:00 4th - Start Pause Quit

then prepare Main panel which add 4 panels and use BoxLayout() / layout of this panel is important: e.g.

JPanel p = new JPanel();
p.add(panel1);
p.add(panel2);
p.add(panel3);
p.add(panel4);
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); 

BoxLayout.Y_AXIS - panel will be layouted from top to bottom

Upvotes: 0

Related Questions