Matthew Morse
Matthew Morse

Reputation: 11

Need to make a variable within an action listener public

I am creating a GUI in which you first click on a radio button to define a variable, and then you click on a button. This button then tests for that variable and then performs two different actions depending on what the integer equals. My problem is that, when I click on the button, the variable which is defined with the radio buttons is not registered. I need to make this variable be able to be found by the action listener which the button takes advantage of. Here is a segment of my code:

radioButtonYes.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        int a = 1;  
    }
});
radioButtonNo.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        int a = 2;
    }
});
submitOne.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if(a == 1){
            frame.setVisible(false);
            JPanel panelFour = new JPanel();
            JLabel labelTwo = new JLabel("Stuff");
            panelFour.add(labelTwo);
            frame.remove(panelOne);
            frame.remove(panelTwo);
            frame.remove(submitOne);
            frame.add(panelFour);
            frame.setVisible(true);
        }else{
            frame.setVisible(false);
            JPanel panelFive = new JPanel();
            JLabel labelThree = new JLabel("Alternate stuff");
            panelFive.add(labelThree);
            frame.remove(panelOne);
            frame.remove(panelTwo);
            frame.remove(submitOne);
            frame.add(panelFive);
            frame.setVisible(true);
        }

    }
});

Upvotes: 1

Views: 1678

Answers (4)

SHILPA SAREEN
SHILPA SAREEN

Reputation: 11

there is no way of making local variable global as scope of variable 'a' is lost when submitOne button is clicked so declare variable 'a' globally.so that it will retain value of last clicked button.

Upvotes: 1

Cody Anderson
Cody Anderson

Reputation: 124

Action Listeners are static method and can only target static variables if they are not created inside the methods themselves. If this is how you want to go about doing this I suggest the following:

Create a global variable static int a;

Then with all the methods remove the int in front of the variable like so, so that they target the global variable and set it to the correct value you are looking for

radioButtonNo.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        a = 2;
    }
});

Upvotes: 2

Joseph K. Strauss
Joseph K. Strauss

Reputation: 4903

You cannot access local variables from outside of the block that it was declared. The fact that you are even allowed to declare int a = 1; is a clear indication that it is a strictly local variable. You must have some other int declared somewhere else in your code, otherwise, if(a == 1){ would not execute.

Either way, you do not need to create listeners for the radio buttons. For your test just see if the yesRadioButton is selected:

if(radioButtonYes.isSelected()){
   ...
}

Of course, yesRadioButton must be declared final before Java 8.

If you are using Java 8, it only needs to be effectively final, meaning that it is not modified anywhere.

In Java 8, declaring ActionListener can be simplified as well:

submitOne.addActionListener( e -> {
  ...
}):

Upvotes: 1

Aaron Esau
Aaron Esau

Reputation: 1123

Ok. Try:

radioButtonYes.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if( e.getSource() == radioButtonYes) {
                int a = 1;  
            }
        }
});

And change radioButtonYes to whatever...

Upvotes: 2

Related Questions