CheckBoxTableCell changelistener not working
Asked Answered
M

1

3

I'm trying to add a change listener to my CheckBoxTableCells but it doesn't seem to be working. I took the example for CheckBoxes figuring they would work the same way. However there is no output when i change its value. How would i add one correctly to a checkboxtablecell?

current code:

tc.setCellFactory(new Callback<TableColumn<Trainee, Boolean>, TableCell<Trainee, Boolean>>() {
                    @Override
                    public TableCell<Trainee, Boolean> call(TableColumn<Trainee, Boolean> p) {
                        final CheckBoxTableCell ctCell = new CheckBoxTableCell<>();
                        ctCell.selectedProperty().addListener(new ChangeListener<Boolean>() {
                            @Override
                            public void changed(ObservableValue ov, Boolean old_val, Boolean new_val) {
                                System.out.println(new_val);
                            }
                        });
                        return ctCell;
                    }
                });
Metamerism answered 8/8, 2014 at 19:13 Comment(2)
I also made my own CheckBoxTableCell and it worked, but because of the functionality requirements i'd rather do it from within the class i'm currently working in.Metamerism
possible duplicate of javafx how to get selected row data in table view with checkboxOcciput
M
13

The selectedProperty is inherited from Cell and it just indicates whether the Cell is selected in the UI component. Since you probably don't have cell selection enabled on your TableView, the cell never becomes selected. This isn't what you're looking for anyway; you want to know whether the CheckBox is selected, not the Cell.

The trick here is to use the selectedStateCallback property of the CheckBoxTableCell. This is a function that maps the index of the cell to a BooleanProperty. That BooleanProperty is bound bidirectionally to the selected state of the check box.

If your column is representing an actual property in your Trainee class (I'll just call it selectedProperty for demonstration) then you can do something like this:

final CheckBoxTableCell<Trainee, Boolean> ctCell = new CheckBoxTableCell<>();
ctCell.setSelectedStateCallback(new Callback<Integer, ObservableValue<Boolean>>() {
    @Override
    public ObservableValue<Boolean> call(Integer index) {
        return table.getItems().get(index).selectedProperty();
    }
});

Then the property in the Trainee class with be bidirectionally bound to the check box state. If you need to do more than just update your model object when the check box is selected/deselected, you can just observe that property.

If you don't have a property in the Trainee class, you can just create a BooleanProperty and observe it:

final CheckBoxTableCell<Trainee, Boolean> ctCell = new CheckBoxTableCell<>();
final BooleanProperty selected = new SimpleBooleanProperty();
ctCell.setSelectedStateCallback(new Callback<Integer, ObservableValue<Boolean>>() {
    @Override
    public ObservableValue<Boolean> call(Integer index) {
        return selected ;
    }
});
selected.addListener(new ChangeListener<Boolean>() {
    @Override
    public void changed(ObservableValue<? extends Boolean> obs, Boolean wasSelected, Boolean isSelected) {
        System.out.println(isSelected);
    }
});

As usual, all this code looks a lot nicer in Java 8.

Misrepresent answered 8/8, 2014 at 20:54 Comment(3)
This is what i was looking for, thanks again! You've helped me out so much today. Need to tweak it for a bit, but i'll manage. I can't thank you enough for how much you've helped me today!Metamerism
Thank you so much ... great and simple solution!Alurd
What if I do have a SimpleBooleanProperty field in the Trainee class? Can you share the code for it?Scantling

© 2022 - 2024 — McMap. All rights reserved.