Issue
how are you ? I've been looking for how to update a Label for one application for another application for days and I can't find it.
I have the main application where I have a layout with a Label:
package updatelistener;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
/**
*
* @author programmer
*/
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
private StringProperty line1 = new SimpleStringProperty(String.valueOf(Config.get("LINE_1")));
@Override
public void initialize(URL url, ResourceBundle rb) {
label.setText("Hello World!");
Updater.Connecting(this);
}
public void setLabelText(String text) {
System.out.println("CONTROLLER SET LABEL CHAMADO");
label.setText(text);
}
}
I have the secondary application, where I will update the Label of the main app:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package updatelistener;
import java.io.IOException;
/**
*
* @author programmer
*/
public class Updater {
static FXMLDocumentController controllerGlobal;
public static void Connecting(FXMLDocumentController controller) {
controllerGlobal = controller;
controller.setLabelText("Bye World");
}
public static void main(String[] args) throws IOException {
controllerGlobal.setLabelText("Update!!!");
}
}
When I run the first time, the label is updated correctly to "Bye World", but I would like to update the Label at run time with the value I typed there in the controllerGlobal.setLabelText ("Update !!!");
and when I ran the app, it would update the text value on the screen.
When I'm running the app to update the Label value, I'm getting a NullPointerException.
Can anyone help me with a solution for this scenario?
.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="updatelistener.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="100.0" layoutY="120" minHeight="16" minWidth="69" prefWidth="200.0" text="Saque">
<font>
<Font size="25.0" />
</font></Label>
</children>
</AnchorPane>
Solution
I got it
Task<Void> task = new Task<Void>() {
@Override
public Void call() throws Exception {
try {
new Thread(listner).start();
while (true) {
Thread.sleep(100);
//LINE1
line_1 = ApplicationConfiguration.getInstance()
.getConfiguration("LINE_1");
if (!oldLine_1.equals(line_1)) {
System.out.println("NEW VALUE[" + line_1 + "]");
updateMessage(line_1);
}
oldLine_1 = line_1;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
};
task.messageProperty().addListener((obs, oldMessage, newMessage) -> label.setText(newMessage));
new Thread(task).start();
ApplicationConfiguration.getInstance().getConfiguration("LINE_1");
It is reading my property file all the time and when it finds a new value it updates the variable value
.
Answered By - Kelvyn Cavalcante