0

I have a question in java FXML, and I would appreciate if anyone can help me to find a solution. So here is what I want to do: I want to have a dynamic label which show the value of a variable like integer. I know that I can use the method setText() of label in order to show the value of the integer, but is there a way to set the label's text in a way that it automatically update itself as the integer changes in program ( without recalling setText() method over every time integer changes)?

Thanks so much

  • You should show that you have tried a code and you have a problem, this is not a please write me the code community – Ibo Sep 14 '17 at 16:42
  • I did not ask you to write me a code. I asked for ideas. Thanks – Radintern Sep 14 '17 at 17:29
  • This community is to work on codes, for ideas you have to choose a different community. On the top right corner, click the most right icon and you can search STACK EXCHANGE communities – Ibo Sep 14 '17 at 19:19

1 Answers1

1

It sounds like what your looking for is a binding of your label to a int type variable. You can accomplish this with

labelFXid.textProperty().bind(value.asString());

where value is an IntegerProperty declared in your controller class and labelFXid is the FXid assigned to your label in the FXML document. You can find a good example of this from Uluk Biy's solution to a similar question. In that example, he was binding a label directly from the FXML file. I had better luck keeping the code in the controller as in the example below.

FXMLDocument.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="labelupdater.FXMLDocumentController">
    <children>
        <Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
        <Label fx:id="label" alignment="CENTER" layoutX="126" layoutY="120" minHeight="16" minWidth="69">
         <font>
            <Font size="24.0" />
         </font></Label>
    </children>
</AnchorPane>

FXMLDocumentController.java

package labelupdater;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class FXMLDocumentController implements Initializable {

    private IntegerProperty counter;

    public int getCounter() {
        return counter.get();
    }

    public void setCounter(int value) {
        counter.set(value);
    }

    public IntegerProperty counterProperty() {
        return counter;
    }

   @FXML
    private Label label;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        setCounter(getCounter() + 1);
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        counter = new SimpleIntegerProperty(0);
        label.textProperty().bind(counter.asString());
    }
}

The end result is that every time the button is clicked setCounter(getCounter() + 1) increments the value of the variable counter by one and the label is automatically updated to display the new value. The counter variable and binding are declared in the initialize of FXMLDocumentController.

edit:typo correction

Gash
  • 26
  • 3