Ramesh KC
Ramesh KC

Reputation: 582

How can I get value from label text field which is dynamically changes JavaFX

<Label fx:id="lblLibrarianId" layoutX="82.0" layoutY="14.0" prefHeight="24.0"  prefWidth="212.0" text="$librarianId" />

I have a controller name LibraryController. I set label value text to librarianId dynamically from another controller. Now, I want to access this librarianId to LibraryController.

final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../fxmlfile/librarian.fxml"));
        fxmlLoader.getNamespace().put("librarianId", librarianId);

This is the way how I set the value dynamically to Label text field. Now I want to retrieve that Label Text value to my LibraryController.

Upvotes: 0

Views: 3679

Answers (1)

Nevets17
Nevets17

Reputation: 109

In your controller, create your label object, and call getText() you'll need to use the @FXML to associate that object with the fx:id in your .fxml file

ex.

public class LibraryController{

     @FXML public Label lblLibrarianId;
     public String librarianID;

     final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../fxmlfile/librarian.fxml"));
     fxmlLoader.getNamespace().put("librarianId", librarianId);


     librarianId = lblLibrarianId.getText();


}

Upvotes: 1

Related Questions