我正在尝试修改 CheckBoxListCell 的使用示例,以便为每个单元格添加自定义背景颜色。这是我到目前为止所做的但无济于事
// ListViewCheckBoxEditing.java
package application;
import java.util.HashMap;
import java.util.Map;
import javafx.application.Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.cell.CheckBoxListCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class ListViewCheckBoxEditing extends Application {
Map<String, ObservableValue<Boolean>> map = new HashMap<>();
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) {
// Populate the map with ListView items as its keys and
// their selected state as the value
map.put("Apple", new SimpleBooleanProperty(false));
map.put("Banana", new SimpleBooleanProperty(false));
map.put("Donut", new SimpleBooleanProperty(false));
map.put("Hash Brown", new SimpleBooleanProperty(false));
ListView<String> breakfasts = new ListView<>();
breakfasts.setPrefSize(200, 120);
breakfasts.setEditable(true);
breakfasts.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
// Add all keys from the map as items to the ListView
breakfasts.getItems().addAll(map.keySet());
// Create a Callback object
Callback<String, ObservableValue<Boolean>> itemToBoolean = (String item) -> map.get(item);
// Set the cell factory to my CheckBoxListCell implementation
breakfasts.setCellFactory(MyCell.forListView(itemToBoolean));
Button printBtn = new Button("Print Selection");
printBtn.setOnAction(e -> printSelection());
VBox root = new VBox(new Label("Breakfasts:"), breakfasts, printBtn);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Using ListView Cell Factory");
stage.show();
}
public void printSelection() {
System.out.println("Selected items: ");
for(String key: map.keySet()) {
ObservableValue<Boolean> value = map.get(key);
if (value.getValue()) {
System.out.println(key);
}
}
System.out.println();
}
public class MyCell extends CheckBoxListCell<String>{
public MyCell(){
super();
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
// I would expect the following to work
setStyle("-fx-background-color: yellow;");
}
}
}
最佳答案
静态方法调用 MyCell.forListView(..)
只是调用方法 CheckBoxListCell.forListView(...)
,因此您根本没有使用自定义单元格类。
对于您想要的功能,您可以简单地更改从该方法返回的单元格的样式:
// Set the cell factory to my CheckBoxListCell implementation
Callback<ListView<String>, ListCell<String>> defaultCellFactory = CheckBoxListCell.forListView(itemToBoolean);
breakfasts.setCellFactory(lv -> {
ListCell<String> cell = defaultCellFactory.call(lv);
cell.setStyle("-fx-background-color: yellow");
return cell ;
});
并完全删除您的单元实现。
如果您需要更复杂的东西,那确实需要覆盖
updateItem(...)
方法,您需要实现回调以返回单元子类的实例:import java.util.HashMap;
import java.util.Map;
import javafx.application.Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.cell.CheckBoxListCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class ListViewCheckBoxEditing extends Application {
Map<String, ObservableValue<Boolean>> map = new HashMap<>();
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) {
// Populate the map with ListView items as its keys and
// their selected state as the value
map.put("Apple", new SimpleBooleanProperty(false));
map.put("Banana", new SimpleBooleanProperty(false));
map.put("Donut", new SimpleBooleanProperty(false));
map.put("Hash Brown", new SimpleBooleanProperty(false));
ListView<String> breakfasts = new ListView<>();
breakfasts.setPrefSize(200, 120);
breakfasts.setEditable(true);
breakfasts.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
// Add all keys from the map as items to the ListView
breakfasts.getItems().addAll(map.keySet());
// Create a Callback object
Callback<String, ObservableValue<Boolean>> itemToBoolean = (String item) -> map.get(item);
// Set the cell factory to my CheckBoxListCell implementation
breakfasts.setCellFactory(lv -> new MyCell(itemToBoolean));
Button printBtn = new Button("Print Selection");
printBtn.setOnAction(e -> printSelection());
VBox root = new VBox(new Label("Breakfasts:"), breakfasts, printBtn);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Using ListView Cell Factory");
stage.show();
}
public void printSelection() {
System.out.println("Selected items: ");
for(String key: map.keySet()) {
ObservableValue<Boolean> value = map.get(key);
if (value.getValue()) {
System.out.println(key);
}
}
System.out.println();
}
public class MyCell extends CheckBoxListCell<String>{
public MyCell(Callback<String, ObservableValue<Boolean>> getSelectedProperty){
super(getSelectedProperty);
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
// I would expect the following to work
setStyle("-fx-background-color: yellow;");
}
}
}
关于listview - 在 JavaFx 8 中使用自定义背景颜色创建 CheckBoxListCell,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39840921/