Using JavaFX Create an application that has a button that has “Enter your info.”
ID: 3587803 • Letter: U
Question
Using JavaFX
Create an application that has a button that has “Enter your info.” When clicked it will present a dialog that has labels and text boxes that allow the user to enter their name, email, and phone number. The dialog will have buttons OK and Cancel.
When the user clicks Cancel, the dialog will go away without doing anything else.
When the user clicks OK, the user’s information will be extracted from the dialog, and dumped out on the console.
Notice that you only are listening for Click events.
Explanation / Answer
Hi,
There is lots of file in these JavaFx but i write only java class
you can download zip file.from here
https://drive.google.com/open?id=0BxRUEyIaVxQ3TkgyUUJ1QVBIV3c
package javafxdialog;
import java.util.Optional;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;
/**
*
* @author Developer
*/
public class JavaFXDialog extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Enter your info");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
showDialog();
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("JavaFX Dialog Box");
primaryStage.setScene(scene);
primaryStage.show();
}
public void showDialog() {
Dialog<User> dialog = new Dialog<>();
dialog.setTitle("User Info");
dialog.setResizable(false);
Label label1 = new Label("Name: ");
Label label2 = new Label("Email: ");
Label label3 = new Label("Phone No: ");
TextField text1 = new TextField();
TextField text2 = new TextField();
TextField text3 = new TextField();
GridPane grid = new GridPane();
grid.add(label1, 1, 1);
grid.add(text1, 2, 1);
grid.add(label2, 1, 2);
grid.add(text2, 2, 2);
grid.add(label3, 1, 3);
grid.add(text3, 2, 3);
dialog.getDialogPane().setContent(grid);
ButtonType buttonTypeOk = new ButtonType("OK", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(buttonTypeOk, ButtonType.CANCEL);
dialog.setResultConverter(new Callback<ButtonType, User>() {
@Override
public User call(ButtonType b) {
if (b == buttonTypeOk) {
return new User(text1.getText(), text2.getText(), text2.getText());
}
return null;
}
});
Optional<User> result = dialog.showAndWait();
if (result.isPresent()) {
System.out.println("Result:" + result.get());
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public class User {
private String name;
private String email;
private String phoneNo;
public User(String name, String email, String phoneNo) {
this.name = name;
this.email = email;
this.phoneNo = phoneNo;
}
@Override
public String toString() {
return "Name: "+name+" Email: "+email+" Phone Number: "+phoneNo; //To change body of generated methods, choose Tools | Templates.
}
}
}
thank you
let me know if any change required
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.