|
| 1 | +package sample; |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2017 Jay Sridhar |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 7 | + * a copy of this software and associated documentation files (the |
| 8 | + * "Software"), to deal in the Software without restriction, including |
| 9 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | + * permit persons to whom the Software is furnished to do so, subject to |
| 12 | + * the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be |
| 15 | + * included in all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 21 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 22 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 23 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + * |
| 25 | + * @Author Jay Sridhar |
| 26 | + */ |
| 27 | + |
| 28 | +import java.util.Optional; |
| 29 | +import javafx.util.Pair; |
| 30 | +import javafx.scene.Node; |
| 31 | +import javafx.scene.control.Dialog; |
| 32 | +import javafx.scene.control.Label; |
| 33 | +import javafx.scene.control.Button; |
| 34 | +import javafx.scene.control.PasswordField; |
| 35 | +import javafx.scene.control.TextField; |
| 36 | +import javafx.scene.control.ButtonType; |
| 37 | +import javafx.scene.control.ButtonBar; |
| 38 | +import javafx.scene.control.DialogPane; |
| 39 | +import javafx.scene.layout.GridPane; |
| 40 | +import javafx.scene.layout.Priority; |
| 41 | +import javafx.scene.image.ImageView; |
| 42 | +import javafx.scene.image.Image; |
| 43 | +import javafx.geometry.Insets; |
| 44 | +import javafx.geometry.HPos; |
| 45 | +import javafx.application.Platform; |
| 46 | +import javafx.application.Application; |
| 47 | +import javafx.stage.Stage; |
| 48 | +import javafx.scene.Scene; |
| 49 | +import javafx.scene.Parent; |
| 50 | +import javafx.event.ActionEvent; |
| 51 | +import javafx.beans.value.ChangeListener; |
| 52 | + |
| 53 | +public class LoginDialog extends Application |
| 54 | +{ |
| 55 | + private Parent makeContents() |
| 56 | + { |
| 57 | + DialogPane root = new DialogPane(); |
| 58 | + root.setHeaderText("Enter Credentials"); |
| 59 | + root.setGraphic(new ImageView(getClass().getResource("/images/key.png").toString())); |
| 60 | + root.setPadding(new Insets(10, 30, 10, 30)); |
| 61 | + root.getStylesheets().add(getClass().getResource("/css/my.css").toString()); |
| 62 | + |
| 63 | + ButtonType okButton = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE); |
| 64 | + root.getButtonTypes().addAll(okButton, ButtonType.CANCEL); |
| 65 | + ((Button)root.lookupButton(ButtonType.CANCEL)).setOnAction(event -> Platform.exit()); |
| 66 | + Button button = ((Button)root.lookupButton(okButton)); |
| 67 | + |
| 68 | + GridPane grid = new GridPane(); |
| 69 | + grid.setHgap(10); |
| 70 | + grid.setVgap(10); |
| 71 | + grid.setPadding(new Insets(10, 40, 10, 40)); |
| 72 | + |
| 73 | + TextField username = new TextField(); |
| 74 | + username.setPromptText("Username"); |
| 75 | + username.setPrefWidth(300); |
| 76 | + GridPane.setHgrow(username, Priority.ALWAYS); |
| 77 | + |
| 78 | + PasswordField password = new PasswordField(); |
| 79 | + password.setPromptText("Password"); |
| 80 | + GridPane.setHgrow(password, Priority.ALWAYS); |
| 81 | + |
| 82 | + ChangeListener<String> cl = (obs, ov, nv) -> { |
| 83 | + button.setDisable(username.textProperty().getValue().isEmpty() || |
| 84 | + password.textProperty().getValue().isEmpty() ); |
| 85 | + }; |
| 86 | + |
| 87 | + username.textProperty().addListener(cl); |
| 88 | + password.textProperty().addListener(cl); |
| 89 | + |
| 90 | + { |
| 91 | + Node node = new Label("Username:"); |
| 92 | + GridPane.setHalignment(node, HPos.RIGHT); |
| 93 | + grid.add(node, 0, 0); |
| 94 | + } |
| 95 | + |
| 96 | + grid.add(username, 1, 0); |
| 97 | + |
| 98 | + { |
| 99 | + Node node = new Label("Password:"); |
| 100 | + GridPane.setHalignment(node, HPos.RIGHT); |
| 101 | + grid.add(node, 0, 1); |
| 102 | + } |
| 103 | + |
| 104 | + grid.add(password, 1, 1); |
| 105 | + |
| 106 | + root.setContent(grid); |
| 107 | + |
| 108 | + { |
| 109 | + button.setDisable(true); |
| 110 | + button.setOnAction(e -> { |
| 111 | + String uname = username.getText(); |
| 112 | + String pwd = password.getText(); |
| 113 | + System.out.println("Login with: " + uname + ", " + pwd); |
| 114 | + Platform.exit(); |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + Platform.runLater(() -> username.requestFocus()); |
| 119 | + |
| 120 | + return root; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void start(Stage stage) throws Exception |
| 125 | + { |
| 126 | + Scene scene = new Scene(makeContents()); |
| 127 | + stage.resizableProperty().setValue(Boolean.FALSE); |
| 128 | + stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/key.png"))); |
| 129 | + stage.setTitle("Application Login"); |
| 130 | + stage.setScene(scene); |
| 131 | + stage.show(); |
| 132 | + } |
| 133 | + |
| 134 | + static public void main(String[] args) throws Exception |
| 135 | + { |
| 136 | + Application.launch(args); |
| 137 | + } |
| 138 | +} |
0 commit comments