WookieFer
WookieFer

Reputation: 3

Selecting a value from an actionPerformed method

I'm trying to get the value from the variable dificultad, which is inside the actionPerformed method to use it in another class. But I really have no clue on how to do it. So I don't know if maybe you could help.

jComboBox1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            int dificultad;
            if (jComboBox1.getSelectedItem() == "Facil") {
                dificultad = 10;
                System.out.println("Facil");
            } else if (jComboBox1.getSelectedItem() == "Intermedio") {
                dificultad = 8;
                System.out.println("Intermedio");
            } else if (jComboBox1.getSelectedItem() == "Dificil") {
                dificultad = 4;
                System.out.println("Dificl");
            } else if (jComboBox1.getSelectedItem() == "Extremo") {
                dificultad = 1;
                System.out.println("Extremo");
            }

        }

Upvotes: 0

Views: 42

Answers (2)

X-Fate
X-Fate

Reputation: 323

Define a variable (public) at the top of your class.

public class testClass {
    public int testVar = 0;

    public void action(){
        jComboBox1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            int dificultad;
            if (jComboBox1.getSelectedItem() == "Facil") {
                dificultad = 10;
                System.out.println("Facil");
            } else if (jComboBox1.getSelectedItem() == "Intermedio") {
                dificultad = 8;
                System.out.println("Intermedio");
            } else if (jComboBox1.getSelectedItem() == "Dificil") {
                dificultad = 4;
                System.out.println("Dificl");
            } else if (jComboBox1.getSelectedItem() == "Extremo") {
                dificultad = 1;
                System.out.println("Extremo");
            }
            testVar = dificultad;
        }
        }
    }
}

To use the value in an other class, simply pass the class object in - for example - a main function and access to the variable.

public class testClass2 {
    public testClass;

    public testClass2(testClass tc) {
        this.testClass = tc;
    }

    public void anotherAction(){
        if (this.testClass.testVar == 1) {
            System.out.println("Extremo!");
        }
    }
}

Hope this helps.

Upvotes: 1

display name
display name

Reputation: 4185

//set a field to use outside the scope of addActionListener()
int laDificulty = 0;

jComboBox1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            int dificultad;
            if (jComboBox1.getSelectedItem() == "Facil") {
                dificultad = 10;
                System.out.println("Facil");
            } else if (jComboBox1.getSelectedItem() == "Intermedio") {
                dificultad = 8;
                System.out.println("Intermedio");
            } else if (jComboBox1.getSelectedItem() == "Dificil") {
                dificultad = 4;
                System.out.println("Dificl");
            } else if (jComboBox1.getSelectedItem() == "Extremo") {
                dificultad = 1;
                System.out.println("Extremo");
            }
            laDificulty = dificultad;

        }

Upvotes: 0

Related Questions