Reputation: 6293
I'm writing a GUI in Java. One method initializes and displays a form:
public class launchQMBPMN extends CytoscapeAction {
private JComboBox termDB;
public launchQMBPMN(QMBPMN SaddleSum) {
super("SaddleSum");
setPreferredMenu("Plugins");
}
public class buttonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JFrame hello = new JFrame();
JLabel test = new JLabel(termDB.getSelectedItem());
test.add(hello);
hello.show();
}
}
public void actionPerformed(ActionEvent e) {
CytoscapeDesktop desktop = Cytoscape.getDesktop();
InteractionTools tools = new InteractionTools();
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 0.5;
buttonListener buttonPressed = new buttonListener();
// TERM DATABASE AND WEIGHTS
JPanel qmbpTermsPanel = new JPanel(new GridBagLayout());
termDB = new JComboBox(tools.discoverTermDatabases());
c.gridx = 1;
c.gridy = 0;
qmbpTermsPanel.add(termDB, c);
...
I'd like to access 'termDB' in my buttonListner class. How do I do that?
Upvotes: 0
Views: 888
Reputation: 28703
Access it as you posted here. Some comments:
show
method of JFrame
(actually, of java.awt.Window
) is deprecated, use setVisible(true);
instead.
I'm not sure test.add(hello);
is that you really need. Is it? It adds the frame to the label.
termDB.getSelectedItem()
returns an Object
, JLabel
constructor requires a string: termDB.getSelectedItem().toString()
?
Upvotes: 1
Reputation: 120168
You can create a subclass of ButtonListener and pass the termDB into it when you create it, or otherwise set it.
Or you can can define an anonymous inner class where need the button listener and make termDB final, and it will be available in your ButtonListener implementation. Or you could pass the termDB reference to the anonymous inner classs like you would in the first option I presented.
Upvotes: 0
Reputation: 45568
Simply access it using its name, that should work as it's inside the outer class.
See also: http://download.oracle.com/javase/tutorial/java/javaOO/innerclasses.html
Upvotes: 2