Reputation: 39
i declared a class then in that class i declared an int x and array of strings option i created a constructor of class in the consructor i used JOptionPane to choose from 3 options i want to assign int value to x for each option chosen
class A extends JFrame implements ActionListner, TextListener {
..........
int x;
String[] option = {"AA", "BB", "CC"};
A() {
..........
int x = JOptionPane.showOptionDialog(null, "Choose from", "Choose", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, option, option[0]);
}
public void B() {
if(x==2) {
......
}
public static void main(String[] args) {
A lol = new A(arg[0]);
}
sorry cant disclose full code now when i choose option 3 (to assign value 2 to x) ,the function in B does not execute but when i assign the value 2 to x while declaring it, B always executes(even when i choose any other option in dialog box)
any ideas what am i doing wrong
Upvotes: 0
Views: 262
Reputation: 2303
You're declaring a local variable in the constructor with the same name with the class varibale x, remove the keyword int.
A() {
x = JOptionPane.showOptionDialog(null, "Choose from", "Choose", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, option, option[0]);
}
Upvotes: 1