JavaFX - TextFields and Labels


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);
    }
}

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post