java - Using a ToggleButton with a while loop JavaFX -
i'm making javafx application has toggle button. want while loop run long button toggled , stop when button toggled off. right in program, while loop activates when press button loop not stop when toggle off button. there i'm missing make loop stop when toggle off button?
import java.util.hashmap; import java.util.map; import java.util.concurrent.atomic.atomicboolean; import javafx.application.application; import javafx.scene.scene; import javafx.scene.control.togglebutton; import javafx.scene.layout.borderpane; import javafx.stage.stage; public class main extends application{ private borderpane layout; private scene scene; togglebutton button = new togglebutton("button"); atomicboolean running = new atomicboolean(); public static void main(string[] args) { launch(args); } @override public void start(stage window) throws exception { layout = new borderpane(); scene = new scene(layout, 450, 80); button.setonaction(e -> robot()); layout.setcenter(button); window.settitle("desktop app"); window.setscene(scene); window.show(); } public void robot() { map<integer, character> keymap = new hashmap(); running.set(button.isselected()); keymap.put(1, 'a'); keymap.put(2, 'b'); keymap.put(3, 'c'); keymap.put(4, 'd'); keymap.put(5, 'e'); keymap.put(6, 'f'); keymap.put(7, 'g'); keymap.put(8, 'h'); keymap.put(9, 'i'); keymap.put(10, 'j'); keymap.put(11, 'k'); keymap.put(12, 'l'); keymap.put(13, 'm'); keymap.put(14, 'n'); keymap.put(15, 'o'); keymap.put(16, 'p'); keymap.put(17, 'q'); keymap.put(18, 'r'); keymap.put(19, 's'); keymap.put(20, 't'); keymap.put(21, 'u'); keymap.put(22, 'v'); keymap.put(23, 'w'); keymap.put(24, 'x'); keymap.put(25, 'y'); keymap.put(26, 'z'); keymap.put(27, ' '); keymap.put(28, '0'); keymap.put(29, '1'); keymap.put(30, '2'); keymap.put(31, '3'); keymap.put(32, '4'); keymap.put(33, '5'); keymap.put(34, '6'); keymap.put(35, '7'); keymap.put(36, '8'); keymap.put(37, '9'); if(running.get()) { while(running.get()) //only runs when button has been toggled { int randomint = (int )(math.random() * 37 + 1); system.out.println(keymap.get(randomint)); } } //while button toggled //choose random integer //map random integer key on keyboard //robot presses key on focused window } }
running while loop on fx application thread block thread, means no more interaction gui possible.
the condition of while loop never change, because of selectedproperty
of togglebutton
never change execution never exits while loop.
you use service
execute loop on background thread have gui responsive when loop runs. service
s can canceled , restarted.
example
the following example creates service
runs task
executes while loop until service
gets cancelled.
i listen changes of selectedproperty
of togglebutton
:
if selected: service reseted , started
if not selected: service gets cancelled
code
togglebutton toggle = new togglebutton(); service<void> toggleservice = new service<void>() { @override protected task<void> createtask() { return new task<void>(){ @override protected void call() throws exception { while(!iscancelled()) { int randomint = (int )(math.random() * 37 + 1); system.out.println(randomint); } return null; } }; } }; toggle.selectedproperty().addlistener((obs, oldval, newval) -> { if(newval) { toggleservice.reset(); toggleservice.start(); } else toggleservice.cancel(); });
Comments
Post a Comment