Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package demoopenwindowtwo;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.*;
- import javafx.scene.control.Alert.AlertType;
- import javafx.scene.layout.GridPane;
- import javafx.scene.layout.HBox;
- import javafx.stage.Modality;
- import javafx.stage.Stage;
- import javafx.stage.StageStyle;
- import java.util.ArrayList;
- public class PersonInputWindow extends Stage {
- public PersonInputWindow(String title, Stage owner) {
- this.initOwner(owner);
- this.initStyle(StageStyle.UTILITY);
- this.initModality(Modality.APPLICATION_MODAL);
- this.setMinHeight(100);
- this.setMinWidth(200);
- this.setResizable(false);
- this.setTitle(title);
- GridPane pane = new GridPane();
- this.initContent(pane);
- Scene scene = new Scene(pane);
- this.setScene(scene);
- }
- // -------------------------------------------------------------------------
- private TextField txfName;
- private TextField txfTitle;
- private CheckBox checkSenior;
- private Person newPerson = null;
- private void initContent(GridPane pane) {
- // pane.setGridLinesVisible(true);
- pane.setPadding(new Insets(20));
- pane.setHgap(10);
- pane.setVgap(10);
- Label lblName = new Label("Name:");
- pane.add(lblName, 0, 0);
- Label lblTitle = new Label("Title:");
- pane.add(lblTitle, 0, 1);
- txfName = new TextField();
- pane.add(txfName, 1, 0, 2, 1);
- txfTitle = new TextField();
- pane.add(txfTitle, 1, 1, 2, 1);
- checkSenior = new CheckBox("Senior");
- pane.add(checkSenior, 1, 2, 2, 1);
- HBox buttonBox = new HBox(20);
- pane.add(buttonBox, 0, 3);
- buttonBox.setPadding(new Insets(10, 10, 0, 10));
- buttonBox.setAlignment(Pos.TOP_RIGHT);
- Button btnCancel = new Button("Cancel");
- buttonBox.getChildren().add(btnCancel);
- btnCancel.setOnAction(event -> this.cancelAction());
- Button btnOK = new Button("OK");
- buttonBox.getChildren().add(btnOK);
- btnOK.setOnAction(event -> this.okAction());
- }
- // -------------------------------------------------------------------------
- // -------------------------------------------------------------------------
- // Button actions
- private void cancelAction() {
- txfName.requestFocus();
- clearFields();
- newPerson = null;
- PersonInputWindow.this.hide();
- }
- private void okAction() {
- String title = txfTitle.getText().trim();
- String name = txfName.getText().trim();
- boolean senior = checkSenior.isSelected();
- if (title.length() > 0 && name.length() > 0) {
- newPerson = new Person(name, title, senior);
- clearFields();
- txfName.requestFocus();
- PersonInputWindow.this.hide();
- } else {
- Alert alert = new Alert(AlertType.INFORMATION);
- alert.setTitle("Add person");
- alert.setHeaderText("Information missing");
- alert.setContentText("Type name and/or title");
- alert.show();
- }
- }
- public Person getNewPerson() {
- return newPerson;
- }
- public void clearNewPerson() {
- newPerson = null;
- }
- private void addAction() {
- String name = txfName.getText().trim();
- String title = txfTitle.getText().trim();
- boolean senior = checkSenior.isSelected();
- if (name.length() > 0 && title.length() > 0) {
- newPerson = new Person(name, title, senior);
- } else {
- Alert alert = new Alert(Alert.AlertType.INFORMATION);
- alert.setTitle("Add person");
- alert.setHeaderText("Missing information");
- alert.setContentText("You need to type name and/or title of the person");
- alert.show();
- // wait for the modal dialog to close
- }
- }
- private void clearFields() {
- txfName.clear();
- txfTitle.clear();
- checkSenior.setSelected(false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement