【问题标题】:JavaFX task communicationJavaFX 任务通信
【发布时间】:2014-11-03 13:33:15
【问题描述】:

我正在尝试做一个无限循环来改变我的 UI。但直到循环结束我的用户界面不会改变。 我正在尝试使用任务,循环中的一个任务和另一个更改 UI 但我不知道如何传达任务。 在我的具体问题中,我正在执行“ping 127.0.0.1”,它返回给我一个无限的字符串。我可以在 System.out 中打印它,但不能在文本区域中打印

作品:

    BufferedReader reader = SSH.execute(“ping 127.0.0.1”);
    String linea;
    String outputText = "";
    while ((linea = reader.readLine()) != null) 
    {
        outputText = outputText + linea +"\n" ;
        System.out.println(linea);
    }
    //taTerminal.setText(outputText);

不起作用:

    BufferedReader reader = SSH.execute(“ping 127.0.0.1”);
    String linea;
    String outputText = "";
    while ((linea = reader.readLine()) != null) 
    {
        outputText = outputText + linea +"\n" ;
        taTerminal.setText(linea);
    }
    //taTerminal.setText(outputText);

【问题讨论】:

  • 这是在后台线程中执行的吗?

标签: javafx task jsch


【解决方案1】:

确保您在后台任务中执行循环。您可以使用TaskmessageProperty 来存储文本,因为它将在FX 应用程序线程上正确更新。然后,您可以绑定观察消息属性并在其更改时更新文本区域:

@FXML private void actionStart(ActionEvent event1)throws Exception
{
    final String cmd = tfCmd.getText();
    final Task<Void> task = new Task<Void>() 
    {
        @Override protected Void call() throws Exception 
        {
            BufferedReader reader = SSH.execute(cmd);

            String linea;
            String outputText = "";
            while ((linea = reader.readLine()) != null) 
            {
                System.out.println(linea);
                outputText = outputText + linea +"\n" ;
                updateMessage(outputText);

            }
            return null;
        }
    };

    task.messageProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> obs, String oldMessage, String newMessage) {
            taTerminal.setText(task.getMessage());
        }
    });

    Thread th = new Thread(task);
    th.start();
}

这适用于少量文本,但效率很低,因为您每次都要替换整个文本。更好的方法是使用Platform.runLater(...) 将文本附加到文本区域:

@FXML private void actionStart(ActionEvent event1)throws Exception
{
    final String cmd = tfCmd.getText();
    final Task<Void> task = new Task<Void>() 
    {
        @Override protected Void call() throws Exception 
        {
            BufferedReader reader = SSH.execute(cmd);

            String linea;
            while ((linea = reader.readLine()) != null) 
            {
                System.out.println(linea);
                final String text = linea + "\n" ;
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        taTerminal.appendText(text);
                    }
                });

            }
            return null;
        }
    };


    Thread th = new Thread(task);
    th.start();
}

【讨论】:

  • 谢谢,但如果我尝试这样做,它会显示一个错误:“从内部类引用的局部变量必须是最终的或有效的最终......”并且 javaFX 不允许我放置最终变量对于widjets
  • 查看更新。 (如果您仍然有编译错误,请说明是哪些行产生了这些错误。)
【解决方案2】:

我找到了解决方案,但不是最好的。我用了两个按钮:

@FXML private TextField tfCmd;
@FXML private TextArea taTerminal;
@FXML private Button btStart;
@FXML private Button btRefresh;

public Task<String> task; 

public FXML_ExecuteInfiniteController() 
{
    this.task = new Task<String>() 
    {
        @Override protected String call() throws Exception 
        {
            String cmd = tfCmd.getText();
            BufferedReader reader = SSH.execute(cmd);

            String linea;
            String outputText = "";
            while ((linea = reader.readLine()) != null) 
            {
                System.out.println(linea);
                outputText = outputText + linea +"\n" ;
                updateMessage(outputText);

            }
            return outputText;
        }
    };
}  

@FXML private void actionStart(ActionEvent event1)throws Exception
{
    Thread th = new Thread(task);
    th.start();
}

@FXML private void actionRefresh(ActionEvent event1)throws Exception
{
    taTerminal.setText(task.getMessage());
}   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    相关资源
    最近更新 更多