Здравствуйте, начал разбираться в javafx, возникла проблема со считываеним информации из textfield и вывод в label, сам реализовал без controller.java и fxml, но хотелось бы с ними, хочу понять как работает контроллер и т.д., чтобы не писать все в main'e. Заранее спасибо
1) вот так работает, без контроллера и fxml, все вручную
@Override
public void start(Stage stage) {
Group root = new Group();
Scene scene = new Scene(root, 300, 150);
stage.setScene(scene);
stage.setTitle("Text Field Sample");
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(5);
grid.setHgap(5);
scene.setRoot(grid);
final TextField name = new TextField();
name.setPromptText("Enter your first name.");
name.setPrefColumnCount(10);
name.getText();
GridPane.setConstraints(name, 0, 0);
grid.getChildren().add(name);
final TextField lastName = new TextField();
lastName.setPromptText("Enter your last name.");
GridPane.setConstraints(lastName, 0, 1);
grid.getChildren().add(lastName);
final TextField comment = new TextField();
comment.setPrefColumnCount(15);
comment.setPromptText("Enter your comment.");
GridPane.setConstraints(comment, 0, 2);
grid.getChildren().add(comment);
Button submit = new Button("Submit");
GridPane.setConstraints(submit, 1, 0);
grid.getChildren().add(submit);
Button clear = new Button("Clear");
GridPane.setConstraints(clear, 1, 1);
grid.getChildren().add(clear);
final Label label = new Label();
GridPane.setConstraints(label, 0, 3);
GridPane.setColumnSpan(label, 2);
grid.getChildren().add(label);
submit.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
if (
(comment.getText() != null && !comment.getText().isEmpty())
) {
label.setText(name.getText() + " " +
lastName.getText() + ", "
+ "thank you for your comment!");
} else {
label.setText("You have not left a comment.");
}
}
});
clear.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
name.clear();
lastName.clear();
comment.clear();
label.setText(null);
}
});
stage.show();
}
public static void main(String[] args) {
launch(args);
}
2) а когда делаю так, то не работает
<GridPane alignment="center" hgap="10" prefHeight="150.0" prefWidth="303.0" vgap="10" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
</rowConstraints>
<TextField id="tittel" fx:id="tittel" layoutX="120.0" layoutY="64.0" onAction="#handleButton1Action" promptText="Tittel" GridPane.rowIndex="1" />
<Button id="button" fx:id="button1" layoutX="100.0" layoutY="50.0" onAction="#handleButton1Action" prefHeight="25.0" prefWidth="70.0" text="Click me" GridPane.rowIndex="3" />
</GridPane>
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
Scene scene = new Scene(root, 300, 275, Color.RED);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}