-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialogExample.java
More file actions
150 lines (116 loc) · 3.79 KB
/
Copy pathDialogExample.java
File metadata and controls
150 lines (116 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package application;
import java.util.Optional;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
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.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class DialogExample
extends Application {
private Text actionStatus;
private static final String titleTxt = "JavaFX Dialogs Example";
public static void main(final String [] args) {
Application.launch(args);
}
@Override
public void start(final Stage primaryStage) {
primaryStage.setTitle(titleTxt);
// Window label
final Label label = new Label("A Dialog");
label.setTextFill(Color.DARKBLUE);
label.setFont(Font.font("Calibri", FontWeight.BOLD, 36));
final HBox labelHb = new HBox();
labelHb.setAlignment(Pos.CENTER);
labelHb.getChildren().add(label);
// Button
final Button btn = new Button("Click to Show Dialog");
btn.setOnAction(new DialogButtonListener());
final HBox buttonHb = new HBox(10);
buttonHb.setAlignment(Pos.CENTER);
buttonHb.getChildren().addAll(btn);
// Status message text
this.actionStatus = new Text();
this.actionStatus.setFont(Font.font("Calibri", FontWeight.NORMAL, 20));
this.actionStatus.setFill(Color.FIREBRICK);
// Vbox
final VBox vbox = new VBox(30);
vbox.setPadding(new Insets(25, 25, 25, 25));;
vbox.getChildren().addAll(labelHb, buttonHb, this.actionStatus);
// Scene
final Scene scene = new Scene(vbox, 500, 250); // w x h
primaryStage.setScene(scene);
primaryStage.show();
}
private class DialogButtonListener implements EventHandler<ActionEvent> {
@Override
public void handle(final ActionEvent e) {
DialogExample.this.displayDialog();
}
}
private void displayDialog() {
this.actionStatus.setText("");
// Custom dialog
final Dialog<PhoneBook> dialog = new Dialog<>();
dialog.setTitle(titleTxt);
dialog.setHeaderText("This is a dialog. Enter info and \n" +
"press Okay (or click title bar 'X' for cancel).");
dialog.setResizable(true);
// Widgets
final Label label1 = new Label("Name: ");
final Label label2 = new Label("Phone: ");
final TextField text1 = new TextField();
final TextField text2 = new TextField();
// Create layout and add to dialog
final GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 35, 20, 35));
grid.add(label1, 1, 1); // col=1, row=1
grid.add(text1, 2, 1);
grid.add(label2, 1, 2); // col=1, row=2
grid.add(text2, 2, 2);
dialog.getDialogPane().setContent(grid);
// Add button to dialog
final ButtonType buttonTypeOk = new ButtonType("Okay", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().add(buttonTypeOk );
// Result converter for dialog
dialog.setResultConverter(b -> {
if (b == buttonTypeOk)
return new PhoneBook(text1.getText(), text2.getText());
return null;
});
// Show dialog
final Optional<PhoneBook> result = dialog.showAndWait();
if (result.isPresent()) {
this.actionStatus.setText("Result: " + result.get());
}
}
private class PhoneBook {
private final String name;
private final String phone;
PhoneBook(final String s1, final String s2) {
this.name = s1;
this.phone = s2;
}
@Override
public String toString() {
return (this.name + ", " + this.phone);
}
}
}