IAmBlake IAmBlake - 1 year ago 218
Java Question

Add elements on TextFlow using external Thread in JavaFX

My program sends a message to the server which is shown in a window and receives a message from a window which is also added in the window of client.

But my program doesn't show the message received from the server though i added:

in accept thread.

What should i do?

public class NewClient extends Application {

Thread send;
Thread accept;

DatagramPacket pack;
DatagramSocket sock;
private List<String> str;
String name , sname;
int listeningPort;
InetAddress server_ip;

String sender;
ScrollPane sp = new ScrollPane();
TextFlow textFlow = new TextFlow();
TextField userTextField = new TextField();
TextField fTextField = new TextField();

private String message;
private String recip = null;
Button button = new Button("Send");

volatile boolean st = false;
Stage theStage;

public Scene Chat_Box() {

TextFlow textFlow = new TextFlow();
textFlow.setPadding(new Insets(10));
TextField textField = new TextField();
textField.setPrefSize(50, 30);
Button button = new Button("Send");
button.setPrefSize(80, 30);
VBox container = new VBox();
VBox box = new VBox();
box.getChildren().addAll(sp, textFlow);
container.setPadding(new Insets(10));
container.getChildren().addAll(box, new HBox(textField, button));
VBox.setVgrow(sp, Priority.ALWAYS);
VBox.setVgrow(textFlow, Priority.ALWAYS);


textField.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ENTER) {
button.setOnAction(e -> {
st = true;
Text text;
if (textFlow.getChildren().size() == 0) {
text = new Text(textField.getText());
message = textField.getText();
} else {
message = textField.getText();
text = new Text("\n" + textField.getText());
if (textField.getText().contains(":)")) {
ImageView imageView = new ImageView(
// Remove :) from text
text.setText(text.getText().replace(":)", " "));
textFlow.getChildren().addAll(text, imageView);
} else {
VBox vb = new VBox();
sp.setPrefSize(400, 300);
sp.vvalueProperty().bind((ObservableValue<? extends Number>) vb.heightProperty());
Scene scene2 = new Scene(container, 400, 300);
return scene2;

public void start(Stage stage) throws IOException {
this.str = getParameters().getRaw();
name = str.get(0);
listeningPort = Integer.parseInt(str.get(1));
server_ip = InetAddress.getByName(str.get(2));
theStage = stage;
Scene scene = Chat_Box();
send = new Thread() {

public void run() {
DatagramSocket sock = null;
try {
sock = new DatagramSocket();
} catch (SocketException ex) {
Logger.getLogger(NewClient.class.getName()).log(Level.SEVERE, null, ex);
while (true) {
InetAddress host = server_ip;
try {
if (st && ( message != null )) {
String in = message;
byte[] data = new byte[1024];
data = in.getBytes();
DatagramPacket sendPack = new DatagramPacket(data, data.length);
st = false;
} catch (Exception e) {

accept = new Thread() {

public void run() {
try {
sock = new DatagramSocket(listeningPort);
} catch (SocketException e) {
while (true) {
byte[] data = new byte[1024];
pack = new DatagramPacket(data, data.length);
try {
} catch (IOException e) {
String incoming = null;
try {
incoming = new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
Text text = new Text(incoming);

public static void main(String[] args) throws IOException {


Answer Source

You should use:


 Text text = new Text(incoming);


The SceneGraph is not allowed to be changed from other Threads . Only JavaFX main Thread can modify the SceneGraph.

So calling this method you send this Runnable Object to a QUEUEand it will be called from the JavaFX Main Thread to modify the SceneGraph.

What is the SceneGraph?

The GUI in JavaFX is constructed as a scene graph. A scene graph is a collection of visual elements,called nodes, arranged in a hierarchical fashion. A scene graph is built using the public JavaFX API.

How events are managed in JavaFX?

In JavaFX,event queues are managed by a single, operating system–level thread called JavaFX Application Thread. All user input events are dispatched on the JavaFX Application Thread. JavaFX requires that a live scene graph must be modified only on the JavaFX Application Thread.

Where should i use Platform.runLater(()->{....});

Every where you are modifying a live SceneGraph from an External Thread other than JavaFX Main Thread.

Extra Links:

How JavaFX application thread works?

Platform.runLater and Task in JavaFX

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download