Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javafx - how to acces FXML "objects"

Tags:

java

javafx

I have some JavaFX components declared in my fxml file.

The Frame

and how do I get the values of the fields (Username, Password) when the button is pressed? (To perform the login).

  Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
  stage.setTitle("Login");
  stage.setScene(new Scene(root,400,300));
  stage.show();

or is this the complete wrong way to do it?

My Questions:

  1. Is it a good idea to declare all fields in the fxml file?
  2. How do I get the objects to acces the Values?
  3. If its the complete wrong way, how can it be done? (I want to use scenebuilder)

EDIT : https://hastebin.com/qexipogoma.xml <- My FXML fie and my controller

like image 354
John E. Avatar asked Jan 19 '26 17:01

John E.


1 Answers

Scene scene = stage.getScene();
Button btn = (Button) scene.lookup("#myBtnID");
TextField txt = (TextField ) scene.lookup("#myTxtID");

you're looking for:

txt.getText();

and

btn.setOnAction( lambda here );

Documentation:

Button

TextField

EDIT: declare ids this way

<TextField fx:id="myTxtID" ...  />
like image 179
Laurent Schwitter Avatar answered Jan 22 '26 08:01

Laurent Schwitter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!