Repository navigation
Expand file tree
/
Copy pathJavaFXDemo10.java
More file actions
62 lines (47 loc) · 1.57 KB
/
Copy pathJavaFXDemo10.java
File metadata and controls
62 lines (47 loc) · 1.57 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
package javafxdemo10;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class JavaFXDemo10 extends Application{
Stage window;
public static void main(String[] args) {
//multiple windows
launch(args);
}
//Tutorial 10
//input and validation
@Override
public void start(Stage PrimaryStage) throws Exception{
window = PrimaryStage;
window.setTitle("JavaFX - The New Boston");
//Form
TextField nameInput = new TextField();
Button button = new Button("Click Me");
button.setOnAction(e -> {
isInt(nameInput, nameInput.getText());
});
//Layout
VBox layout = new VBox(10);
layout.setPadding(new Insets(20, 20, 20, 20));
layout.getChildren().addAll(nameInput, button);
Scene scene = new Scene(layout, 300, 250);
window.setScene(scene);
window.show();
}
private boolean isInt(TextField input, String message){
try{
int age = Integer.parseInt(input.getText());
System.out.println("User is " + age);
return true;
}
catch(NumberFormatException e){
System.out.println("Error: " + message + " is NaN");
return false;
}
}
}