# Java FX



## merv (10. Dez 2017)

Hallo , 
ich habe ein Bookmanager implementiert mit Java FX ich sollte mit Observer Pattern arbeiten .
Bei der Aufgabenstellung heisst es :

Use the Observer pattern to decouple the logic of your application (Model) from the GUI (View). In no case shall the application logic be dependent on the GUI classes.

Meine Frage wäre wie ich bei meinem Programm ListView updaten kann , da es so mit den Observer Pattern keinen Sinn ergibt glaub ich ... 

Mein Code :

```
import java.util.Collection;
import java.util.List;

import javafx.application.Application;
import javafx.collections.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.*;
import javafx.scene.text.*;
import javafx.stage.*;

public class MainFX extends Application implements Observer {

    public static void main(String[] args) {
        launch(args);
    }

    ObservableList<Object> myList = FXCollections.observableArrayList();
    ListView<Object> listView = new ListView<>(myList);
   
    @Override
    public void start(final Stage stage) throws Exception {

        BorderPane root = new BorderPane();

        // BUTTONS
        Button addBtn = new Button("Add");
        Button removeBtn = new Button("Remove");
        Button editBtn = new Button("Edit");

        final BookManager bm = new BookManager();
        bm.addObserver(this);

        // ADD_GRID
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25, 25, 25, 25));

        // ADD_SCENE
        Scene scene = new Scene(grid, 450, 275);
        stage.setScene(scene);
        Text scenetitle = new Text("Add Book");
        scenetitle.setFont(Font.font("Arial", FontWeight.NORMAL, 18));
        grid.add(scenetitle, 0, 0, 2, 1);

        // ADD_LABELS
        Label title = new Label("Enter new book title:");
        grid.add(title, 0, 1);
        TextField titleField = new TextField();
        grid.add(titleField, 1, 1);

        Label author = new Label("Enter new book author:");
        grid.add(author, 0, 2);
        TextField authorField = new TextField();
        grid.add(authorField, 1, 2);

        Label year = new Label("Enter new book year:");
        grid.add(year, 0, 3);
        TextField yearField = new TextField();
        grid.add(yearField, 1, 3);

        Label isbn = new Label("Enter new book isbn:");
        grid.add(isbn, 0, 4);
        TextField isbnField = new TextField();
        grid.add(isbnField, 1, 4);

        // ADD_BUTTONS
        Button add = new Button("Add Book");
        Button cancelAdd = new Button("Cancel");
        HBox hbAdd = new HBox(10);
        hbAdd.setAlignment(Pos.BOTTOM_RIGHT);
        hbAdd.getChildren().addAll(add, cancelAdd);
        grid.add(hbAdd, 1, 5);

        // EDIT_GRID
        GridPane grid1 = new GridPane();
        grid1.setAlignment(Pos.CENTER);
        grid1.setHgap(10);
        grid1.setVgap(10);
        grid1.setPadding(new Insets(25, 25, 25, 25));

        // EDIT_SCENE
        Scene scene2 = new Scene(grid1, 450, 275);
        stage.setScene(scene2);
        Text sceneTitle = new Text("Edit book");
        sceneTitle.setFont(Font.font("Arial", FontWeight.NORMAL, 18));
        grid1.add(sceneTitle, 0, 0, 2, 1);

        // EDIT_BUTTONS
        Button saveEdit = new Button("Save edit");
        Button cancelEdit = new Button("Cancel edit");
        HBox hbSave = new HBox(10);
        hbSave.setAlignment(Pos.BOTTOM_RIGHT);
        hbSave.getChildren().addAll(saveEdit, cancelEdit);
        grid1.add(hbSave, 1, 5);

        // EDIT_LABELS
        Label title1 = new Label("Enter new book title:");
        grid1.add(title1, 0, 1);
        TextField titleField1 = new TextField();
        grid1.add(titleField1, 1, 1);

        Label author1 = new Label("Enter new book author:");
        grid1.add(author1, 0, 2);
        TextField authorField1 = new TextField();
        grid1.add(authorField1, 1, 2);

        Label year1 = new Label("Enter new book year:");
        grid1.add(year1, 0, 3);
        TextField yearField1 = new TextField();
        grid1.add(yearField1, 1, 3);

        Label isbn1 = new Label("Enter new book isbn:");
        grid1.add(isbn1, 0, 4);
        TextField isbnField1 = new TextField();
        grid1.add(isbnField1, 1, 4);

        // TOOLBAR
        ToolBar toolBar = new ToolBar(addBtn, removeBtn, editBtn);
        root.setTop(toolBar);
        root.setCenter(listView);
        Scene scene3 = new Scene(root, 200, 200);
        stage.setScene(scene3);
        stage.show();

        // CANCEL ADD BUTTON HANDLER
        cancelAdd.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                stage.setScene(scene3);
            }
        });

        // CANCEL EDIT BUTTON HANDLER
        cancelEdit.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                stage.setScene(scene3);
            }
        });

        // ADD BUTTON HANDLER
        addBtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                stage.setScene(scene);
            }
        });

        // ADD BOOK BUTTON HANDLER
        add.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                try {
                    if ((titleField.getText().length() > 0) && (authorField.getText().length() > 0)
                            && (yearField.getText().length() > 0) && (isbnField.getText().length() > 0)) {

                        Book book = new Book(titleField.getText(), authorField.getText(),
                                Integer.parseInt(yearField.getText()), isbnField.getText());

                        boolean successadd = bm.addBook(book);

                        if (successadd) {
                            titleField.clear();
                            authorField.clear();
                            yearField.clear();
                            isbnField.clear();
                            stage.setScene(scene3);
                        } else {
                            Alert alert = new Alert(AlertType.WARNING);
                            alert.setTitle("Warning");
                            alert.setHeaderText("This ISBN already exists!");
                            alert.setContentText("Enter a unique ISBN");
                            alert.showAndWait();
                        }
                    } else {
                        Alert alert = new Alert(AlertType.WARNING);
                        alert.setTitle("Warning");
                        alert.setHeaderText("Some Fields are not filled out");
                        alert.setContentText("Fill out all Fields in the form");
                        alert.showAndWait();
                    }
                } catch (NullPointerException e) {
                    e.printStackTrace();
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        });

        // REMOVE BOOK BUTTON HANDLER
        removeBtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if (getSelection() != null) {
                    bm.removeBook(getSelection());
                } else {
                    Alert alert = new Alert(AlertType.WARNING);
                    alert.setTitle("Warning");
                    alert.setHeaderText("Nothing selected to remove!");
                    alert.setContentText("Select a book to remove.");
                    alert.showAndWait();
                }
            }
        });

        // EDIT BOOK BUTTON HANDLER
        editBtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                try {
                    if (getSelection() != null) {
                        String t = getSelection().getTitle();
                        String a = getSelection().getAuthor();
                        String y = String.valueOf(getSelection().getYear());
                        String i = getSelection().getIsbn();

                        titleField1.setText(t);
                        authorField1.setText(a);
                        yearField1.setText(y);
                        isbnField1.setText(i);

                        stage.setScene(scene2);
                    } else {
                        Alert alert = new Alert(AlertType.WARNING);
                        alert.setTitle("Warning");
                        alert.setHeaderText("Nothing selected to edit!");
                        alert.setContentText("Select a book to edit!");
                        alert.showAndWait();
                    }
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        });

        // SAVE EDIT BUTTON HANDLER
        saveEdit.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                try {
                    if ((titleField1.getText().length() > 0) || (authorField1.getText().length() > 0)
                            || (yearField1.getText().length() > 0) || (isbnField1.getText().length() > 0)) {

                        boolean successEdit = bm.editBook(getSelection(), titleField1.getText(), authorField1.getText(),
                                Integer.parseInt(yearField1.getText()), isbnField1.getText());

                        if (successEdit) {
                            titleField1.clear();
                            authorField1.clear();
                            yearField1.clear();
                            isbnField1.clear();
                            stage.setScene(scene3);
                        } else {
                            Alert alert = new Alert(AlertType.WARNING);
                            alert.setTitle("Warning");
                            alert.setHeaderText("No Field is filled out!");
                            alert.setContentText("Fill out fields to edit!");
                            alert.showAndWait();
                        }
                    } else {
                        Alert alert = new Alert(AlertType.WARNING);
                        alert.setTitle("Warning");
                        alert.setHeaderText("No Field is filled out!");
                        alert.setContentText("Fill out fields to edit!");
                        alert.showAndWait();
                    }
                } catch (NullPointerException e) {
                    e.printStackTrace();
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    // GET SELECTED BOOK
    public Book getSelection() {
        return (Book) listView.getSelectionModel().getSelectedItem();
    }

    @Override
    public void update(Book book, Actions.ACTIONS a) {

        switch (a) {
        case ADD:
             ((List<Object>) listView).add(book);
            break;
        case REMOVE:
            ((List<Object>) listView).remove(book);
            break;
        case EDIT:
            myList.set(myList.indexOf(book), book);
            break;
        };
    }
}
```

Bei der update Methode muss ich das irgendwie anders machen denk ich .... Ich bräuchte dringende Hilfe BITTE ......
DANKE!


----------

