Ad

Dynamically Fill ListView From Data Model In Javafx

I've created a method to fill listviews with objects that fit the correct criteria:

public void setCourseModules(Collection<Module> modules) {

    t1UnSel.getItems().clear();
    t2UnSel.getItems().clear();
    t1Sel.getItems().clear();
    t2Sel.getItems().clear();
    yrSel.getItems().clear();

    modules.forEach(m -> {
        if (m.getRunPlan().equals(Delivery.TERM_1)) {
            if (m.isMandatory()) {
                t1Sel.getItems().add(m);
            } else {
                t1UnSel.getItems().add(m);
            }
        } else if (m.getRunPlan().equals(Delivery.TERM_2)) {
            if (m.isMandatory()) {
                t2Sel.getItems().add(m);
            } else {
                t2UnSel.getItems().add(m);
            }
        } else {
            yrSel.getItems().add(m);
        }
    });
}

and call it with:

smp.setCourseModules(course[0].getModulesOnCourse());

However, this is just a hardcoded entry to get the first course from the Array, I need to be able to dynamically change that index based on a comboBox selection, struggling to think how I can get the index of the course name as an int without hardcoding it...

Attached is the gist, the Student Setup class and the Controller are most important, Student class containing the comboBox and Controller has the course data declaration.

Ad

Answer

Just to extend M. le Rutte's answer

You could use

myComboBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
    @Override
    public void changed(ObservableValue observable, Object oldValue, Object newValue) {
        //do something with selected item
    }
});

or

myComboBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
        //do something with selected index
    }
});
Ad
source: stackoverflow.com
Ad