This discussion is archived
2 Replies Latest reply: Feb 5, 2013 10:48 AM by 893630 RSS

popup issue

893630 Newbie
Currently Being Moderated
Hi,
I have an interesting problem. I have a popup window with a button.
The button has a setOnAction method i.e.
  btn.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent arg0) {
          
          //step:1 
         btn.setDisable(true);
           
           //step:2
         some large work.
            
            //step:3
            popup.hide.
                              My expectation is when I click the btn , it should disable itself first and then go through the large work and ultimately popup hides/terminates.
          
          However, when I run the code, btn isn't disable in UI till the large work finishes. It acts as if step 2 has become step 1 and vice versa though on debugging I can see control passing throug step1 before step2. For some reason , UI is not updated till step 2 finishes.
          
          Can I get someInsight on this.


Thanks
  • 1. Re: popup issue
    shakir.gusaroff Expert
    Currently Being Moderated
    Hi. I would put step 2 in a separate thread:
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.concurrent.Task;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    
    public class TaskP extends Application {
    
        @Override
        public void start(Stage primaryStage) {
           final Button btn = new Button();
            btn.setText("Submit");
            btn.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    btn.setDisable(true);
                    Thread th = new Thread(createTask());
                    th.start();
                 
                }
            });
    
    
            StackPane root = new StackPane();
            root.getChildren().add(btn);
            Scene scene = new Scene(root, 300, 250);
            primaryStage.setScene(scene);
            primaryStage.show();
    
    
        }
    
        Task<Void> createTask() {
            Task<Void> task = new Task<Void>() {
                @Override
                protected Void call() throws Exception {
                    Thread.sleep(500);  
                    Platform.runLater(new Runnable() {
                        public void run() {
    
                            int iterations;
                            for (iterations = 0; iterations < 100000; iterations++) {
                                System.out.println("Iteration " + iterations);
                            }
                        }
                    });
    
                    return null;
                }
            };
    
            return task;
    
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
  • 2. Re: popup issue
    893630 Newbie
    Currently Being Moderated
    Thanks.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points