Peddler
Peddler

Reputation: 6085

CardLayout issue

I have a card layout, first card is a menu.

I Select the second card, and carry out some action. We'll say add a JTextField by clicking a button. If I return to the menu card, and then go back to the second card, that JTextField I added the first time will still be there.

I want the second card to be as I originally constructed it each time I access it, with the buttons, but without the Textfield.

Upvotes: 1

Views: 1889

Answers (2)

nIcE cOw
nIcE cOw

Reputation: 24626

Here is the final sorted out version, to remove the card, after doing changes to it, have a look, use the revalidate() and repaint() thingy as usual :-)

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

public class ApplicationBase extends JFrame
{
    private JPanel centerPanel;
    private int topPanelCount = 0;

    private String[] cardNames = {
                                                        "Login Window",
                                                        "TextField Creation"
                                                   };

    private TextFieldCreation tfc;
    private LoginWindow lw;

    private JButton nextButton;
    private JButton removeButton;

    private ActionListener actionListener = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            if (ae.getSource() == nextButton)
            {   
                    CardLayout cardLayout = (CardLayout) centerPanel.getLayout();
                    cardLayout.next(centerPanel);
            }
            else if (ae.getSource() == removeButton)
            {
                    centerPanel.remove(tfc);
                    centerPanel.revalidate();
                    centerPanel.repaint();
                    tfc = new TextFieldCreation();
                    tfc.createAndDisplayGUI();  
                    centerPanel.add(tfc, cardNames[1]);
            }
        }
    };

    private void createAndDisplayGUI()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        centerPanel = new JPanel();
        centerPanel.setLayout(new CardLayout());

        lw = new LoginWindow();
        lw.createAndDisplayGUI();
        centerPanel.add(lw, cardNames[0]);
        tfc = new TextFieldCreation();
        tfc.createAndDisplayGUI();
        centerPanel.add(tfc, cardNames[1]);

        JPanel bottomPanel = new JPanel();
        removeButton = new JButton("REMOVE");
        nextButton = new JButton("NEXT");       
        removeButton.addActionListener(actionListener);
        nextButton.addActionListener(actionListener);

        bottomPanel.add(removeButton);
        bottomPanel.add(nextButton);

        add(centerPanel, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.PAGE_END);

        pack();
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ApplicationBase().createAndDisplayGUI();
            }
        });
    }
}

class TextFieldCreation extends JPanel
{
    private JButton createButton;
    private int count = 0;

    public void createAndDisplayGUI()
    {
        final JPanel topPanel = new JPanel();
        topPanel.setLayout(new GridLayout(0, 2));

        createButton = new JButton("CREATE TEXTFIELD");
        createButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                JTextField tfield = new JTextField();
                tfield.setActionCommand("JTextField" + count);

                topPanel.add(tfield);
                topPanel.revalidate();
                topPanel.repaint();
            }
        });

        setLayout(new BorderLayout(5, 5));
        add(topPanel, BorderLayout.CENTER);
        add(createButton, BorderLayout.PAGE_END);
    }
}

class LoginWindow extends JPanel
{
    private JPanel topPanel;
    private JPanel middlePanel;
    private JPanel bottomPanel;

    public void createAndDisplayGUI()
    {
        topPanel = new JPanel();

        JLabel userLabel = new JLabel("USERNAME : ", JLabel.CENTER);
        JTextField userField = new JTextField(20);
        topPanel.add(userLabel);
        topPanel.add(userField);

        middlePanel = new JPanel();

        JLabel passLabel = new JLabel("PASSWORD : ", JLabel.CENTER);
        JTextField passField = new JTextField(20);
        middlePanel.add(passLabel);
        middlePanel.add(passField);

        bottomPanel = new JPanel();

        JButton loginButton = new JButton("LGOIN");
        bottomPanel.add(loginButton);

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        add(topPanel);
        add(middlePanel);
        add(bottomPanel);
    }
}

If you just wanted to remove the Latest Edit made to the card, try this code :

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

public class ApplicationBase extends JFrame
{
    private JPanel centerPanel;
    private int topPanelCount = 0;

    private String[] cardNames = {
                                                        "Login Window",
                                                        "TextField Creation"
                                                   };

    private TextFieldCreation tfc;
    private LoginWindow lw;

    private JButton nextButton;
    private JButton removeButton;

    private ActionListener actionListener = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            if (ae.getSource() == nextButton)
            {   
                    CardLayout cardLayout = (CardLayout) centerPanel.getLayout();
                    cardLayout.next(centerPanel);
            }
            else if (ae.getSource() == removeButton)
            {
                    TextFieldCreation.topPanel.remove(TextFieldCreation.tfield);
                    TextFieldCreation.topPanel.revalidate();
                    TextFieldCreation.topPanel.repaint();
            }
        }
    };

    private void createAndDisplayGUI()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        centerPanel = new JPanel();
        centerPanel.setLayout(new CardLayout());

        lw = new LoginWindow();
        lw.createAndDisplayGUI();
        centerPanel.add(lw, cardNames[0]);
        tfc = new TextFieldCreation();
        tfc.createAndDisplayGUI();
        centerPanel.add(tfc, cardNames[1]);

        JPanel bottomPanel = new JPanel();
        removeButton = new JButton("REMOVE");
        nextButton = new JButton("NEXT");       
        removeButton.addActionListener(actionListener);
        nextButton.addActionListener(actionListener);

        bottomPanel.add(removeButton);
        bottomPanel.add(nextButton);

        add(centerPanel, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.PAGE_END);

        pack();
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ApplicationBase().createAndDisplayGUI();
            }
        });
    }
}

class TextFieldCreation extends JPanel
{
    private JButton createButton;
    private int count = 0;
    public static JTextField tfield;
    public static JPanel topPanel;

    public void createAndDisplayGUI()
    {
        topPanel = new JPanel();
        topPanel.setLayout(new GridLayout(0, 2));

        createButton = new JButton("CREATE TEXTFIELD");
        createButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                tfield = new JTextField();
                tfield.setActionCommand("JTextField" + count);

                topPanel.add(tfield);
                topPanel.revalidate();
                topPanel.repaint();
            }
        });

        setLayout(new BorderLayout(5, 5));
        add(topPanel, BorderLayout.CENTER);
        add(createButton, BorderLayout.PAGE_END);
    }
}

class LoginWindow extends JPanel
{
    private JPanel topPanel;
    private JPanel middlePanel;
    private JPanel bottomPanel;

    public void createAndDisplayGUI()
    {
        topPanel = new JPanel();

        JLabel userLabel = new JLabel("USERNAME : ", JLabel.CENTER);
        JTextField userField = new JTextField(20);
        topPanel.add(userLabel);
        topPanel.add(userField);

        middlePanel = new JPanel();

        JLabel passLabel = new JLabel("PASSWORD : ", JLabel.CENTER);
        JTextField passField = new JTextField(20);
        middlePanel.add(passLabel);
        middlePanel.add(passField);

        bottomPanel = new JPanel();

        JButton loginButton = new JButton("LGOIN");
        bottomPanel.add(loginButton);

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        add(topPanel);
        add(middlePanel);
        add(bottomPanel);
    }
}

Upvotes: 1

Eric Hydrick
Eric Hydrick

Reputation: 3537

Make sure the panel you're trying to reset has code that takes it back to its "as it was originally constructed" state. Then, when you process the whatever event that causes you to change cards, call that code to restore the original state before showing the card.

Upvotes: 1

Related Questions