Question

How do you use GridBagLayout and CardLayout together?

I'm trying to add these buttons to a panel, but it breaks the GridBagLayout, here's what is in the doc;

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

I'm trying to do:

        JPanel m = new JPanel();
 
    button = new JButton("Button 1");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 0;
    m.add(button, c);
 
    button = new JButton("Button 2");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 0;
    m.add(button, c);
    
    
    windows = new JPanel(new CardLayout());
        windows.add(m, "test");
        
    pane.add(windows, c);

However it reverts as though it's using flowlayout.

What is it I'm missing? Any help is much appreciated.

Full

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public final class GUI extends JPanel implements ActionListener {
    
    // buttons
    private JButton newButton, findButton;
    JPanel panels;

    public void addComponentToPane (Container pane) {
        pane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        
        c.fill = GridBagConstraints.HORIZONTAL;
        
        JPanel mainMenu = new JPanel();

        // add
            mainMenu.add(newButton = new JButton("Add"), c);
                newButton.setActionCommand("new");
                newButton.addActionListener(this);
        
        // find
            mainMenu.add(findButton = new JButton("Find"), c);
                findButton.setActionCommand("find");  
                findButton.addActionListener(this);

        panels = new JPanel(new CardLayout());
            panels.add(mainMenu, "mainWindow");
            
        pane.add(panels, c);
    }

    public void actionPerformed(ActionEvent e) {
        
        CardLayout allpanels = (CardLayout)(panels.getLayout());
        
        switch (e.getActionCommand()) {
            
            case "new":
                allpanels.show(panels, "add");
            break;
            
            case "find":
                System.out.println("find");
            break;
        }
    }

    private static void createGUI () {

        JFrame frame = new JFrame("Check");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GUI contentPane = new GUI();
        contentPane.addComponentToPane(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        createGUI(); 
    }
}
Was it helpful?

Solution

Your code is quite tangled but the main problems are:

  • You're adding Buttons to a JPanel with a GridBagConstraints instance as the 'constraints' arguments, but you haven't set the GridBagLayout on this panel
  • You're trying to switch to a card in the CardLayout-panel that you haven't added ("add")
  • You say that it looks like a FlowLayout but you haven't set any GridBagConstraints that make it look different than that.

I've refactored your code a bit and fixed these problems mentioned above. If you can provide more detail about the goals you're trying to achieve we may be able to give a better answer.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public final class GUI extends JPanel implements ActionListener {

    // buttons
    private JButton newButton, findButton;
    JPanel panels;

    public void addComponentToPane(Container pane) {
        pane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.HORIZONTAL;

        createPanels();

        pane.add(panels, c);
    }

    private void createPanels() {
        JPanel mainMenu = createMainMenu();

        panels = new JPanel(new CardLayout());
        panels.add(mainMenu, "mainWindow");
        panels.add(new JLabel("Add card"), "add");
        panels.add(new JLabel("Find card"), "find");
    }

    private JPanel createMainMenu() {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.HORIZONTAL;

        JPanel mainMenu = new JPanel(new GridBagLayout());

        // add
        mainMenu.add(newButton = new JButton("Add"), c);
        newButton.setActionCommand("new");
        newButton.addActionListener(this);

        // find
        mainMenu.add(findButton = new JButton("Find"), c);
        findButton.setActionCommand("find");
        findButton.addActionListener(this);
        return mainMenu;
    }

    public void actionPerformed(ActionEvent e) {
        CardLayout allpanels = (CardLayout) (panels.getLayout());

        switch (e.getActionCommand()) {
        case "new":
            allpanels.show(panels, "add");
            break;

        case "find":
            allpanels.show(panels, "find");
            break;
        }
    }

    private static void createGUI() {

        JFrame frame = new JFrame("Check");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GUI contentPane = new GUI();
        contentPane.addComponentToPane(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        createGUI();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top