document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2. *bem10jfx.blogspot.com
  3.  */
  4. package threadsjfx_2;
  5.  
  6. import javafx.application.Application;
  7. import javafx.concurrent.Service;
  8. import javafx.concurrent.Task;
  9. import javafx.event.EventHandler;
  10. import javafx.scene.Group;
  11. import javafx.scene.Scene;
  12. import javafx.scene.control.Button;
  13. import javafx.scene.control.ProgressBar;
  14. import javafx.scene.input.MouseEvent;
  15. import javafx.scene.layout.HBox;
  16. import javafx.scene.layout.VBox;
  17. import javafx.scene.paint.Color;
  18. import javafx.scene.text.Text;
  19. import javafx.stage.Stage;
  20.  
  21. /**
  22.  *
  23.  * @author mabso
  24.  */
  25. public class ThreadsJfx_2 extends Application {
  26.  
  27.     String ashStr;
  28.     int bx;
  29.  
  30.     Text tx1, txt22, tx2, tx3, tx4;
  31.     Button btstart, btcancel, btrestart;
  32.  
  33.     ProgressBar pg = new ProgressBar();
  34.     Service<Integer> service;
  35.  
  36.     @Override
  37.     public void start(Stage primaryStage) {
  38.         Stage stage = new Stage();
  39.         Group gp = new Group();
  40.         Scene scene = new Scene(gp, 400, 400, Color.ALICEBLUE);
  41.  
  42.         stage.setScene(scene);
  43.         tx1 = new Text("Title :");
  44.         txt22 = new Text("Value de retorno");
  45.         tx2 = new Text("0");
  46.         tx3 = new Text("Loop:");
  47.         tx4 = new Text("Progress :");
  48.  
  49.         VBox vbcenter = new VBox(5);
  50.         VBox vbPote1 = new VBox(5);
  51.  
  52.         HBox hbPote1 = new HBox(5);
  53.         vbcenter.getChildren().addAll(vbPote1, hbPote1);
  54.         vbcenter.layoutXProperty().bind(scene.widthProperty().divide(3.0));
  55.         vbcenter.layoutYProperty().bind(scene.heightProperty().divide(3.0));
  56.        
  57.         vbPote1.getChildren().addAll(tx1, txt22, tx2, tx3, tx4, pg);
  58.         gp.getChildren().addAll(vbcenter);
  59.  
  60.         btstart = new Button("Start");
  61.         btcancel = new Button("Cancelar");
  62.         btrestart = new Button("Restart");
  63.         hbPote1.getChildren().addAll(btstart, btrestart, btcancel);
  64.  
  65.         btstart.setOnMouseClicked((MouseEvent event) -> {
  66.  
  67.             service = new Service() {
  68.                 @Override
  69.                 protected Task<Integer> createTask() {
  70.                     //retorno do taskstringk
  71.                     Task<Integer> task = new Task<Integer>() {
  72.                         @Override
  73.                         protected Integer call() throws Exception {
  74.                             System.out.println("Metodo.call() integer iniciado");
  75.                             tx4.setText("Progress :INICIALIZADO");
  76.                             for (int i = 0; i <= 10000; i++) {
  77.                                 System.out.println("cont" + i); //loop
  78.  
  79.                                 Thread.sleep(10);
  80.  
  81.                                 updateTitle("Title : aplication service ");
  82.  
  83.                                 updateValue(i);
  84.                                 if (i % 50 == 0) {// multiplus de 50
  85.                                     updateMessage("Loop:" + i);
  86.                                 }
  87.  
  88.                                 updateProgress(i, 10000);
  89.                             }
  90.  
  91.                             tx4.setText("Progress :FINALIZADO");
  92.                             System.out.println(".call()string finalizando");
  93.  
  94.                             return bx;// retorno nul
  95.                         }
  96.                     };
  97.                     pg.progressProperty().bind(task.progressProperty());
  98.  
  99.                     tx1.textProperty().bind(task.titleProperty());
  100.                     txt22.setText("Value");
  101.                     tx2.textProperty().bind(task.valueProperty().asString());
  102.                     tx3.textProperty().bind(task.messageProperty());
  103.  
  104.                    
  105.                     return task;
  106.  
  107.                 }
  108.             };
  109.  
  110.             service.start();
  111.  
  112.         });
  113.         btrestart.setOnMouseClicked(new EventHandler<MouseEvent>() {
  114.             @Override
  115.             public void handle(MouseEvent event) {
  116.                 service.restart();
  117.             }
  118.         });
  119.         btcancel.setOnMouseClicked(new EventHandler<MouseEvent>() {
  120.             @Override
  121.             public void handle(MouseEvent event) {
  122.                 service.cancel();
  123.                 tx4.setText("Progress :Serv.Cancelado");
  124.             }
  125.  
  126.         });
  127.         stage.show();
  128.     }
  129.  
  130.     public static void main(String[] args) {
  131.         launch(args);
  132.     }
  133.  
  134. }
  135.  
');