0

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>
  • Are you looking to communicate between two different JVM instances? – Slaw Apr 06 '20 at 17:33
  • In the real scenario of my problem, I will update the data through a socket communication, where I will pass this data will be updated via the property file, but before implementing it in the real scenario, I wanted to make it work in an example like this, you have the possibility to update a Label through another class? – Kelvyn Cavalcante Apr 06 '20 at 18:02
  • From another class _in the same JVM_? Sure, that's no different than any other communication between object instances in any application. But between two JVMs? That requires something like sockets (just one option). – Slaw Apr 06 '20 at 18:04
  • If you're having an issue communicating with or between FXML controllers, check out [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml). – Slaw Apr 06 '20 at 18:09
  • In this example, it will be for another class, in the same JVM Instance. My big difficulty is knowing how to do this, I tried for Listener and Task, but to no avail. You manages to come show an example of how I could do this? @Slaw – Kelvyn Cavalcante Apr 06 '20 at 18:10
  • Check out the Q&A in my previous comment to see if that helps at all. – Slaw Apr 06 '20 at 18:13
  • I saw about the topic and I'm still stuck on how I update the Label at run time. I am able to work with the main Controller already from another class, so much so that when I run the JavaFX app, I get the text "Bye World" on the screen, but now I want this text to be changed without having to close the app, I need a way to update this text, either by a new class or by an external property file. I just can't update via a button or TextField. How do I do this? :( – Kelvyn Cavalcante Apr 06 '20 at 18:35
  • Is there any way I can refresh the screen so that it recharges with the new value? – Kelvyn Cavalcante Apr 06 '20 at 19:23

1 Answers1

0

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.