Evorlor
Evorlor

Reputation: 7553

Initializing String from JOptionPane inside of an ActionListener

I am trying to use a JOptionPane to create a popup window for the user to give some input. I then need to use that input elsewhere. I am getting the error The final local variable answer cannot be assigned, since it is defined in an enclosing type for answer in actionPerformed(ActionEvent) (See below). Is there a way to have the user input a String in the popup window while allowing the main window the have access to that String?

final String answer;
JButton getAnswerButton = getAnswerButton();
getAnswerButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        answer = JOptionPane.showInputDialog("What is the answer?");
    }
});
System.out.println(answer);  //Need access to answer *outside* of my JButton

Upvotes: 0

Views: 551

Answers (3)

formixian
formixian

Reputation: 1559

The prefered option would be to call a protected or private method with the answer as a parameter. This is more convenient since there is no member field involved in and it keeps things together.

public class YourClass {

   public void setButtonListener() {
      JButton getAnswerButton = getAnswerButton();
      getAnswerButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
           String answer = JOptionPane.showInputDialog("What is the answer?");
           displayAnswer(answer);
        }
      });
   }

   protected void displayAnswer(String answer) {
      System.out.println(answer);  //Need access to answer *outside* of my JButton
   }
}

Upvotes: 1

M A
M A

Reputation: 72844

Make answer a field member in your class. You simply cannot refer to a local variable defined in a method from an inner class unless it is final, and since it should be final you cannot assign it again in the inner class. The requirement of making it final is due to the fact that changes done by the inner class ActionListener to variables declared in the method calling the inner class will not be visible to that method.

A better way would be something like:

public class YourClass {
   String answer;

   ...

   public void setButtonListener() {
      JButton getAnswerButton = getAnswerButton();
      getAnswerButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
           answer = JOptionPane.showInputDialog("What is the answer?");
        }
      });
   }

   public void displayAnswer() {
      System.out.println(answer);  //Need access to answer *outside* of my JButton
   }
}

You would call setButtonListener during the setup which would add a listener to the button to set the field, and then you would be able to access the field after the button is clicked.

Upvotes: 1

Nivas
Nivas

Reputation: 18344

When a variable is declated final in Java, a value can be assigned once to the variable but cannot be changed.

To change the value, you may need to remove final from the declation. In your case, you will get another error that a non-final local variable cannot be used in (anonymous) inner classes (which is probably why you added the final in the first place).

Depending on what your application is doing, making answer a class level variable (rather than a method level one) is one solution.

Upvotes: 1

Related Questions