0

I'm new with JavaFX and made a Textfield in the Scene Builder. But I don't find a tutorial where someone explains how to get the input from the Textfield. My code I called my Textfield Texfield(fx:id) and left the On Action empty can you tell me what I have to fill in those two boxes and than in my code?

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

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");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}



package sample;

import com.sun.javafx.scene.control.IntegerField;
import javafx.fxml.FXML;

import java.awt.*;
import java.awt.event.ActionEvent;

public class Controller {

}



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label layoutX="72.0" prefHeight="20.0" prefWidth="457.0" text="GUESSING GAME">
         <font>
            <Font name="Arial Black" size="48.0" />
         </font>
      </Label>
      <Label layoutX="72.0" layoutY="187.0" text="Your guess:">
         <font>
            <Font name="Arial" size="22.0" />
         </font>
      </Label>
      <Label layoutX="395.0" layoutY="187.0" text="Attempts:">
         <font>
            <Font name="Arial" size="22.0" />
         </font>
      </Label>
      <TextField fx:id="Textfield" layoutX="197.0" layoutY="188.0" prefHeight="25.0" prefWidth="66.0" />
      <Label layoutX="496.0" layoutY="188.0" text="0">
         <font>
            <Font name="Arial" size="22.0" />
         </font>
      </Label>
   </children>
</AnchorPane>
Mindmax
  • 49
  • 1
  • 7
  • 1
    time to read and understand a basic tutorial on fx (the javafx tag wiki has references).. it's not enough to use a code/fxml producing tool like scenebuilder, you need to _understand_ what it is doing .. also: your imports are whacky, don't mix swing/awt with fx! – kleopatra Jan 30 '20 at 11:48
  • Does this answer your question? [What are JavaFX, FXML and Scene Builder?](https://stackoverflow.com/questions/22433952/what-are-javafx-fxml-and-scene-builder) – fuggerjaki61 Jan 30 '20 at 15:27

1 Answers1

1

you need to add an id to the element in your fxml and then link it in the controller using @FXML the variable name must be the same name you set as the id.

And then you can access the text field or any UI element

Mouamle Hasan
  • 264
  • 1
  • 4
  • 14