JavaFX - 控制和并发

时间:2014-07-27 17:44:10

标签: javafx javafx-2

我有一个示例Hello World JavaFx。我正在使用Eclipse和eFxclipse插件。我的Eclipse是kepler,它是Eclipse 4.3.2版本,Java servion是Jdk1.7-045。

我尝试添加的是很少的并发代码,我只想更新示例中的按钮文本。这个后端任务是否可以与前期UI控件交互,例如按钮,场景?如果没有,我如何制作定制的后端任务,然后与UI控件进行交互?

提前致谢

package com.juhani.fx.exer;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javafx.application.Application;
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 HelloWorld extends Application{

    private static final short COREPOOLSIZE=2;
    private static final short MAXIMUMPOOLSIZE=2; 
    private static final int WORKQUEUECAPACITY=100;
    private static Logger log = LogManager.getLogger(
            HelloWorld.class.getName());

    private ExecutorService executors = new             ThreadPoolExecutor(COREPOOLSIZE,MAXIMUMPOOLSIZE,20,TimeUnit.MINUTES,new ArrayBlockingQueue<Runnable>(WORKQUEUECAPACITY));



    public static void main(String[] args) {
       LogMessage logMessage = new LogMessage("BEGIN",1.0,1,"HELLOWORLD");
       log.trace(logMessage.toString());
       launch(args);        

   }

   @Override
   public void start(final Stage primaryStage) throws InterruptedException {
      primaryStage.setTitle("Hello World!");
      final Button btn = new Button();
      btn.setText("Say 'Hello World'");
      btn.setOnAction(new EventHandler<ActionEvent>() {
          @Override
          public void handle(ActionEvent event) {
            System.out.println("Hello World!");
         }
       });

      final StackPane root = new StackPane();
      root.getChildren().add(btn);
      final Scene scene= new Scene(root,300,250);
      primaryStage.setScene(scene);
      primaryStage.show();

      Task<Boolean> task = new Task<Boolean>() {
         @Override 
         public Boolean call() {    
            for(int i=0;i<20;i++){
               btn.setText("First row\nSecond row "+i);
               primaryStage.show();
               try {
                  Thread.sleep(1000);
               } catch (InterruptedException e) {
                  log.error(new LogMessage("entering interruption",1.0,2,"exception").toString());                       
               }
            }
            return new Boolean(true);               
          }        
       };           
       executors.submit(task);      
    }   
}

2 个答案:

答案 0 :(得分:5)

这个答案专门讨论了Platform.runLater的使用。如果您正在使用Task,最好使用它提供的方法更新UI,如中所述 kleopatra's answer


要更新UI,您必须使用Javafx线程。

进入任何其他线程后,使用Platform.runLater()将这些数据更新回Javafx UI。一个工作示例可以在下面找到

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

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 HelloWorld extends Application {

    private static final short COREPOOLSIZE = 2;
    private static final short MAXIMUMPOOLSIZE = 2; 
    private static final int WORKQUEUECAPACITY = 100;

    private ExecutorService executors = new 
       ThreadPoolExecutor(COREPOOLSIZE, MAXIMUMPOOLSIZE, 20, TimeUnit.MINUTES,
                          new ArrayBlockingQueue<Runnable>(WORKQUEUECAPACITY));

    public static void main(String[] args) {
        launch(args);        
    }

    @Override
    public void start(final Stage primaryStage) throws InterruptedException {
        primaryStage.setTitle("Hello World!");
        final Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        final StackPane root = new StackPane();
        root.getChildren().add(btn);
        final Scene scene = new Scene(root, 300, 250);
        primaryStage.setScene(scene);
        primaryStage.show();

        Task<Boolean> task = new Task<Boolean>() {
            @Override 
            public Boolean call() {    
                final AtomicInteger i = new AtomicInteger(0);
                for( ; i.get() < 20; i.incrementAndGet()) {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            btn.setText("First row\nSecond row " + i);                      
                        }
                    });

                try {
                  Thread.sleep(1000);
                }
                catch (InterruptedException e) {}
            }
            return Boolean.valueOf(true);               
          }        
       };           
       executors.submit(task);      
    }   
}

有关详细信息,您可以浏览提供的链接here

答案 1 :(得分:2)

一个Task旨在与fx-application线程上的ui进行交互,以利用你应该按照设计使用的支持: - )

作为一般规则,您不得在任务的调用方法[*]中访问ui。相反,更新其中一个属性(消息,进度...)并将该属性绑定到您的UI。示例代码:

Task<Boolean> taskWithBinding = new Task<Boolean>() {
    @Override 
    public Boolean call() {    
        final AtomicInteger i = new AtomicInteger(0);
        for( ; i.get() < 20; i.incrementAndGet()) {
            updateMessage("First row\nSecond row " + i);                      
            try {
                Thread.sleep(1000);
            }
            catch (InterruptedException e) {
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;               
    }        
};           
btn.textProperty().bind(taskWithBinding.messageProperty());

[*]在other answer中列出了一个例外(将访问包装到runLater中)。这样做在技术上是正确的 - 但是你正在绕过任务的能力并且可以使用任意的Runnable ...