如何从其他线程向VBox添加AnchorPane?

时间:2018-12-30 22:38:04

标签: java multithreading javafx

我有问题。我有线程,wh正在等待来自套接字的传入消息:

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

public class Receiver implements Runnable {

    private Socket socket;
    private Thread thread;

    public Receiver(Socket socket){
        this.socket = socket;
        thread = new Thread(this, "Receiver");
        thread.start();
    }

    @Override
    public void run() {
        System.out.println("trfam ja, RECIVER");
        while(true) {
            try {
                DataInputStream input = new DataInputStream(socket.getInputStream());
                System.out.println(input.readUTF());

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

,我想将上述线程中的AnchorPane添加到VBox,wh在JavaFX主线程中,但是我做不到,因为我有错误。如何解决我的问题?我听说过时间轴和任务,但是我无法实现它。感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

我做了类似的事情: 收件人:

package logic;

import window.classes.Window;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;

public class Receiver implements Runnable {

    private Socket socket;
    private Thread thread;
    private Window window;

    public Receiver(Socket socket, Window window) {
        this.window = window;
        this.socket = socket;
        thread = new Thread(this, "Receiver");
        thread.start();
    }

    @Override
    public void run() {
        System.out.println("trfam ja, RECIVER");
        while(true) {
            try {
                if (socket.isConnected() && socket.getKeepAlive()) {
                    try {
                        DataInputStream input = new DataInputStream(socket.getInputStream());
                        String inputString = input.readUTF();
                        window.setSomebodysMessageString(inputString);
                        window.startThread();

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

和主要的JavaFX线程:

    package window.classes;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import logic.Receiver;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Window extends Application {

    private Socket socket;
    private String somebodysMessageString = "";

    @FXML
    private VBox vBoxLeftPanel;

    @FXML
    private Button buttonSend;

    @FXML
    private TextField textFieldForWriteMessages;

    @FXML
    private VBox vBoxForMessages;

    @FXML
    private void messageSent(ActionEvent event) throws IOException {
        if (!textFieldForWriteMessages.getText().replaceAll(" ", "").equals("") &&
                socket.isConnected()) {
            String message = textFieldForWriteMessages.getText();
            MyMessage myMessage = new MyMessage(message);

            FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/window/fxmls/myMessage.fxml"));
            loader.setController(myMessage);
            AnchorPane anchorPane = loader.load();
            vBoxForMessages.getChildren().add(anchorPane);
            myMessage.setText(message);
            textFieldForWriteMessages.setText("");

            DataOutputStream output = new DataOutputStream(socket.getOutputStream());
            output.writeUTF(message);
        }
    }



    @Override
    public void start(Stage primaryStage) throws Exception {
        socket = new Socket("localhost", 9999);
        Receiver receiver = new Receiver(socket, this);

        FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/window/fxmls/window.fxml"));
        loader.setController(this);
        AnchorPane anchorPane = loader.load();
        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void startThread() {
        ReceiverThread receivierThread = new ReceiverThread();
    }

    public void setSomebodysMessageString(String somebodysMessageString) {
        this.somebodysMessageString = somebodysMessageString;
    }



    protected class ReceiverThread {
        protected ReceiverThread() {
            Thread thread = new Thread(new Runnable() {
                @Override public void run() {
                    Platform.runLater(new Runnable() {
                        @Override public void run() {
                            SomebodysMessage somebodysMessage= new SomebodysMessage(somebodysMessageString);

                            FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/window/fxmls/somebodysMessage.fxml"));
                            loader.setController(somebodysMessage);
                            AnchorPane anchorPane = null;
                            try {
                                anchorPane = loader.load();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            vBoxForMessages.getChildren().add(anchorPane);
                            somebodysMessage.setText(somebodysMessageString);
                            textFieldForWriteMessages.setText("");
                        }
                    });
                }
            });
            thread.start();
        }
    }
}

一切正常。

如何检查套接字是否与服务器连接?当套接字正在连接时,此方法将返回true,而当套接字断开连接时,该方法将返回false