Almato
Almato

Reputation: 53

Java Swing CardLayout: multiple cards seem to be displayed simultaneously

I am currently working on a short Java Swing project using Eclipse (Luna, Win 8.1). The aim is to display a frame that contains a menu. The menu has different pages (Main, Options, etc.), realized by JPanels with Buttons, Labels, etc. Those JPanels are organized by a CardLayout, in such a manner that calling the CardLayout.show(..)-method switches between different menu pages. When I stopped programming two days ago, everything was working perfectly. However, when I wanted to continue yesterday morning, my code seemed to completely ignore the CardLayout: Buttons situated in other cards would pop up through the first card's background when moving the mouse cursor over their positions. I tried to fix that (researching, using backups, etc.) for 17 hours straight. The problem still persists and I am pretty desperate by now =/. The following SSCCE shows the same behaviour (move mouse over bottom left corner to see the "Option" card's "Back" button pop up).

package sscce;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
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.JPanel;

public class MainFrame extends JFrame {
    private CardLayout mainWindowLayout;
    // mainPanel will be used as the MainFrame's main unit to manage Components
    private JPanel mainPanel;

    // Constructor
    public MainFrame() {
        setTitle("sscce");
        setPreferredSize(new Dimension(800, 600));
        setResizable(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());

        mainPanel = new JPanel();
        mainPanel.setPreferredSize(getSize());
        mainWindowLayout = new CardLayout();
        mainPanel.setLayout(mainWindowLayout);

        buildMainMenu();
        buildOptions();

        getContentPane().add(mainPanel);
        mainPanel.setVisible(true);
        // starting application it will "show" (container,
        // contentIdentifier(String))
        mainWindowLayout.show(mainPanel, "2");
        pack();
        setVisible(true);
    }// Constructor

    private void buildMainMenu() {
        JPanel panelMainMenu = new JPanel(new GridBagLayout());
        panelMainMenu.setBackground(Color.orange);

        // 1 Button (centered) to go to Options-Card
        JButton bnOptions = new JButton("Options");
        // Button Functionality
        bnOptions.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                mainWindowLayout.show(mainPanel, "2");
            }
        });
        panelMainMenu.add(bnOptions);
        panelMainMenu.setOpaque(true);
        // container content identifier(String)
        mainPanel.add(panelMainMenu, "1");
        panelMainMenu.setVisible(true);
    }// buildMainMenu

    private void buildOptions() {
        JPanel panelOptions = new JPanel();
        panelOptions.setLayout(new GridBagLayout());
        // standardized margins for all Elements:
        final Insets gbcInsets = new Insets(5, 5, 5, 5);
        panelOptions.setBackground(Color.white);

        JPanel fillerRight = new JPanel();
        JPanel fillerTop = new JPanel();
        JButton bnOptBack = new JButton("Back");

        bnOptBack.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                mainWindowLayout.show(mainPanel, "1");
            }
        });

        // add filler panels to keep button in position
        fillerRight.setBackground(Color.red);
        fillerRight.setOpaque(true);
        panelOptions.add(fillerRight, new GridBagConstraints(1, 1, 1, 2, 1, 0,
                GridBagConstraints.NORTH, GridBagConstraints.BOTH, gbcInsets,
                0, 0));
        fillerRight.setVisible(true);

        fillerTop.setBackground(Color.pink);
        fillerTop.setOpaque(true);
        panelOptions.add(fillerTop, new GridBagConstraints(0, 0, 2, 1, 0, 1,
                GridBagConstraints.NORTH, GridBagConstraints.BOTH, gbcInsets,
                0, 0));
        fillerTop.setVisible(true);

        // 1 Button in lower left hand corner, sends you back to MainMenu-card 
        panelOptions.add(bnOptBack, new GridBagConstraints(0, 1, 1, 1, 0, 0,
                GridBagConstraints.NORTH, GridBagConstraints.BOTH, gbcInsets,
                0, 0));
        bnOptBack.setVisible(true);

        panelOptions.setOpaque(true);
        // container content identifier(String)
        mainPanel.add(panelOptions, "2");
        panelOptions.setVisible(true);
    }// buildOptions

    public static void main(String[] args) {
        MainFrame m = new MainFrame();
    }// main
}// MainFrame

Any help is greatly appreciated. Thanks in advance :) Edit: I guess you don't need to look at the "buildMainMenu" and "buildOptions" methods too closely, the setup for the single menu pages seems to work fine.

Upvotes: 2

Views: 1045

Answers (1)

Robin
Robin

Reputation: 36611

It works as expected on my machine when I comment out the following two lines

panelOptions.setVisible(true);

and

panelMainMenu.setVisible(true);

When using the CardLayout, you just add the components and use CardLayout#show to decide which one you show. Calling setVisible first seems to cause problems (for whatever reason).

Making sure that the Swing code is triggered on the EDT is also a good idea:

public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        MainFrame m = new MainFrame();
      }
    });

Upvotes: 5

Related Questions