java - how to assign variable from inside an actionlistener to one outside -



java - how to assign variable from inside an actionlistener to one outside -

i'm c programmer handed lastly min java gui task. given not gui or java person, have created 2 objects:

1 text box, i'm hoping assigned results of 2nd object. 2 combo box. when user selects combo box, wish value populated first object (textfield). here actionlistener():

class foo { // declared instance variable private string thevalue; // created textfield, , jcombobox thingies listbox.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { // line produces reported error //textfield.settext( (string)(((jcombobox)e.getsource()).getselecteditem()) ); // line works, below error when seek assign thevalue outside method thevalue = ((jcombobox)e.getsource()).getselecteditem(); } }); ... textfield.setvalue(thevalue); // errors out

and next (apparently know java error): cannot refer non-final variable textfield within inner class defined in different method

the commented line produced same results. hoping able assign thevalue value of text field, can not declare within listener, there scope problem.

any assistance appreciated :-)

either create jtextfield final or create instance field within class.

final jtextfield textfield = ...

or

public class ... { private jtextfield textfield; public ...() { = new jtextfield(...);

you can other variables to. typically, unless have reason otherwise, suggest using instance fields. take @ understanding class members more details...

you can think of instance fields "private" variables in c, declared within c file itself, can not referenced externally file declared in (sorry long time since i've done c might not exclusively correct)

updated

firstly, gui's tend event driven, is, don't operate in linear/procedural fashion. set bunch of callbacks , wait trigger them. when callback triggered, take appropriate action...

import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class test { public static void main(string[] args) { new test(); } public test() { eventqueue.invokelater(new runnable() { @override public void run() { seek { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } grab (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { private jtextfield textfield; private jcombobox combobox; private string thevalue; public testpane() { textfield = new jtextfield(10); combobox = new jcombobox(new string[]{"banana", "apple", "grapes", "strawberries"}); combobox.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { thevalue = (string)combobox.getselecteditem(); textfield.settext(thevalue); } }); combobox.setselecteditem(null); setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridwidth = gridbagconstraints.remainder; add(textfield, gbc); add(combobox, gbc); } } }

in example, actionlistener assigned jcombobox not called immediately, meaning remaining code below assignment run , before actionlistener has chance of been called.

think of function pointer or callback. can pass function, don't know when might called...

when state of jcombobox changes , triggers , action event, actionlisteners actionpeformed method called, @ time can obtain current value , apply text field , assign variable...or ever else need do...

notice, attached actionlistener , called combobox.setselecteditem(null), cause actionlistener notified...tricky ;)

java variable-assignment

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -