LastKind
LastKind

Reputation: 1

How to implements Button ActionListener to run the cardLayout by other classes?

I don't what the problem is? I try to switch the two seperate classes extends JPanel with the cardLayout by using JButton and I don't know am I used the correct code... Here is my coding.

CardLayoutMenu

public class CardLayoutMenu extends JFrame implements ActionListener{

    CardLayout cardLayout = new CardLayout();

    private JPanel p1 = new JPanel(cardLayout);

    final String MAIN = "MAIN";
    final String OPTION = "OPTION";

    MainPanel mainPanel = new MainPanel();
    OptionPanel optionPanel = new OptionPanel();

    private Object object;

    public CardLayoutMenu(Object object) {          
        this.object = object;
    }

    public CardLayoutMenu(){

        setLayout(new BorderLayout());
        setTitle("Card Layout Menu");
        setSize(300,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null);            
        add(p1);            
        p1.add(mainPanel, MAIN);
        p1.add(optionPanel, OPTION);
    }

    public void actionPerformed(ActionEvent e){

        try{
            cardLayout.show(p1, OPTION);
        }catch(Exception ex){
            System.out.println("" + ex);
        }
    }
}

Here is my MainPanel

public class MainPanel extends JPanel{

    private JButton jbtOption = new JButton("Option");

    public MainPanel() {            
        setLayout(new FlowLayout());
        add(jbtOption);         
        jbtOption.addActionListener(new CardLayoutMenu(this));
    }
}

Then my OptionPanel, use the JButton jbtBack to go back the MainPanel

public class OptionPanel extends JPanel{

    private JButton jbtBack = new JButton("Back");

    public OptionPanel() {          
        setLayout(new FlowLayout());
        add(jbtBack);           
    }
}

Upvotes: 0

Views: 608

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

This code here will cause an infinite recursion:

public MainPanel() {
  setLayout(new FlowLayout());
  add(jbtOption);
  jbtOption.addActionListener(new CardLayoutMenu(this));
}

Since this constructor is ultimately called from the CardLayoutMenu class, you'll have a CardLayoutMenu object that creates a MainPanel object which creates a CardLayoutMenu object that creates a MainPanel object which creates a CardLayoutMenu object that creates a MainPanel object which creates a ... well, I think that you get the picture.

One basic rule I strongly urge on you is to not make your GUI classes implement Listener interfaces as it is asking the class to do too much and often leads to confusing code such as yours. This is sort of fine in small example programs, but I wish that it wasn't used as it encourages newbies to continue to do this sort of thing. Instead consider creating an ActionListener object and pass this listener to any class that needs a button that needs to tell the CardLayout to change views. You can pass this listener into these classes via a constructor or setter method parameter.

Upvotes: 3

Related Questions