Posted By

aztrovoi on 10/07/12


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

RobinD


login form


 / Published in: Java
 

http://java.dzone.com/tips/javafx-and-jdbc

  1. import javafx.application.Application;
  2. import javafx.event.ActionEvent;
  3. import javafx.event.EventHandler;
  4. import javafx.geometry.Insets;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Button;
  7. import javafx.scene.control.Label;
  8. import javafx.scene.control.PasswordField;
  9. import javafx.scene.control.TextField;
  10. import javafx.scene.effect.DropShadow;
  11. import javafx.scene.effect.Reflection;
  12. import javafx.scene.layout.BorderPane;
  13. import javafx.scene.layout.GridPane;
  14. import javafx.scene.layout.HBox;
  15. import javafx.scene.paint.Color;
  16. import javafx.scene.text.Font;
  17. import javafx.scene.text.FontWeight;
  18. import javafx.scene.text.Text;
  19. import javafx.stage.Stage;
  20.  
  21. /**
  22.  *
  23.  * @web http://zoranpavlovic.blogspot.com/
  24.  */
  25. public class Login extends Application {
  26.  
  27. String user = "JavaFX2";
  28. String pw = "password";
  29. String checkUser, checkPw;
  30.  
  31. public static void main(String[] args) {
  32. launch(args);
  33. }
  34.  
  35. @Override
  36. public void start(Stage primaryStage) {
  37. primaryStage.setTitle("JavaFX 2 Login");
  38.  
  39. BorderPane bp = new BorderPane();
  40. bp.setPadding(new Insets(10,50,50,50));
  41.  
  42. //Adding HBox
  43. HBox hb = new HBox();
  44. hb.setPadding(new Insets(20,20,20,30));
  45.  
  46. //Adding GridPane
  47. GridPane gridPane = new GridPane();
  48. gridPane.setPadding(new Insets(20,20,20,20));
  49. gridPane.setHgap(5);
  50. gridPane.setVgap(5);
  51.  
  52. //Implementing Nodes for GridPane
  53. Label lblUserName = new Label("Username");
  54. final TextField txtUserName = new TextField();
  55. Label lblPassword = new Label("Password");
  56. final PasswordField pf = new PasswordField();
  57. Button btnLogin = new Button("Login");
  58. final Label lblMessage = new Label();
  59.  
  60. //Adding Nodes to GridPane layout
  61. gridPane.add(lblUserName, 0, 0);
  62. gridPane.add(txtUserName, 1, 0);
  63. gridPane.add(lblPassword, 0, 1);
  64. gridPane.add(pf, 1, 1);
  65. gridPane.add(btnLogin, 2, 1);
  66. gridPane.add(lblMessage, 1, 2);
  67.  
  68.  
  69. //Reflection for gridPane
  70. Reflection r = new Reflection();
  71. r.setFraction(0.7f);
  72. gridPane.setEffect(r);
  73.  
  74. //DropShadow effect
  75. DropShadow dropShadow = new DropShadow();
  76. dropShadow.setOffsetX(5);
  77. dropShadow.setOffsetY(5);
  78.  
  79. //Adding text and DropShadow effect to it
  80. Text text = new Text("JavaFX 2 Login");
  81. text.setFont(Font.font("Courier New", FontWeight.BOLD, 28));
  82. text.setEffect(dropShadow);
  83.  
  84. //Adding text to HBox
  85. hb.getChildren().add(text);
  86.  
  87. //Add ID's to Nodes
  88. bp.setId("bp");
  89. gridPane.setId("root");
  90. btnLogin.setId("btnLogin");
  91. text.setId("text");
  92.  
  93. //Action for btnLogin
  94. btnLogin.setOnAction(new EventHandler() {
  95. public void handle(ActionEvent event) {
  96. checkUser = txtUserName.getText().toString();
  97. checkPw = pf.getText().toString();
  98. if(checkUser.equals(user) && checkPw.equals(pw)){
  99. lblMessage.setText("Congratulations!");
  100. lblMessage.setTextFill(Color.GREEN);
  101. }
  102. else{
  103. lblMessage.setText("Incorrect user or pw.");
  104. lblMessage.setTextFill(Color.RED);
  105. }
  106. txtUserName.setText("");
  107. pf.setText("");
  108. }
  109. });
  110.  
  111. //Add HBox and GridPane layout to BorderPane Layout
  112. bp.setTop(hb);
  113. bp.setCenter(gridPane);
  114.  
  115. //Adding BorderPane to the scene and loading CSS
  116. Scene scene = new Scene(bp);
  117. scene.getStylesheets().add(getClass().getClassLoader().getResource("login.css").toExternalForm());
  118. primaryStage.setScene(scene);
  119. primaryStage.titleProperty().bind(
  120. scene.widthProperty().asString().
  121. concat(" : ").
  122. concat(scene.heightProperty().asString()));
  123. //primaryStage.setResizable(false);
  124. primaryStage.show();
  125. }
  126. }
  127. In order to style this application properly you'll need to create login.css file in /src folder of your project. If you dont know how to do that, please check out JavaFX 2: Styling Buttons tutorial.
  128.  
  129. Here is CSS code of our example:
  130. #root {
  131. -fx-background-color: linear-gradient(lightgray, gray);
  132. -fx-border-color: white;
  133. -fx-border-radius: 20;
  134. -fx-padding: 10 10 10 10;
  135. -fx-background-radius: 20;
  136.  
  137. }
  138.  
  139. #bp {
  140. -fx-background-color: linear-gradient(gray,DimGrey );
  141.  
  142. }
  143.  
  144. #btnLogin {
  145. -fx-background-radius: 30, 30, 29, 28;
  146. -fx-padding: 3px 10px 3px 10px;
  147. -fx-background-color: linear-gradient(orange, orangered );
  148. }
  149.  
  150. #text {
  151. -fx-fill: linear-gradient(orange , orangered);
  152. }

Report this snippet  

You need to login to post a comment.