I have a javafx project that is supposed to grab information through tcp pings from a server and constantly update a label. I used the JFX class Scheduled Service to implement this feature, and I tested by having it increment a number every 5 seconds, and that worked 100% as it should. However when I tried the same thing with a tcp ping to fetch information from the server, the server does recognize the message and responds but my label just goes blank. Here is the method in my jfx controller class
{ swissservice sak = new swissservice( "getquestion", gameidtext.getText(), placeholder, placeholder,3335);
sak.setExecutor(ThreadPoolExecutor1);
sak.setPeriod(Duration.seconds(1));
cardfinal.textProperty().bind(sak.valueProperty());}
I've tried using both bind and settext (only settext() worked on my original test for the incrementing ), I've also tried changing up what it binds/settext's to by changing it to getvalue/messageproperty and a couple of others.
Here is the code needed for the MRE
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Teodorant Insanity Topic Simulator ");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package sample;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.util.Duration;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;
public class swissservice extends ScheduledService<String> {
int socket1;
String a1;
String b1;
String c1;
String d1;
String delimiter = "spergzilion";
public swissservice(String a, String b, String c, String d, int socket) {
this.a1 = a;
this.b1 = b;
this.c1 = c;
this.d1 = d;
this.socket1 = socket;
}
protected Task<String> createTask() {
return new Task<String>() {
protected String call() throws Exception {
{
String fatman = a1 + delimiter + b1 + delimiter + c1 + delimiter + d1;
System.out.println(fatman);
try (Socket s = new Socket("149.28.232.148", socket1);
// try (Socket s = new Socket("localhost", socket1);
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
) {
try {
// s.setSoTimeout(5000);
dout.writeUTF(fatman);
dout.flush();
System.out.println("1");
updateValue(din.readUTF());
System.out.println(getValue());
System.out.println("4");
try {
s.close();
din.close();
dout.close();
} catch (IOException e) {
e.printStackTrace();
}
return getValue();
} catch (EOFException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return getValue();
}
}
};
}
public int getSocket1() {
return socket1;
}
public void setSocket1(int socket1) {
this.socket1 = socket1;
}
public String getA1() {
return a1;
}
public void setA1(String a1) {
this.a1 = a1;
}
public String getB1() {
return b1;
}
public void setB1(String b1) {
this.b1 = b1;
}
public String getC1() {
return c1;
}
public void setC1(String c1) {
this.c1 = c1;
}
public String getD1() {
return d1;
}
public void setD1(String d1) {
this.d1 = d1;
}
}
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.util.Duration;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.Scanner;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class Controller implements Initializable {
@FXML
public Button downloaderOfCards;
@FXML
public Button BatchUpload;
@FXML
public TextField FileLocation1;
@FXML
public TextArea Creator1;
@FXML
public TextArea Destination1;
@FXML
public Label cardfinal;
@FXML
public Label scores;
@FXML
public Label answers;
@FXML
public TextField votetarget;
@FXML
public TextField gameidtext;
@FXML
public Button answerbutton;
@FXML
public Button sendstuff;
@FXML
public Button votebutton;
@FXML
public Button joingame;
@FXML
public Button creategame;
@FXML
public TextField username;
@FXML
public TextField personalanswer;
@FXML
public Button update1;
// SqlMinion Sqlminion1 = new SqlMinion();
ThreadPoolExecutor ThreadPoolExecutor1 = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
// ThreadPoolExecutor1.submit(() -> { ;});
// RNGjesus Ron1 = new RNGjesus();
// Courier lonecourier = new Courier();
String placeholder = "placeholder";
Binder binder1= new Binder();
@FXML
private void Paloki(ActionEvent event) throws Exception
{ swissservice sak = new swissservice( gameidtext.getText(), username.getText(), personalanswer.getText(), votetarget.getText(), 3894);
sak.setExecutor(ThreadPoolExecutor1);
sak.setPeriod(Duration.seconds(5));
// cardfinal.textProperty().bind(sak.lastValueProperty());
sak.setOnSucceeded( e -> {cardfinal.setText(sak.getValue());
sak.setA1(gameidtext.getText());
sak.setB1(username.getText());
sak.setC1(personalanswer.getText());
sak.setD1(votetarget.getText());
});
sak.start();
}
@FXML
private void createGame(ActionEvent event) throws Exception {
{
try{Courier task1 = new Courier("addsession", gameidtext.getText(), username.getText(), "Paloki",3333);
Thread thread1 = new Thread(task1);
// cardfinal.textProperty().bind(task1.valueProperty());
// task1.setOnSucceeded( e -> {cardfinal.textProperty().unbind();});
thread1.start();
Thread.sleep(1);}
catch (Exception e) {e.printStackTrace();}
try {
// cardfinal.textProperty().bind(task1.valueProperty());
// task1.setOnSucceeded( e -> {cardfinal.textProperty().unbind();});
{ swissservice sak = new swissservice( "getquestion", gameidtext.getText(), placeholder, placeholder,3335);
sak.setExecutor(ThreadPoolExecutor1);
sak.setPeriod(Duration.seconds(1));
cardfinal.textProperty().bind(sak.valueProperty());
}
}
catch (Exception e) {e.printStackTrace();}
}
}
@FXML
private void get(ActionEvent event) throws Exception
{Courier task1 = new Courier("getanswers", gameidtext.getText(), placeholder, placeholder,3334);
Thread thread1 = new Thread(task1);
answers.textProperty().bind(task1.valueProperty());
task1.setOnSucceeded( e -> {answers.textProperty().unbind();});
thread1.start();
Courier task2 = new Courier("getquestion", gameidtext.getText(), placeholder, placeholder,3335);
Thread thread2 = new Thread(task2);
cardfinal.textProperty().bind(task2.valueProperty());
task2.setOnSucceeded( e -> {cardfinal.textProperty().unbind();});
thread2.start();
Courier task3 = new Courier("getscore", gameidtext.getText(), placeholder, placeholder,3336);
Thread thread3 = new Thread(task3);
scores.textProperty().bind(task3.valueProperty());
task3.setOnSucceeded( e -> {scores.textProperty().unbind();});
thread3.start();}
@FXML
private void set(ActionEvent event) throws Exception
{Courier task1 = new Courier("vote", gameidtext.getText(), username.getText(), votetarget.getText(),3337);
Thread thread1 = new Thread(task1);
thread1.setDaemon(true);
thread1.start();
votetarget.setText(placeholder);
Courier task2 = new Courier("answer", gameidtext.getText(), username.getText(), personalanswer.getText(),3338);
Thread thread2 = new Thread(task2);
thread2.setDaemon(true);
thread2.start();
personalanswer.setText(placeholder);
}
@FXML
private void joingame(ActionEvent event) throws Exception {
{
try {
Courier task1 = new Courier("addplayer", gameidtext.getText(), username.getText(), "Paloki",3333);
Thread thread1 = new Thread(task1);
thread1.start();
} catch (Exception e) {
e.printStackTrace();
}
{ Courier task1 = new Courier("getanswers", gameidtext.getText(), placeholder, placeholder,3334);
Thread thread1 = new Thread(task1);
answers.textProperty().bind(task1.valueProperty());
task1.setOnSucceeded( e -> {answers.textProperty().unbind();});
thread1.start();
Courier task2 = new Courier("getquestion", gameidtext.getText(), placeholder, placeholder,3335);
Thread thread2 = new Thread(task2);
cardfinal.textProperty().bind(task2.valueProperty());
task2.setOnSucceeded( e -> {cardfinal.textProperty().unbind();});
thread2.start();
Courier task3 = new Courier("getscore", gameidtext.getText(), placeholder, placeholder,3336);
Thread thread3 = new Thread(task3);
scores.textProperty().bind(task3.valueProperty());
task3.setOnSucceeded( e -> {scores.textProperty().unbind();});
thread3.start();}
}
}
@FXML
private void vote(ActionEvent event) throws Exception {
{Courier task1 = new Courier("vote", gameidtext.getText(), username.getText(), votetarget.getText(),3337 );
Thread thread1 = new Thread(task1);
thread1.start();
votetarget.setText(placeholder);}
{ Courier task4 = new Courier("getanswers", gameidtext.getText(), placeholder, placeholder,3334);
Thread thread4 = new Thread(task4);
answers.textProperty().bind(task4.valueProperty());
task4.setOnSucceeded( e -> {answers.textProperty().unbind();});
thread4.start();
Courier task2 = new Courier("getquestion", gameidtext.getText(), placeholder, placeholder,3335);
Thread thread2 = new Thread(task2);
cardfinal.textProperty().bind(task2.valueProperty());
task2.setOnSucceeded( e -> {cardfinal.textProperty().unbind();});
thread2.start();
Courier task3 = new Courier("getscore", gameidtext.getText(), placeholder, placeholder,3336);
Thread thread3 = new Thread(task3);
scores.textProperty().bind(task3.valueProperty());
task3.setOnSucceeded( e -> {scores.textProperty().unbind();});
thread3.start();}
}
@FXML
private void answer(ActionEvent event) throws Exception {
{Courier task1 = new Courier("answer", gameidtext.getText(), username.getText(), personalanswer.getText(),3338);
Thread thread1 = new Thread(task1);
thread1.start();
personalanswer.setText(placeholder);
{ Courier task4 = new Courier("getanswers", gameidtext.getText(), placeholder, placeholder,3334);
Thread thread4 = new Thread(task4);
answers.textProperty().bind(task4.valueProperty());
task4.setOnSucceeded( e -> {answers.textProperty().unbind();});
thread4.start();
Courier task2 = new Courier("getquestion", gameidtext.getText(), placeholder, placeholder,3335);
Thread thread2 = new Thread(task2);
cardfinal.textProperty().bind(task2.valueProperty());
task2.setOnSucceeded( e -> {cardfinal.textProperty().unbind();});
thread2.start();
Courier task3 = new Courier("getscore", gameidtext.getText(), placeholder, placeholder,3336);
Thread thread3 = new Thread(task3);
scores.textProperty().bind(task3.valueProperty());
task3.setOnSucceeded( e -> {scores.textProperty().unbind();});
thread3.start();}
}}
@FXML
private void uploadcards(ActionEvent event) throws Exception {
File file1 = new File(FileLocation1.getText());
Scanner sc = new Scanner(file1);
StringBuilder Bonerstorm = new StringBuilder();
while (sc.hasNextLine()) {
Bonerstorm.append(sc.nextLine());
Bonerstorm.append("yormungandryormungandr");
}
try {Courier task1 = new Courier(Destination1.getText(), Creator1.getText(), String.valueOf(Bonerstorm) ,votetarget.getText(),3337);
Thread thread1 = new Thread(task1);
thread1.start();
} catch (Exception e) {
e.printStackTrace();
}
// cardfinal.setText("abraca dabra");
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
}
package sample;
import javafx.concurrent.Task;
import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Courier extends Task<String> {
private final String mode1;
private final String gameid1;
private final String payload1;
private final String judge1;
private final int socket1;
public final String delimiter = "spergzilion";
public Courier(String mode, String gameid, String payload, String judge, int socket) {
this.mode1 = mode;
this.gameid1 = gameid;
this.payload1 = payload;
this.judge1 = judge;
this.socket1 = socket;
}
@Override
protected String call() throws Exception {
return sendmessagepaloki(mode1, gameid1, payload1, judge1);
}
public String sendmessagepaloki
(String mode, String gameid, String payload, String judge)
throws Exception {
String fatman = mode + delimiter + gameid + delimiter + payload + delimiter + judge;
System.out.println(fatman);
try (Socket s = new Socket("149.28.232.148", socket1);
// try (Socket s = new Socket("localhost", socket1);
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
) {
try {
// s.setSoTimeout(5000);
dout.writeUTF(fatman);
dout.flush();
System.out.println("1");
updateValue(din.readUTF());
System.out.println(getValue());
System.out.println("4");
try {
s.close();
din.close();
dout.close();
} catch (IOException e) {
e.printStackTrace();
}
return getValue();
} catch (EOFException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return getValue();
}
}
package sample;
public class fakemain {
public static void main (String[] args) {
sample.Main.main(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<AnchorPane prefHeight="699.0" prefWidth="977.0" GridPane.columnIndex="1">
<children>
<Button fx:id="downloaderOfCards1" layoutX="22.0" layoutY="575.0" mnemonicParsing="false" onAction="#Paloki" prefHeight="37.0" prefWidth="345.0" text="Count - 0" />
<TextField fx:id="FileLocation1" alignment="CENTER" layoutX="21.0" layoutY="660.0" prefHeight="25.0" prefWidth="650.0" text="Write/Copy paste the location of the folder you want to upload to the server. Use \\ instead of \ for the file location" />
<Button fx:id="BatchUpload1" layoutX="21.0" layoutY="620.0" minWidth="50.0" mnemonicParsing="false" onAction="#uploadcards" prefHeight="25.0" prefWidth="107.0" text="Batch Upload" />
<TextArea fx:id="Creator1" layoutX="545.0" layoutY="615.0" prefHeight="37.0" prefWidth="225.0" text="Type in the name of the card creator" />
<Label fx:id="cardfinal" alignment="CENTER" layoutX="27.0" layoutY="8.0" prefHeight="167.0" prefWidth="908.0" text="Press "Setup Decks" to shuffle the decks " textAlignment="JUSTIFY" wrapText="true">
<font>
<Font size="20.0" />
</font>
</Label>
<TextArea fx:id="Destination1" layoutX="156.0" layoutY="614.0" prefHeight="38.0" prefWidth="381.0" text="Type s or q depending if you are uploading a situation or a question" />
<Button fx:id="update1" layoutX="804.0" layoutY="608.0" mnemonicParsing="false" onAction="#get" prefHeight="25.0" prefWidth="118.0" text="update results" />
<Label fx:id="scores" alignment="CENTER" layoutX="424.0" layoutY="174.0" prefHeight="319.0" prefWidth="522.0" text="scores go here" />
<Label fx:id="answers" alignment="CENTER" layoutX="27.0" layoutY="172.0" prefHeight="386.0" prefWidth="381.0" text="answers go here" />
<TextField fx:id="username" layoutX="795.0" layoutY="562.0" promptText="username" />
<TextField fx:id="votetarget" layoutX="423.0" layoutY="515.0" promptText="votetarget" />
<TextField fx:id="gameidtext" layoutX="423.0" layoutY="562.0" promptText="gameidtext" />
<Button fx:id="answerbutton" layoutX="681.0" layoutY="515.0" mnemonicParsing="false" onAction="#answer" prefHeight="25.0" prefWidth="107.0" text="answerbutton>" />
<Button fx:id="joingame" layoutX="685.0" layoutY="562.0" mnemonicParsing="false" onAction="#joingame" prefHeight="25.0" prefWidth="107.0" text="<joingame>" />
<Button fx:id="creategame" layoutX="582.0" layoutY="562.0" mnemonicParsing="false" onAction="#createGame" prefHeight="25.0" prefWidth="97.0" text="<creategame>" />
<Button fx:id="votebutton" layoutX="582.0" layoutY="515.0" mnemonicParsing="false" onAction="#vote" prefHeight="25.0" prefWidth="89.0" text="<votebutton" />
<TextField fx:id="personalanswer" layoutX="795.0" layoutY="515.0" promptText="personalanswer" />
<Button fx:id="sendstuff" layoutX="804.0" layoutY="648.0" mnemonicParsing="false" onAction="#set" prefHeight="38.0" prefWidth="118.0" text="sendstuff" />
</children>
</AnchorPane>
</children>
</GridPane>