import javafx.application.Application; import javafx.event.*; import javafx.scene.Scene; import javafx.scene.control.Dialog; import javafx.scene.input.MouseEvent; import javafx.stage.Stage; import java.util.Optional; public class EmailBuddyApp extends Application { private EmailBuddyList model; // The model private EmailBuddyPanel view; // The view public void start(Stage primaryStage) { // Initially, no buddies model = new EmailBuddyList(); //String[] names = {"Stan Dupp", "April Rain", "Patty O' Lantern", "Robin Banks", "Sandy Beach"}; // Make a new viewing panel and add it to the pane view = new EmailBuddyPanel(model); //Handle the Add button view.getAddButton().setOnAction(new EventHandler() { // This is the single event handler for all of the buttons public void handle(ActionEvent actionEvent) { EmailBuddy aBuddy = new EmailBuddy(); // Now bring up the dialog box Dialog dialog = new BuddyDetailsDialog(primaryStage, "New Buddy Details", aBuddy); Optional result = dialog.showAndWait(); if (result.isPresent()) { model.add(aBuddy); // Add the buddy to the model view.update(); } } }); // Handle the Remove button view.getRemoveButton().setOnAction(new EventHandler() { // This is the single event handler for all of the buttons public void handle(ActionEvent actionEvent) { int index = view.getBuddyList().getSelectionModel().getSelectedIndex(); if (index >= 0) { model.remove(index); view.update(); } } }); // Handle the Hot List Button view.getHotListButton().setOnAction(new EventHandler() { // This is the single event handler for all of the buttons public void handle(ActionEvent actionEvent) { view.update(); } }); // Handle a double-click in the list view.getBuddyList().setOnMousePressed(new EventHandler() { public void handle(MouseEvent mouseEvent) { if (mouseEvent.getClickCount() == 2) { EmailBuddy selectedBuddy; int selectedIndex = view.getBuddyList().getSelectionModel().getSelectedIndex(); if (selectedIndex >= 0) { if (view.getHotListButton().isSelected()) selectedBuddy = model.getHotListBuddy(selectedIndex); else selectedBuddy = model.getBuddy(selectedIndex); if (selectedBuddy == null) return; // Now bring up the dialog box Dialog dialog = new BuddyDetailsDialog(primaryStage, "Edit Buddy Details", selectedBuddy); Optional result = dialog.showAndWait(); if (result.isPresent()) { view.update(); } } } else { view.update(); // Enables Remove button on single click } } }); primaryStage.setTitle("Email Buddy App"); primaryStage.setScene(new Scene(view, 400,300)); primaryStage.show(); } public static void main(String[] args) { launch(args); } }