Gbenga Ogunbule
Gbenga Ogunbule

Reputation: 21

how to submit a form automatically in javafx

I want create a cbt in javafx and I run into a problem of not knowing how to submit a form automatically if the time elapsed and may be one of the students is yet to finish the test. Also, I want to know how to disable a form in javafx

Upvotes: 0

Views: 443

Answers (1)

fabian
fabian

Reputation: 82461

Disabling a Node can be done by simply calling node.setDisable(true). Since children are also automatically disabled, a you could also do this for the parent of Nodes you want to disable, as long as there are no other children that should not be disabled.

A timeout can be easily be implemented using a 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 for 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 and stop timeout task
            future.cancel(false);
            System.out.println("Your name is " + tf.getText());
        }
    });

    Scene scene = new Scene(root);

    primaryStage.setScene(scene);
    primaryStage.show();
}

@Override
public void stop() throws Exception {
    executorService.shutdown();
}

If you want to show the time in the ui, a Timeline may be more suitable than a 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 at the 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 is " + tf.getText());
    });

    Scene scene = new Scene(root);

    primaryStage.setScene(scene);
    primaryStage.show();
}

Upvotes: 2

Related Questions