为什么textarea没有在javaFx中更新?

时间:2016-02-05 11:20:02

标签: java javafx

package client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ResourceBundle;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class HomeController implements Initializable {

    @FXML
    private Button btn;

    @FXML
    private TextArea txtarea;

    @FXML
    private TextField txtmsg;
    Socket sock;
    DataInputStream di;
    DataOutputStream dout;

    public void initialize(URL location, ResourceBundle resources) {

        try {
            sock = new Socket(InetAddress.getByName("localhost"), 8081);
            System.out.println("Client Started....");
            txtarea.setText("Client Started...");

            di = new DataInputStream(sock.getInputStream());
            dout = new DataOutputStream(sock.getOutputStream());

            btn.setOnAction(new EventHandler<ActionEvent>() {

                public void handle(ActionEvent event) {

                    try {
                        dout.writeUTF(txtmsg.getText().trim());
                        txtmsg.clear();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });

            Task<Void> t = new Task<Void>() {

                @Override
                protected Void call() throws Exception {
                    // TODO Auto-generated method stub

                    String msg = di.readUTF().trim();
                    System.out.println(msg);
                    updateMessage("\nServer says : " + msg);
                    txtarea.appendText("\nServer says : " + msg);
                    return null;
                }

            };
            new Thread(t).start();
            // Executors.newSingleThreadExecutor().submit(t);


        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

我在javaFX中创建聊天应用程序...上面的代码是为客户端.. 当服务器发送一些消息时,它不会出现在客户端textarea中。在控制台中收到消息。

你可以给我一些解决方案吗?

1 个答案:

答案 0 :(得分:0)

您正在更新背景线程中的TextArea,这是不正确的。您必须在Platform.runLater()内更新它,这将强制它在JavaFX应用程序线程上执行。

这是一个快速演示。注意:我在此示例中使用的是Service,因为它是reusable Worker

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        Button btn = new Button("Append");
        TextArea txtarea = new TextArea("Hi ");

        Service<Void> service = new Service<Void>() {
            @Override
            protected Task<Void> createTask() {
                return new Task<Void>() {
                    @Override
                    protected Void call() throws Exception {
                        Platform.runLater(() -> txtarea.appendText("message "));
                        return null;
                    }
                };
            }
        };

        btn.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                /*
                 *You can add conditions to check the status of 
                 *the service and execute proper methods.
                 */
                service.restart();
            }
        });

        VBox root = new VBox(txtarea, btn);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 500, 500);
        stage.setScene(scene);
        stage.show();
    }
}