java - How can I access a variable in an JButton ActionListener? -


hopefully isn't of mess, i'm new java. , can't figure out how access "idaysalive" variable outside of actionlistener.

    jbutton jbutton_calculate = new jbutton("calculate");     jbutton_calculate.addactionlistener(new actionlistener() {         public void actionperformed (actionevent arg0) {              int idaybirth = integer.parseint(jtextfield_dobday.gettext());             int imonthbirth = integer.parseint(jtextfield_dobmonth.gettext());             int iyearbirth = integer.parseint(jtextfield_dobyear.gettext());              int idaycurrent = integer.parseint(jtextfield_cdday.gettext());             int imonthcurrent = integer.parseint(jtextfield_cdmonth.gettext());             int iyearcurrent = integer.parseint(jtextfield_cdyear.gettext());              double idaysalive;              calendar cabirthdate = new gregoriancalendar(iyearbirth, imonthbirth - 1, idaybirth);             calendar cacurrentdate = new gregoriancalendar(iyearcurrent, imonthcurrent - 1, idaycurrent);              idaysalive = cacurrentdate.gettimeinmillis() - cabirthdate.gettimeinmillis();             idaysalive = idaysalive / (24 * 60 * 60 * 1000) + 1; //this variable         }     });      jbutton_calculate.setfont(new font("calibri", font.bold, 15));     jbutton_calculate.setbounds(180, 205, 103, 23);     frame.getcontentpane().add(jbutton_calculate);      jlabel lbldaysalive = new jlabel("days alive: " + idaysalive);      //idaysalive cannot resolved variable      lbldaysalive.setfont(new font("calibri", font.bold, 18));     lbldaysalive.setbounds(135, 257, 147, 21);     frame.getcontentpane().add(lbldaysalive); 

to access variable outside of anonymous class has declared outside , has final.

in case not work, because if make double final cannot change it. can either use concrete class instead of anonymous 1 store result in attribute access later. alternatively use helper class result can declared final , holds attribute changed anonymous action listener. or make attribute of enclosing class.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -