简体   繁体   中英

How to add AnchorPane to VBox from other thread?

I have a problem. I have thread, whinch is waiting for incoming message from socket:

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();
            }
        }
    }
}

and I want to add AnchorPane from above thread to VBox, whinch is in main JavaFX thread, but I can't, because I have error. How to resolve my problem? I heard about Timeline and Tasks, but I can't implement it. Thanks for all help.

I did something like that: Receiver:

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();
            }
        }
    }
}

And main JavaFX thread:

    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();
        }
    }
}

and all is working.

How to check is socket connecting with server? This method would return true when socket is connecting and false when socket is disconnecting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM