J Xkcd
J Xkcd

Reputation: 423

ActionListener for JButton from another class not triggering

My main class with a static CardLayout JPanel which includes a JPanel from the Home class.

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

public class Runner1 extends JPanel{

    public Runner1() {
        initComponents();
    }

    private void initComponents() {
        setLayout(new CardLayout());

        pnlHome = new Home();
        pnlSignIn = new SignIn();
        add(pnlHome, "Home");
        add(pnlSignIn, "SignIn");
    }

    public static void showJPanel(String s) {
        CardLayout cl = (CardLayout) (pnlRunner.getLayout());
        cl.show(pnlRunner, s);
    }
    
    public static void createAndShowGUI(){
        pnlRunner = new Runner1();
        
        JFrame frame = new JFrame();
        frame.setTitle("Delivery System");
        frame.setSize(new Dimension(500, 400));
        frame.getContentPane().add(pnlRunner);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    
    public static Runner1 pnlRunner;
    private JPanel pnlHome;
    private JPanel pnlSignIn;
}

My other class where the actionlistener for the JButton doesn't get triggered, when debugging, the btnNewOrderActionPerformed doesn't get executed.

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

public class Home extends JPanel  {
    public Home() {
        initComponents();
    }
    
    private void initComponents(){
        
        setLayout(new BorderLayout());
        
        add(new TextArea("Active Orders"),BorderLayout.CENTER);
        
        JButton btnNewOrder1 = new JButton("New Order");
        btnNewOrder1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnNewOrderActionPerformed(e);
            }
        });
        add(btnNewOrder1, BorderLayout.PAGE_END);
    }
    
    private void btnNewOrderActionPerformed(ActionEvent e){
        System.out.println("test");
    }
    
    private OrderMap[] JOrders; //Lists of JPanel of orders
    private JButton btnNewOrder;
}

Another question regarding static implementation of the CardLayout JPanel, is there a non-static way of accomplishing the same thing (where the shown JPanel can be controlled by components from external classes)?

Upvotes: 0

Views: 30

Answers (1)

camickr
camickr

Reputation: 324098

My other class where the actionlistener for the JButton doesn't get triggered

Works fine for me, after fixing up the code so it compiles. See comment of your initial question.

is there a non-static way of accomplishing the same thing (where the shown JPanel can be controlled by components from external classes)?

First, get rid of the static keyword on the method.

Then you have a couple of options:

  1. pass a reference of the Runner1 class to each child panel.
  2. in the child panel you can use the getParent() method to get a reference to the Runner1 class.

Once you have a reference you can then reference any method in the Runner1 class.

Upvotes: 1

Related Questions