JavaFX - TextFields and Labels

April 14, 2018 Oclemy JavaFX TextField, JavaFX Label 0 minutes, 49 seconds

In this example we see a JavaFX example involving a Label and TextField objects.

We will arrange them using a gridpane.


import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.TextField;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class MyTextField extends Application {

    /*
    Entry point for javaFX application
     */
    @Override
    public void start(Stage stage) {

        Scene scene = new Scene(new Group(), 500, 250);
        stage.setTitle("JavaFX TextField");

        //instantiate TextFields
        TextField nameTxt = new TextField ();
        TextField propellantTxt = new TextField ();

        nameTxt.setText("Name");
        propellantTxt.setText("Propellant");

        GridPane grid = new GridPane();
        grid.setVgap(5);
        grid.setHgap(10);
        grid.setPadding(new Insets(5, 5, 5, 5));
        grid.add(new Label("Name: "), 0, 0);
        grid.add(nameTxt, 1, 0);
        grid.add(new Label("Propellant: "), 0, 1);
        grid.add(propellantTxt, 1, 1);

        Group root = (Group) scene.getRoot();
        root.getChildren().add(grid);
        stage.setScene(scene);
        stage.show();

    }

    /*
    Main method
     */
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Comments