I am currently attempting to make a very basic class MyController, which takes the content of a TextField in an FXML created window and, when the button in the window is pressed, inserts its contents into a string which is printed to the terminal. Once this is done, it changes the contents of the TextField to "done!".
The code for the FXML file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="practice1.MyController">
<children>
<Button fx:id="HelloWorld" layoutX="30.0" layoutY="34.0" mnemonicParsing="false" onAction="#sayHello" text="Hello World" />
<TextField fx:id="message" layoutX="30.0" layoutY="77.0" />
</children>
</AnchorPane>
The only information of interest in this is that the TextField has the fx:id "message".
The code for my class MyController
is as follows:
package practice1;
import javafx.event.*;
import javafx.scene.control.TextField;
public class MyController {
private TextField message;
public void sayHello(ActionEvent event) {
System.out.print("You said \"");
System.out.print(message.getText());
System.out.println("\"");
message.setText("done!");
}
}
Your TextField in the controller is private so the loader cannot access it. Using javafx.fxml.FXML annotation as follows you can mark it as accessible to FXML.
@FXML
private TextField message;
You could alternatively just declare it as public.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.