JAVAFX

btl1903

Mitglied
habe folgende aufgabe und komme leider nicht mehr weiter (muss die aufgabe bis morgen schaffen):

Implement a small JavaFX [2] GUI application called Book-Manager.

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.


**Book-Manager Model**
Implement an application that manages a list of books (title, author, year, isbn, ...).
Add support for adding, removing, editing books.

**GUI**
Write a GUI for that application.
The GUI shall provide a toolbar containing the buttons 'Add', 'Remove' to add and remove a book, respectively.
Also, provide a list (or a table) that displays all books.

-'Add' asks for the data of a new book and adds it (use some available dialog, see e.g. [4] -- make it simple).
-'Remove' removes the book that is currently selected in the list/table.

Do not allow duplicates (two books with the same isbn)! -- this is a business logic rule!

Note: The model part of your application must be able to exist without any GUI components.
Therefore, the actual list of books is part of the model, not part of a class that relates to the GUI.

The model must not depend on the view. Use the Observer pattern [1].
Go for the push-approach; encapsulate the broadcasted details.

Use different java packages for the model and the view.

Note that you might require exception handling in the button-handlers.
____________________________________________________________

Ich habe die gui und die application schon alles gemacht, die handler usw. auch. Aber bei mir funktionieren die buttons nicht. Ich glaube, das Problem ist, dass es die scenes nicht ändert. Bei EDIT gibt es auch einen Fehler, aber ich komme nicht mehr weiter.
So lautet mein Code:
Code:
import java.util.ArrayList;

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();
        Button cmdAdd = new Button("Add");
        Button cmdRemove = new Button("Remove");
        Button cmdEdit = new Button("Edit");

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

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

        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);

        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);

        Button btn = new Button("Add Book");
        Button cancel = new Button("Cancel");
        HBox hbBtn = new HBox(10);
        hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
        hbBtn.getChildren().addAll(btn,cancel);
        grid.add(hbBtn, 1, 5);
       
        /*Button cancel = new Button("Cancel");
        HBox hbCancel = new HBox(10);
        hbCancel.setAlignment(Pos.BOTTOM_LEFT);
        hbCancel.getChildren().add(cancel);
        grid.add(hbCancel, 1, 5);*/
       
        // EDIT
       
        GridPane grid1 = new GridPane();
        grid1.setAlignment(Pos.CENTER);
        grid1.setHgap(10);
        grid1.setVgap(10);
        grid1.setPadding(new Insets(25, 25, 25, 25));
       
        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);
       
        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);
       
        /*Button cancelEdit = new Button("Cancel edit");
        HBox hbCancelEdit = new HBox(10);
        hbCancelEdit.setAlignment(Pos.BOTTOM_LEFT);
        hbCancelEdit.getChildren().add(cancelEdit);
        grid1.add(hbCancelEdit, 1, 5);*/
       
        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);
       
       
         //Test
         
          Book b1 = new Book ("B1", "A.", 1990, "8473876783452");
          Book b2 = new Book ("B2", "B.", 2015, "8473876723452");
          Book b3 = new Book ("B3", "C.", 2015, "8434876723452");
          bm.addBook(b1);
          bm.addBook(b2);
          bm.addBook(b3);
        

        // INITIALIZE ZUM TESTEN
        ArrayList<Book> books = bm.getList();

        for (Book book : books) {
            myList.add((Book) book);
        }
               
        //CANCEL ADD BUTTON
        cancel.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                stage.setScene(scene);
            }
        });

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

        // BTN BUTTON HANDLER
        btn.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(scene);
                        } 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
        cmdRemove.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();           
                }
            }
        });

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

        //SAVE EDIT BUTTON
        saveEdit.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
               
               
               
               
                /*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);*/
               
                try {
                    if ((titleField1.getText().length() > 0) && (authorField1.getText().length() > 0)
                            && (yearField1.getText().length() > 0) && (isbnField1.getText().length() > 0)) {
                       
                        Book book = new Book(titleField1.getText(), authorField1.getText(),
                                Integer.parseInt(yearField1.getText()), isbnField1.getText());
                       
                        boolean successEdit = bm.editBook(book, titleField1.getText(), authorField1.getText(),
                                Integer.parseInt(yearField1.getText()), isbnField1.getText());
                       
                   
                        if (successEdit) {
                            /*titleField1.clear();
                            authorField1.clear();
                            yearField1.clear();
                            isbnField1.clear();*/
                            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("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();
                }           
            }
        });
       
        // EDIT BOOK BUTTON
        cmdEdit.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                try {                                       
                    if (getSelection() != null) {
                       
                        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();
                }
                    /*
                        String t = titleField1.getText();
                        String a = authorField1.getText();
                        String y = yearField1.getText();
                        String i = isbnField1.getText();
   
                        titleField1.setText(t);
                        authorField1.setText(a);
                        yearField1.setText(y);
                        isbnField1.setText(i);
                       
                        /*
                        String t = getSelection().getTitle();
                        String a = getSelection().getAuthor();
                        String y = getSelection().getYear();
                        String i = getSelection().getIsbn();
                       
                        titleField1.setText(t);
                        authorField1.setText(a);
                        yearField1.setText(y);
                        isbnField1.setText(i);
                        

                    } 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();
                }
                catch (NumberFormatException e) {
                    e.printStackTrace();
                }*/
            }

        });

        ToolBar toolBar = new ToolBar(cmdAdd, cmdRemove, cmdEdit);
        root.setTop(toolBar);
        root.setCenter(listView);
        Scene scene3 = new Scene(root, 200, 200);
        stage.setScene(scene3);
        stage.show();
    }
 
      public Book getSelection() {
        //statt
          //return (Book) listView.getCellFactory();
          return (Book) listView.getSelectionModel().getSelectedItem();
    }
 
    @Override
    public void update(Book book, Actions.ACTIONS a) {
       
        switch (a) {
        case ADD:
            myList.add(book);
            break;
        case REMOVE:
            myList.remove(book);
            break;
        case EDIT:
            myList.set(myList.indexOf(book), book);
            break;
        }
    }
}
 

krgewb

Top Contributor
Was brauche ich dazu? Bei mir gehen diese import-Anweisungen nicht:
Java:
import javafx.application.Application;
import javafx.scene.control.Alert.AlertType;
Meine Version: jre1.8.0_131
 

Neue Themen


Oben