Advertisement
relax4o

Showing Dialog

Jan 15th, 2017
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1.  @FXML
  2.     public void showDialogPane() {
  3.         // using Dialog methods
  4.         Dialog<ButtonType> dialog = new Dialog<>();
  5.  
  6.         // we must set owner which invokes the dialog
  7.         dialog.initOwner(mainBorderPane.getScene().getWindow());
  8.  
  9.         // setting a header title to dialog pane
  10.         dialog.setHeaderText("Enter your new To Do schedule");
  11.  
  12.         // setting a window title to dialog
  13.         dialog.setTitle("Add new item");
  14.  
  15.         // we need to instantiate the FXMLLoader to use it
  16.         FXMLLoader fxmlLoader = new FXMLLoader();
  17.  
  18.         // setting the FXML file location which will be loaded later
  19.         fxmlLoader.setLocation(getClass().getResource("additemdialog.fxml"));
  20.  
  21.         try {
  22.             // trying to load the content from FXML file into the dialog pane
  23.             dialog.getDialogPane().setContent(fxmlLoader.load());
  24.         } catch (IOException e ) {
  25.             e.getStackTrace();
  26.             return;
  27.         }
  28.  
  29.         // creating OK and Cancel button to dialog window
  30.         dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
  31.         dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
  32.  
  33.         // showing the dialog window and waiting for response by the buttons created above
  34.         Optional<ButtonType> result = dialog.showAndWait();
  35.  
  36.         if ( result.isPresent() && result.get() == ButtonType.OK) {
  37.             // check for pressing an OK button
  38.  
  39.             // we must get DialogController instance
  40.             DialogController controller = fxmlLoader.getController();
  41.  
  42.             // getting the newest ToDo item added in the list
  43.             TodoItem item = controller.processResult();
  44.  
  45.             // auto-selecting the newest todo item
  46.             todoListView.getSelectionModel().select(item);
  47.         }
  48.         else {
  49.             System.out.println("Cancel is pressed!");
  50.         }
  51.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement