Friday, 8 June 2018

JavaFx: TextField: Set prompt text

By using 'setPromptText' method of TextField object, you can set the prompt text

Ex
TextField userName = new TextField();
userName.setPromptText("Enter your name");

Find the below working example.

TestFieldApp.java
package com.sample.demos;

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

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class TextFieldApp extends Application {

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException {
  GridPane grid = new GridPane();
  grid.setPadding(new Insets(10, 10, 10, 10));
  grid.setVgap(5);
  grid.setHgap(5);

  TextField userName = new TextField();
  userName.setPromptText("Enter your name");
  GridPane.setConstraints(userName, 0, 0);
  grid.getChildren().add(userName);

  TextField password = new TextField();
  password.setPromptText("Enter password");
  GridPane.setConstraints(password, 0, 1);
  grid.getChildren().add(password);

  Button button = new Button("SignIn");
  GridPane.setConstraints(button, 0, 2);
  grid.getChildren().add(button);

  Scene scene = new Scene(grid, 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