Thursday, 7 June 2018

JavaFX: Create TextField widget

TextField class provides below constructors to create TextField widget.

public TextField()
public TextField(String text)

Ex
Label name = new Label("Enter Your Name:");
TextField nameField = new TextField();

Find the below working example.

TextFieldApp.java
package com.sample.demos;

import java.io.FileNotFoundException;
import java.net.MalformedURLException;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TextFieldApp extends Application {

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException {
  Label name = new Label("Enter Your Name:");
  TextField nameField = new TextField();
  HBox hBox1 = new HBox(40, name, nameField);
  
  Label phoneNumber = new Label("Enter Your PhoneNumber:");
  TextField phoneNumberField = new TextField();
  HBox hBox2 = new HBox(40, phoneNumber, phoneNumberField);

  VBox vbox = new VBox(10, hBox1, hBox2);

  Scene scene = new Scene(vbox, 500, 300);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX Text Field Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(TextFieldApp.class, args);
 }
}





Previous                                                 Next                                                 Home

No comments:

Post a Comment