Skip to content

Commit e4179ab

Browse files
committed
JavaFX Login Dialog sample.
1 parent 4d364a0 commit e4179ab

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed

source/javafx-login/pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4+
http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>sample</groupId>
7+
<artifactId>sample</artifactId>
8+
<version>0.1.0-SNAPSHOT</version>
9+
<name>sample</name>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<jmh.version>1.17.3</jmh.version>
14+
<javac.target>1.8</javac.target>
15+
<exec.class>sample.sample1</exec.class>
16+
<args></args>
17+
</properties>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<version>3.1</version>
25+
<configuration>
26+
<compilerVersion>${javac.target}</compilerVersion>
27+
<source>${javac.target}</source>
28+
<target>${javac.target}</target>
29+
<compilerArgument>-Xlint</compilerArgument>
30+
</configuration>
31+
</plugin>
32+
<plugin>
33+
<artifactId>maven-clean-plugin</artifactId>
34+
<version>2.5</version>
35+
<configuration>
36+
<filesets>
37+
<fileset>
38+
<directory>.</directory>
39+
<includes>
40+
<include>**/*~</include>
41+
</includes>
42+
</fileset>
43+
</filesets>
44+
</configuration>
45+
</plugin>
46+
<plugin>
47+
<groupId>org.codehaus.mojo</groupId>
48+
<artifactId>exec-maven-plugin</artifactId>
49+
<version>1.5.0</version>
50+
<configuration>
51+
<executable>java</executable>
52+
<commandlineArgs>-classpath %classpath ${exec.class} ${args}</commandlineArgs>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
<dependencies>
59+
<dependency>
60+
<groupId>org.openjdk.jmh</groupId>
61+
<artifactId>jmh-core</artifactId>
62+
<version>${jmh.version}</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.openjdk.jmh</groupId>
66+
<artifactId>jmh-generator-annprocess</artifactId>
67+
<version>${jmh.version}</version>
68+
<scope>provided</scope>
69+
</dependency>
70+
</dependencies>
71+
72+
</project>
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.dialog-pane {
2+
-fx-background-color: azure;
3+
}
4+
5+
.dialog-pane .label {
6+
-fx-text-fill: black;
7+
}
8+
9+
.dialog-pane .content {
10+
/* -fx-background-color: white; */
11+
}
12+
13+
.dialog-pane .header-panel .label {
14+
-fx-font-weight: bold;
15+
-fx-font-size: 3em;
16+
}
783 Bytes
Loading

0 commit comments

Comments
 (0)