JavaFX | TextInputDialog
TextInputDialog is a part of JavaFX library. TextInputDialog is a dialog that allows the user to enter a text, and the dialog contains a header text, a TextField and confirmation buttons.
Constructor of the TextInputDialog class are:
- TextInputDialog(): creates a text input dialog with no initial text.
- TextInputDialog(String txt): creates a text input dialog with initial text txt.
Commonly used methods:
| method | explanation |
|---|---|
| getDefaultValue() | returns the default value of the text input dialog |
| getEditor() | returns the editor of the text input dialog |
| setHeaderText(String s) | sets the header text of the header of text input dialog |
Below programs illustrate the TextInputDialog class:
- Program to create a TextInputDialog and add it to the stage: This program creates a TextInputDialog with an initial text and a header text. The header text is set using setHeaderText() function. Button is indicated by the name d and text input dialog will have name td. The button will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button inside the scene. Finally, the show() method is called to display the final results. The TextInputDialog will be shown on the click of the button.
// Java Program to create a text input// dialog and add it to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.control.Alert.AlertType;importjava.time.LocalDate;publicclassTextInputDialog_1extendsApplication {// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating textInput dialog");// create a tile paneTilePane r =newTilePane();// create a text input dialogTextInputDialog td =newTextInputDialog("enter any text");// setHeaderTexttd.setHeaderText("enter your name");// create a buttonButton d =newButton("click");// create a event handlerEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// show the text input dialogtd.show();}};// set on action of eventd.setOnAction(event);// add button and labelr.getChildren().add(d);// create a sceneScene sc =newScene(r,500,300);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

- Program to create a TextInputDialog and add a label to display the text entered: This program creates a TextInputDialog (td). Button indicated by the name d and TextInputDialog will have name td. The button will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button inside the scene. Finally, the show() method is called to display the final results.when the button will be clicked the text input dialog will be shown. A label named l will be created that will be added to the scene which will show the text that the user inputs in the dialog.
// Java Program to create a text input dialog// and add a label to display the text enteredimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.control.Alert.AlertType;importjava.time.LocalDate;publicclassTextInputDialog_2extendsApplication {// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating textInput dialog");// create a tile paneTilePane r =newTilePane();// create a label to show the input in text dialogLabel l =newLabel("no text input");// create a text input dialogTextInputDialog td =newTextInputDialog();// create a buttonButton d =newButton("click");// create a event handlerEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// show the text input dialogtd.showAndWait();// set the text of the labell.setText(td.getEditor().getText());}};// set on action of eventd.setOnAction(event);// add button and labelr.getChildren().add(d);r.getChildren().add(l);// create a sceneScene sc =newScene(r,500,300);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

- JavaFX | Checkbox
- JavaFX | Tooltip
- JavaFX | ChoiceBox
- JavaFX | CustomMenuItem
- JavaFX | Tab Class
- JavaFX | ChoiceDialog
- JavaFX | MenuButton
- JavaFX | Box with examples
- JavaFX | ProgressIndicator
- JavaFX | ProgressBar
- JavaFX | Label
- JavaFx | PasswordField
- JavaFX | TextField
- JavaFX | Pos Class
- JavaFX | Arc with examples
Note : The above programs might not run in an online IDE please use an offline IDE.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextInputDialog.html
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ManasChhabra2

