1

I am creating a WindowBuilder GUI and need to pass a variable created with a radio button to an EventHandler class to use in further processing. The output of the radio button event is successful; however, the variable "df", which is declared in the actionPerformed method in not resolved in the EventHanler class. Any help will be appreciated.

public TestClass() {

    /* INSERT RADIOBUTTON INTO FRAME. */
    JRadioButton rdbtnNo = new JRadioButton("No");
    rdbtnNo.setFont(new Font("Tahoma", Font.BOLD, 12));
    rdbtnNo.setBounds(332, 509, 63, 23);
    frame.getContentPane().add(rdbtnNo);

    /* LISTEN FOR RADIOBUTTON BUTTON. */
    rdbtnNo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            String command = event.getActionCommand();
            System.out.println(command);
            int df = 20;                    
        }           
    });

    rdbtn.setActionCommand("event");
    rdbtn.addActionListener(new EventHandler());

}

public class EventHandler implements ActionListener {
    public void actionPerformed(ActionEvent event) {

        System.out.println(df);
    }
}                               

4 Answers4

0
rdbtnNo.addActionListener(new EventHandler());

in your code. you have other mistakes too, but when you have that inner class declared below, you must instantiate it in the addActionListener method to take advantage of it.

strash
  • 1,291
  • 2
  • 15
  • 29
0

It cannot be resolved in the other class, because the scope of that variable is only in that method:

public void actionPerformed(ActionEvent event) {
                String command = event.getActionCommand();
                System.out.println(command);
                int df = 20;                    
            } 
strash
  • 1,291
  • 2
  • 15
  • 29
0

the variable "df", which is declared in the actionPerformed method in not resolved in the EventHanler class.

This is because of variable scope. In your example, you are declaring df as a local variable inside the actionPerformed(ActionEvent) method of the anonymous inner class you are passing to addActionListener(ActionListener). Local variables can only be accessed inside the code block they were created in. This means that your df variable cannot be accessed anywhere else other than the actionPerformed(ActionEvent) method.

The first step to fixing this is making df an instance variable in your Test class so that it can be accessed inside and outside of the actionPerformed(ActionEvent) method.

From here there are two possible approaches:

  1. Use a second anonymous inner class for both buttons

    public class Test {
    
        private int df;
    
        public Test() {
            // ...
            final JButton button = new JButton("Click Me");
            button.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    df = 20;
                }
    
            });
            final JButton button2 = new JButton("Click Me Again");
            button2.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(df == 20) { // got instance
                        // TODO do a thing
                    }
                }
    
            });
            // ...
        }
    
    }
    
  2. Pass df to the constructor of EventHandler

    public class Test {
    
        private int df;
    
        public Test() {
            // ... button1 ...
            final JButton button2 = new JButton("Click Me Again");
            button2.addActionListener(new EventHandler());
            // ...
        }
    
    }
    
    // different file
    public class EventHandler implements ActionListener {
    
        private int df;
    
        public EventHandler(int df) {
            this.df = df; // got instance
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            if(df == 20) {
                // TODO do a thing
            }
        }
    }
    

Side Note: Don't use a null layout! See Why is it frowned upon to use a null layout in Swing?

Community
  • 1
  • 1
MasterBlaster
  • 948
  • 2
  • 12
  • 26
-1

First thing is get rid of the anonymous inner class and use lambdas instead. It makes your code much simpler to understand.

public TestClass() {

    /* INSERT RADIOBUTTON INTO FRAME. */
    JRadioButton rdbtnNo = new JRadioButton("No");
    rdbtnNo.setFont(new Font("Tahoma", Font.BOLD, 12));
    rdbtnNo.setBounds(332, 509, 63, 23);
    frame.getContentPane().add(rdbtnNo);

    /* LISTEN FOR RADIOBUTTON BUTTON. */
    rdbtnNo.addActionListener(event -> pressedTheButton(event));

    rdbtn.setActionCommand("event");
    rdbtn.addActionListener(new EventHandler());

}

public void pressedTheButton(ActionEvent event) {
    String command = event.getActionCommand();
    System.out.println(command);
    int df = 20;
    printStuff(df);
}

public void printStuff(int input) {
    System.out.println(input);
}



           ///DELETE THIS.  This is unneeded, use Java 8 stuff, it's awesome////
public class EventHandler implements ActionListener {
    public void actionPerformed(ActionEvent event) {

        System.out.println(df);
    }
}       
Nick Ziebert
  • 1,258
  • 1
  • 9
  • 17