Search code examples
javaeclipseswingwindowbuildercardlayout

How to get CardLayout implemented properly with Eclipse WindowBuilder?


So using WindowBuilder (latest version of Ecliple, Kepler) I've created a frame as so: enter image description here

But I'd like to switch between them with a button, which I've created on the panelWelcome. I'm guessing I'd add an itemListener, then create a method that switches between the cards. The problem is, I have no idea how to proceed after that. Here's the code that's automatically generated:

package client;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.CardLayout;
import java.awt.GridBagLayout;

import javax.swing.JLabel;

import java.awt.GridBagConstraints;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class Test {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test window = new Test();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Test() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new CardLayout(0, 0));

        JPanel panelWelcome = new JPanel();
        frame.getContentPane().add(panelWelcome, "name_98933171901972");
        GridBagLayout gbl_panelWelcome = new GridBagLayout();
        gbl_panelWelcome.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0};
        gbl_panelWelcome.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        gbl_panelWelcome.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
        gbl_panelWelcome.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        panelWelcome.setLayout(gbl_panelWelcome);

        JLabel lblTitle = new JLabel("MEMEPlayer");
        lblTitle.setFont(new Font("Segoe UI", Font.BOLD, 12));
        GridBagConstraints gbc_lblTitle = new GridBagConstraints();
        gbc_lblTitle.insets = new Insets(0, 0, 5, 0);
        gbc_lblTitle.gridx = 5;
        gbc_lblTitle.gridy = 0;
        panelWelcome.add(lblTitle, gbc_lblTitle);

        JLabel lblNewLabel = new JLabel("Welcome! To get started, select a movie from the drop down menu");
        lblNewLabel.setFont(new Font("Segoe UI", Font.PLAIN, 11));
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
        gbc_lblNewLabel.gridx = 5;
        gbc_lblNewLabel.gridy = 2;
        panelWelcome.add(lblNewLabel, gbc_lblNewLabel);

        JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"The Avengers (2012)", "Monsters, Inc. (2001)", "Prometheus (2012)"}));
        GridBagConstraints gbc_comboBox = new GridBagConstraints();
        gbc_comboBox.insets = new Insets(0, 0, 5, 0);
        gbc_comboBox.gridx = 5;
        gbc_comboBox.gridy = 4;
        panelWelcome.add(comboBox, gbc_comboBox);

        JButton btnNewButton = new JButton("Next >");
        GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
        gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
        gbc_btnNewButton.gridx = 5;
        gbc_btnNewButton.gridy = 6;
        btnNewButton.addItemListener((ItemListener) this);
        panelWelcome.add(btnNewButton, gbc_btnNewButton);

        JLabel lblNewLabel_1 = new JLabel("");
        lblNewLabel_1.setIcon(new ImageIcon("C:\\temp\\Meme1\\largeVLC.png"));
        GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
        gbc_lblNewLabel_1.gridx = 5;
        gbc_lblNewLabel_1.gridy = 8;
        panelWelcome.add(lblNewLabel_1, gbc_lblNewLabel_1);

        JPanel panelVideo = new JPanel();
        frame.getContentPane().add(panelVideo, "name_98968999152440");
    }
}

Thanks for any help!


Solution

  • The problem I see you're facing is that you are setting the layout to the frame. This is a problem because it means that the frame can only have on visible component at a time. That component being one of the panels. So you can put a button. The button would have to be on the one of the panels, which may be hard to maintain, in terms of navigation.

    So instead, have a main JPanel that has the CardLayout, and add your card panels to that main panel. Then you can add the main panel to the frame, along with buttons for navigation.

    Another option is to have a menu bar with option to change the cards, then that way, you can keep the card layout on the frame, because navigation is controlled by the menu options.

    See How to Use CardLayout if you're not really sure even how to use CardLayout, hand coding. You're going to need a reference to layout and call one of its navigation methods like show(), next(), or previous()

    You may also find this post interesting. It used Netbeans, but maybe you'll pick something up