I have to implement an application that manages a list of students (name, surname, matnr.) and also add support for adding, removing, renaming students. Therefore i have to use MVC, Observer and JavaFX. My Problem is, that I'm not sure, where to put the action when a button is clicked (view or controller). If controller, I don't know how. Also I have a problem with implementing a main class and starting the view out of this class. That's one reason, why I wrote a part of the main in the view. Here is my Code:

View:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class ViewStudentList extends Application implements Observer {

    private StudentManager studMan = new StudentManager();
    private ListView<Student> list = new ListView<Student>();
    private ObservableList<Student> data;
    private Controller c = new Controller(studMan, this);

    @Override
    public void start(Stage stage) {
        studMan.addStudent("Name1", "Surname1", 123);
        studMan.addStudent("Name2", "Surname2", 456);
        studMan.addStudent("Name3", "Surname3", 789);
        data = FXCollections.observableArrayList(studMan.getList());
        VBox box = new VBox();

        stage.setTitle("ListViewSample");
        final Label label = new Label("StudentList");
        label.setFont(Font.font("Arial", 20));
        box.getChildren().addAll(label, list);
        VBox.setVgrow(list, Priority.ALWAYS);

        final Label status = new Label();

        final TextField nameField = new TextField();
        nameField.setMinHeight(25);
        nameField.setPromptText("Name");
        final TextField surnameField = new TextField();
        surnameField.setMinHeight(25);
        surnameField.setPromptText("Surname");
        final TextField matnrField = new TextField();
        matnrField.setMinHeight(25);
        matnrField.setPromptText("MatNr");

        HBox hb1 = new HBox();
        hb1.getChildren().addAll(nameField, surnameField, matnrField);
        hb1.setSpacing(5);
        hb1.setAlignment(Pos.CENTER);

        Button addButton = new Button("ADD");
        addButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if (nameField.getText() != null && !nameField.getText().isEmpty() && surnameField.getText() != null
                        && !surnameField.getText().isEmpty() && matnrField.getText() != null && !matnrField.getText().isEmpty()) {
                    if (matnrField.getText().matches("\\d+")) {
                        if (c.add(nameField.getText(), surnameField.getText(), Integer.parseInt(matnrField.getText()))) {
                            status.setText("Added Student");
                        } else {
                            status.setText("Adding not possible, MatNr already exists!");
                        }
                    } else {
                        status.setText("MatNr must be a number!");
                    }
                } else {
                    status.setText("Adding failed!");
                }
            }
        });

        Button renameButton = new Button("RENAME");
        renameButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                if (surnameField.getText() != null && !surnameField.getText().isEmpty()) {
                    final int selectedIdx = list.getSelectionModel().getSelectedIndex();
                    if (selectedIdx != -1) {
                        Student itemToRemove = list.getSelectionModel().getSelectedItem();
                        final int newSelectedIdx = (selectedIdx == list.getItems().size() - 1) ? selectedIdx - 1 : selectedIdx;
                        if (c.rename(surnameField.getText(), itemToRemove.getMatnr())) {
                            status.setText("Remamed " + itemToRemove);
                            list.getSelectionModel().select(newSelectedIdx);
                            data = FXCollections.observableArrayList(c.getList());
                            list.setItems(data);
                        } else {
                            status.setText("Renaming " + itemToRemove + " not possible!");
                        }
                    }
                } else {
                    status.setText("Renaming failed!");
                }
            }
        });

        Button removeButton = new Button("REMOVE");

        removeButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                final int selectedIdx = list.getSelectionModel().getSelectedIndex();
                if (selectedIdx != -1) {
                    Student itemToRemove = list.getSelectionModel().getSelectedItem();
                    final int newSelectedIdx = (selectedIdx == list.getItems().size() - 1) ? selectedIdx - 1 : selectedIdx;
                    if (c.removeStudent(itemToRemove.getMatnr())) {
                        status.setText("Removed " + itemToRemove);
                        list.getSelectionModel().select(newSelectedIdx);
                    } else {
                        status.setText("Removing " + itemToRemove + " not possible!");
                    }
                }
            }
        });

        Button clearButton = new Button("CLEAR");
        clearButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
                public void handle(ActionEvent e) {
                    nameField.clear();
                    surnameField.clear();
                    matnrField.clear();
                    status.setText(null);
                }
            });

        HBox hb2 = new HBox();
        hb2.getChildren().addAll(addButton, removeButton, renameButton, clearButton);
        hb2.setSpacing(5);
        hb2.setAlignment(Pos.CENTER);

        box.getChildren().addAll(hb2, hb1, status);
        box.setSpacing(5);
        Scene scene = new Scene(box, 300, 400);
        list.setItems(data);

        stage.setScene(scene);
        stage.show();
    }

    public void addController(Controller c) {
        this.c = c;
    }

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

    @Override
    public void update() {
        data = FXCollections.observableArrayList(c.getList());
        list.setItems(data);
    }

}

Controller:

import java.util.ArrayList;

public class Controller{

StudentManager studMan;
ViewStudentList viewList;

public Controller(StudentManager studMan, ViewStudentList viewList){
    this.studMan = studMan;
    this.viewList = viewList;
    studMan.attach(viewList);
}


public boolean removeStudent(int matnr){
    return studMan.removeStudent(matnr);
}

public ArrayList<Student> getList() {
    return studMan.getList();
}

public boolean rename(String surname, int matnr) {
    return studMan.rename(surname, matnr);
}

public boolean add(String name, String surname, int matnr) {
    return studMan.addStudent(name, surname, matnr);
    }    
}

Model:

import java.util.ArrayList;

public class StudentManager implements Subject {

    private ArrayList<Observer> observers = new ArrayList<Observer>();
    private ArrayList<Student> list = new ArrayList<Student>();

    public boolean addStudent(String name, String surname, int matnr) {
        for (Student s : list) {
            if (s.getMatnr() == matnr) {
                System.out.println("MatNr already exists!");
                return false;
            }
        }
        list.add(new Student(name, surname, matnr));
        notifyObservers();
        return true;
    }

    public boolean removeStudent(int matnr) {
        int i = 0;
        for (Student s : list) {
            if (s.getMatnr() == matnr) {
                list.remove(i);
                notifyObservers();
                return true;
            }
            i++;
        }
        System.out.println("Student with this MatNr doesn't exist!");
        return false;
    }

    public boolean rename(String surname, int matnr) {
        for (Student s : list) {
            if (s.getMatnr() == matnr) {
                s.setSurname(surname);
                notifyObservers();
                return true;
            }
        }
        return false;
    }

    public void printList() {
        for (Student s : list) {
            System.out.println(s);
        }
    }

    public ArrayList<Student> getList(){
        return list;
    }

    @Override
    public void attach(Observer o) {
        observers.add(o);
    }

    @Override
    public void detach(Observer o) {
        observers.remove(o);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update();
        }
    }
}

Hope, you can help me :)

Related posts

Recent Viewed