L
L
Lord Drous2017-03-19 15:39:17
Java
Lord Drous, 2017-03-19 15:39:17

How to count from TextField in javaFx?

Hello, I started to understand javafx, there was a problem with reading information from a textfield and outputting it to a label, I implemented it myself without controller.java and fxml, but I would like to use them, I want to understand how the controller works, etc., so as not to write everything in main'e. Thanks in advance
1) this is how it works, without a controller and fxml, everything is done manually

@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) and when I do this, it does not work
<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>

a9a94bbb39a64e3995a3e8ab27465788.png
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);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rou1997, 2017-03-20
@Rou1997

Because you have a newly created JTextField, instead of "caught" from FXML:
Remove this.
In addition, in FXML you have id=tittel, which means that the field name must be tittel, not textField, otherwise it will not be picked up.
Are you going to write JavaFX for Android? Seriously?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question