JavaFX ListView//Radio Button setSelected() not working

331 Views Asked by At

Heyall, im working on this form, that has a listview on top and text fields with attributes of the list objects beneath it. I implemented listeners so that if you click on one of the objects in the list, they form will fill with the corresponding data. This works for every text field, and for my age slider too. But im struggling with the radio buttons.

I have them like this inside my controller:

@FXML
private RadioButton rb_m;

@FXML
private ToggleGroup geschlechtGroup;

@FXML
private RadioButton rb_w;

This is my OnClickList event:

@FXML
    void clickList(ActionEvent event)
    {



            if(list.getSelectionModel().getSelectedItem().getGeschlecht().equals("Weiblich"))
                    rb_w.setSelected(true);
            else
                    rb_m.setSelected(true);

                tf_vn.setText(list.getSelectionModel().getSelectedItem().getVorname());
                tf_nn.setText(list.getSelectionModel().getSelectedItem().getNachname());
                tf_strasse.setText(list.getSelectionModel().getSelectedItem().getStrasse());
                tf_plz.setText(list.getSelectionModel().getSelectedItem().getPlz());
                tf_ort.setText(list.getSelectionModel().getSelectedItem().getWohnort());
                label_age.setText(list.getSelectionModel().getSelectedItem().getAlter() + " ");
    }

Since the textfiels fill, the clickList Event itself seems to be working properly. What really confuses me is why the radio buttons wont check. The problem must be with the if/else then.

Here is my List:

ObservableList<Person> items = FXCollections.observableArrayList();
    list.setItems(items);
    items.add(new Person("Eva", "Becker", "Lorenzstrasse 20", "95028",
            "Hof", "Weiblich", 44));
    items.add(new Person("Max", "Schmitt", "Testallee 5", "81245",
            "München", "Maennlich", 24));

    list.getSelectionModel().selectedItemProperty().addListener((v,oldValue, newValue) -> tf_vn.setText(newValue.getVorname()));
    list.getSelectionModel().selectedItemProperty().addListener((v,oldValue, newValue) -> tf_nn.setText(newValue.getNachname()));
    list.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) -> tf_strasse.setText(newValue.getStrasse()));
    list.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) -> tf_plz.setText(newValue.getPlz()));
    list.getSelectionModel().selectedItemProperty().addListener((v,oldValue, newValue) -> tf_ort.setText(newValue.getWohnort()));
    list.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) -> label_age.setText(" " + newValue.getAlter()));
    list.getSelectionModel().selectedItemProperty().addListener((v,oldValue, newValue) -> sliderAge.setValue(newValue.getAlter()));

And of course my person class has the method getGeschlecht which returns the gender String. Shouldnt my equal if block work properly then? Ive been looking for hours now and I just cant grasp whats the problem.

1

There are 1 best solutions below

0
On

ListView item selections don't trigger ActionEvents. This means unless you use a listener to invoke the clickList, it's not called when the cell selection changes. The other changes are applied by your listeners.

You should set the selected property from a listener too. Also I recommend using a single listener for this purpose:

initialize method

list.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) -> {
    tf_vn.setText(newValue.getVorname());
    tf_nn.setText(newValue.getNachname());
    tf_strasse.setText(newValue.getStrasse());
    tf_plz.setText(newValue.getPlz());
    tf_ort.setText(newValue.getWohnort());
    label_age.setText(" " + newValue.getAlter());
    sliderAge.setValue(newValue.getAlter());
    (newValue.getGeschlecht().equals("Weiblich") ? rb_w : rb_m).setSelected(true);
});