问题描述
我有一个使用旁边的项 CheckBoxListCell
来显示与复选框列表列表视图。如何将侦听器添加到该复选框,当作为一个项目被选中或未被选中知道吗?
I have a listview that uses a CheckBoxListCell
to display a list with checkboxes next to the items. How do I add a listener to this checkbox to know when an item as been selected or unselected?
推荐答案
解决方案
您不要将监听器添加到复选框。你一个侦听器添加到将其与由CheckBoxListCell.forListView例程。
You don't add a listener to the checkbox. You add a listener to the observable property of the object which was associated with the checkbox by the CheckBoxListCell.forListView routine.
设置的关联:
ListView<Task> checklist = new ListView<>(tasks);
checklist.setCellFactory(CheckBoxListCell.forListView(Task::selectedProperty));
添加一个侦听所有项目:
Adding a listener for all items:
tasks.forEach(task -> task.selectedProperty().addListener((observable, wasSelected, isSelected) -> {
if (isSelected) {
// . . .
} else {
// . . .
}
}));
文档
的方法,在CheckBoxListCell.forListView javadoc中像这样:
The process is described in the CheckBoxListCell.forListView javadoc like so:
getSelectedProperty
- 一个回调,鉴于T类型的对象
(这是取出的ListView.items列表的值),将
返回重新presents是否给予ObservableValue
项目选择或没有。这ObservableValue将被绑定
双向(即单元格中的复选框将设置/取消
这个属性基于用户的互动,以及该复选框会
反映ObservableValue的状态,如果外部变化)。
示例程序
这表明一些可能与CheckBoxListCell使用模式的样本程序:
A sample program which demonstrated some of the patterns which could be used with CheckBoxListCell:
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.collections.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.CheckBoxListCell;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;
import java.util.*;
import java.util.stream.Collectors;
public class CheckList extends Application {
@Override
public void start(Stage stage) throws Exception{
ObservableList<Task> tasks = FXCollections.observableArrayList(
Arrays.stream(taskNames).map(Task::new).collect(Collectors.toList())
);
ListView<String> reactionLog = new ListView<>();
tasks.forEach(task -> task.selectedProperty().addListener((observable, wasSelected, isSelected) -> {
if (isSelected) {
reactionLog.getItems().add(reactionStrings.get(task.getName()));
reactionLog.scrollTo(reactionLog.getItems().size() - 1);
}
}));
ListView<Task> checklist = new ListView<>(tasks);
checklist.setCellFactory(CheckBoxListCell.forListView(Task::selectedProperty, new StringConverter<Task>() {
@Override
public String toString(Task object) {
return object.getName();
}
@Override
public Task fromString(String string) {
return null;
}
}));
HBox layout = new HBox(10, checklist, reactionLog);
layout.setPrefSize(350, 150);
layout.setPadding(new Insets(10));
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.show();
}
public static class Task {
private ReadOnlyStringWrapper name = new ReadOnlyStringWrapper();
private BooleanProperty selected = new SimpleBooleanProperty(false);
public Task(String name) {
this.name.set(name);
}
public String getName() {
return name.get();
}
public ReadOnlyStringProperty nameProperty() {
return name.getReadOnlyProperty();
}
public BooleanProperty selectedProperty() {
return selected;
}
public boolean isSelected() {
return selected.get();
}
public void setSelected(boolean selected) {
this.selected.set(selected);
}
}
public static void main(String[] args) {
launch(args);
}
private static final String[] taskNames = {
"Walk the dog",
"Skin the cat",
"Feed the pig"
};
private static final Map<String, String> reactionStrings = new HashMap<>();
static {
reactionStrings.put("Walk the dog", "The dog thanks you");
reactionStrings.put("Skin the cat", "The cat hates you");
reactionStrings.put("Feed the pig", "The pig wants more");
}
}
选择第一个项目一次,第三项三次后
示例输出。
Sample output after selecting the first item once and the third item three times.
这篇关于如何添加监听器复选框使用CheckBoxListCell一个ListView内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!