so i'm trying to access the JComboBox from the class called Calculator. the JComboBox itself lies in a method, and inside a JPanel in another class called GUI. thanks.
here's my code
public class GUI {
public static JFrame MainFrame(){
//the endless code
frame.add(konvpanel();
return frame;
}
public static JPanel konvpanel(){
JPanel a = new JPanel();
String [] itembox = {"...","XXX","===","|||"};
JComboBox nnn = new JComboBox(itembox);
a.add(nnn);
return a;
}
}
thanks in advance.
You can't access nnn
since that reference doesn't exist outside the scope of that method. So you have two options:
I'd go with option one.
Another option, depending on why you need to access the combo box, might be to add a listener to the combo box and add a reference to the Calculator
instance to that listener as well. Then whenever the event you registered for is triggered, you pass that information to the calculator.
In your case to access the JComboBox
, declare it as below.
public class GUI {
public static JComboBox nnn;
public static JPanel konvpanel(){
JPanel a = new JPanel();
String [] itembox = {"...","XXX","===","|||"};
nnn = new JComboBox(itembox); /*refer the previously declared variable*/
a.add(nnn);
return a;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.