how to submit a form automatically in javafx -
i want create cbt in javafx , run problem of not knowing how submit form automatically if time elapsed , may 1 of students yet finish test. also, want know how disable form in javafx
disabling node
can done calling node.setdisable(true)
. since children automatically disabled, parent of node
s want disable, long there no other children should not disabled.
a timeout can implemented using scheduledexecutorservice
:
private scheduledexecutorservice executorservice; @override public void start(stage primarystage) { textfield tf = new textfield(); label label = new label("your name: "); button submit = new button("submit"); gridpane root = new gridpane(); label.setlabelfor(tf); root.addrow(0, label, tf); root.add(submit, 1, 1); root.setpadding(new insets(10)); root.setvgap(5); root.sethgap(5); atomicboolean done = new atomicboolean(false); executorservice = executors.newscheduledthreadpool(1); // schedule timeout execution in 10 sec scheduledfuture future = executorservice.schedule(() -> { if (!done.getandset(true)) { system.out.println("timeout"); platform.runlater(() -> { root.setdisable(true); }); } }, 10, timeunit.seconds); submit.setonaction((actionevent event) -> { if (!done.getandset(true)) { // print result , stop timeout task future.cancel(false); system.out.println("your name " + tf.gettext()); } }); scene scene = new scene(root); primarystage.setscene(scene); primarystage.show(); } @override public void stop() throws exception { executorservice.shutdown(); }
if want show time in ui, timeline
may more suitable scheduledexecutorservice
however:
@override public void start(stage primarystage) { textfield tf = new textfield(); label label = new label("your name:"); button submit = new button("submit"); gridpane root = new gridpane(); label.setlabelfor(tf); label time = new label("time:"); progressbar bar = new progressbar(); time.setlabelfor(bar); root.addrow(0, time, bar); root.addrow(1, label, tf); root.add(submit, 1, 2); root.setpadding(new insets(10)); root.setvgap(5); root.sethgap(5); timeline timeline = new timeline( new keyframe(duration.zero, new keyvalue(bar.progressproperty(), 0)), new keyframe(duration.seconds(10), evt -> { // execute @ end of animation system.out.println("timeout"); root.setdisable(true); }, new keyvalue(bar.progressproperty(), 1)) ); timeline.play(); submit.setonaction((actionevent event) -> { // stop animation timeline.pause(); system.out.println("your name " + tf.gettext()); }); scene scene = new scene(root); primarystage.setscene(scene); primarystage.show(); }
Comments
Post a Comment