Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.62 KB | None | 0 0
  1. package demoopenwindowtwo;
  2.  
  3. import javafx.geometry.Insets;
  4. import javafx.geometry.Pos;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.*;
  7. import javafx.scene.control.Alert.AlertType;
  8. import javafx.scene.layout.GridPane;
  9. import javafx.scene.layout.HBox;
  10. import javafx.stage.Modality;
  11. import javafx.stage.Stage;
  12. import javafx.stage.StageStyle;
  13.  
  14. import java.util.ArrayList;
  15.  
  16. public class PersonInputWindow extends Stage {
  17.     public PersonInputWindow(String title, Stage owner) {
  18.         this.initOwner(owner);
  19.         this.initStyle(StageStyle.UTILITY);
  20.         this.initModality(Modality.APPLICATION_MODAL);
  21.         this.setMinHeight(100);
  22.         this.setMinWidth(200);
  23.         this.setResizable(false);
  24.  
  25.         this.setTitle(title);
  26.         GridPane pane = new GridPane();
  27.         this.initContent(pane);
  28.  
  29.         Scene scene = new Scene(pane);
  30.         this.setScene(scene);
  31.     }
  32.  
  33.     // -------------------------------------------------------------------------
  34.     private TextField txfName;
  35.     private TextField txfTitle;
  36.     private CheckBox checkSenior;
  37.  
  38.     private Person newPerson = null;
  39.  
  40.     private void initContent(GridPane pane) {
  41.         // pane.setGridLinesVisible(true);
  42.         pane.setPadding(new Insets(20));
  43.         pane.setHgap(10);
  44.         pane.setVgap(10);
  45.  
  46.         Label lblName = new Label("Name:");
  47.         pane.add(lblName, 0, 0);
  48.  
  49.         Label lblTitle = new Label("Title:");
  50.         pane.add(lblTitle, 0, 1);
  51.  
  52.         txfName = new TextField();
  53.         pane.add(txfName, 1, 0, 2, 1);
  54.  
  55.         txfTitle = new TextField();
  56.         pane.add(txfTitle, 1, 1, 2, 1);
  57.  
  58.         checkSenior = new CheckBox("Senior");
  59.         pane.add(checkSenior, 1, 2, 2, 1);
  60.  
  61.         HBox buttonBox = new HBox(20);
  62.         pane.add(buttonBox, 0, 3);
  63.         buttonBox.setPadding(new Insets(10, 10, 0, 10));
  64.         buttonBox.setAlignment(Pos.TOP_RIGHT);
  65.  
  66.         Button btnCancel = new Button("Cancel");
  67.         buttonBox.getChildren().add(btnCancel);
  68.         btnCancel.setOnAction(event -> this.cancelAction());
  69.  
  70.         Button btnOK = new Button("OK");
  71.         buttonBox.getChildren().add(btnOK);
  72.         btnOK.setOnAction(event -> this.okAction());
  73.     }
  74.  
  75.     // -------------------------------------------------------------------------
  76.  
  77.     // -------------------------------------------------------------------------
  78.     // Button actions
  79.  
  80.     private void cancelAction() {
  81.         txfName.requestFocus();
  82.         clearFields();
  83.         newPerson = null;
  84.         PersonInputWindow.this.hide();
  85.     }
  86.  
  87.     private void okAction() {
  88.         String title = txfTitle.getText().trim();
  89.         String name = txfName.getText().trim();
  90.         boolean senior = checkSenior.isSelected();
  91.  
  92.         if (title.length() > 0 && name.length() > 0) {
  93.             newPerson = new Person(name, title, senior);
  94.             clearFields();
  95.             txfName.requestFocus();
  96.             PersonInputWindow.this.hide();
  97.         } else {
  98.             Alert alert = new Alert(AlertType.INFORMATION);
  99.             alert.setTitle("Add person");
  100.             alert.setHeaderText("Information missing");
  101.             alert.setContentText("Type name and/or title");
  102.             alert.show();
  103.         }
  104.  
  105.     }
  106.  
  107.     public Person getNewPerson() {
  108.         return newPerson;
  109.     }
  110.  
  111.     public void clearNewPerson() {
  112.         newPerson = null;
  113.     }
  114.  
  115.     private void addAction() {
  116.         String name = txfName.getText().trim();
  117.         String title = txfTitle.getText().trim();
  118.         boolean senior = checkSenior.isSelected();
  119.         if (name.length() > 0 && title.length() > 0) {
  120.             newPerson = new Person(name, title, senior);
  121.         } else {
  122.             Alert alert = new Alert(Alert.AlertType.INFORMATION);
  123.             alert.setTitle("Add person");
  124.             alert.setHeaderText("Missing information");
  125.             alert.setContentText("You need to type name and/or title of the person");
  126.             alert.show();
  127.             // wait for the modal dialog to close
  128.         }
  129.     }
  130.  
  131.     private void clearFields() {
  132.         txfName.clear();
  133.         txfTitle.clear();
  134.         checkSenior.setSelected(false);
  135.     }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement