java - How to use variables set in an actionlistener -
i having problems understanding how utilize actionlistener alter value of variables.
in programme need store choices user makes selecting radio buttons.
i have got main class card layout, several classes each different panels. in 1 of panels have radio buttons, actionlistener inner class.
when seek print variable value in main class, printed immediately, before user has made choice, instantiate panel class , variable variable before has been changed user.
i know should not think in linear manner java, how can create sure fetch variable after has been changed user , not before? not able i? understand there flaw in thinking haven't slept ages , cannot head around this.
public class screen3 extends jpanel{ jradiobutton add-on = new jradiobutton("addition"); jradiobutton subtraction = new jradiobutton("subtraction"); jradiobutton multiplication = new jradiobutton("multiplication"); jradiobutton partition = new jradiobutton("division"); jradiobutton = new jradiobutton("all"); jradiobutton single = new jradiobutton("single"); jradiobutton 2 = new jradiobutton("double"); jradiobutton triple = new jradiobutton("triple"); jradiobutton mix = new jradiobutton("mix"); jradiobutton 5 = new jradiobutton("5"); jradiobutton 10 = new jradiobutton("10"); private int type, digit, rounds; public screen3() { jpanel firstbuttonpanel = new jpanel(); jpanel secondbuttonpanel = new jpanel(); buttongroup myfirstgroup = new buttongroup(); buttongroup mysecondgroup = new buttongroup(); myfirstgroup.add(addition); myfirstgroup.add(subtraction); myfirstgroup.add(multiplication); myfirstgroup.add(division); //myfirstgroup.add(all); mysecondgroup.add(single); mysecondgroup.add(two); mysecondgroup.add(triple); //mysecondgroup.add(mix); firstbuttonpanel.setlayout(new flowlayout()); firstbuttonpanel.add(addition); firstbuttonpanel.add(subtraction); firstbuttonpanel.add(multiplication); firstbuttonpanel.add(division); //firstbuttonpanel.add(all); secondbuttonpanel.setlayout(new flowlayout()); secondbuttonpanel.add(single); secondbuttonpanel.add(two); secondbuttonpanel.add(triple); //secondbuttonpanel.add(mix); jpanel buttons = new jpanel(); buttons.setlayout(new borderlayout()); buttons.add(selectionlabel, borderlayout.north); buttons.add(firstbuttonpanel, borderlayout.center); buttons.add(secondbuttonpanel, borderlayout.south); buttongroup mythirdgroup = new buttongroup(); jpanel endbuttons = new jpanel(); mythirdgroup.add(five); mythirdgroup.add(ten); endbuttons.add(five); endbuttons.add(ten); endpanel.setlayout(new borderlayout()); endpanel.add(rounds, borderlayout.north); endpanel.add(endbuttons, borderlayout.center); setlayout(new borderlayout()); add(buttons, borderlayout.north); selection sn = new selection(); addition.addactionlistener(sn); subtraction.addactionlistener(sn); multiplication.addactionlistener(sn); division.addactionlistener(sn); single.addactionlistener(sn); two.addactionlistener(sn); triple.addactionlistener(sn); five.addactionlistener(sn); ten.addactionlistener(sn); } public int gettype() { homecoming type; } public int getdigit() { homecoming digit; } public int getrounds() { homecoming rounds; } public class selection implements actionlistener { public void actionperformed(actionevent e) { if(addition.isselected()) { type = 1; } else if(subtraction.isselected()) { type = 2; } else if(multiplication.isselected()) type = 3; else if(division.isselected()) type = 4; //else if(all.isselected()) //type = 5; if(single.isselected()) { digit = 1; system.out.println("single"); } else if(two.isselected()) digit = 2; else if(triple.isselected()) digit = 3; if(five.isselected()) rounds = 5; else if(ten.isselected()) rounds = 10; } }
}
here main class:
public class driver { public jframe frame = new jframe("math game"); public jpanel screens = new jpanel(new cardlayout()); int digit = 1; int rounds = 1; int type = 1; driver() { } public void show() { jpanel buttonpanel = new jpanel(); jbutton next = new jbutton("next"); jbutton previous = new jbutton("previous"); buttonpanel.add(previous); buttonpanel.add(next); screen1 screen1 = new screen1(); screen2 screen2 = new screen2(); screen3 screen3 = new screen3(); screens.add(screen1, "welcome"); screens.add(screen2, "next"); screens.add(screen3, "selection"); frame.add(screens); frame.add(buttonpanel, borderlayout.page_end); frame.setsize(400, 500); frame.setlocationrelativeto(null); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); next.addactionlistener(new actionlistener() { public void actionperformed(actionevent ae) { cardlayout cl = (cardlayout)(screens.getlayout()); cl.next(screens); } }); previous.addactionlistener(new actionlistener() { public void actionperformed(actionevent ae) { cardlayout cl = (cardlayout)(screens.getlayout()); cl.previous(screens); } }); } public static void main(string args[]) { swingutilities.invokelater(new runnable() { public void run() { driver dr = new driver(); dr.show(); } }); }
}
i seek test print of system.out.println(screen3.gettype()); either in show() or main
use joptionpane
/jdialog
has modality.
have read on how create dialogs
in illustration here printed after jdialog
closed:
public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { jdialog jd = new jdialog(); jd.setdefaultcloseoperation(jdialog.dispose_on_close); jd.setmodal(true); jd.pack(); jd.setvisible(true); system.out.println("here"); } }); }
in illustration here printed after joptionpane
closed:
public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { jpanel panel=new jpanel(); panel.add(new jlabel("hello, world!")); joptionpane.showmessagedialog(null, panel, "panel message",joptionpane.plain_message); system.out.println("here"); } }); }
i know should not think in linear manner java, how can create sure fetch variable after has been changed user , not before?
after using modal jdialog
/joptionpane
utilize public getters access variable contained within class instance:
public class test { public static void main(string[] args) { x x= new x();//will homecoming after dialog closed system.out.println(x.gety()); } } class x { private int y=0;//will assigned during dialog/joptionpanes life span public x() { swingutilities.invokelater(new runnable() { @override public void run() { //creates , shows modal dialog/optionpane allow modification of variable y through input/controls } }); } public int gety() { homecoming y; } }
java swing actionlistener
No comments:
Post a Comment