listview - 在 JavaFx 8 中创建具有自定义背景颜色的 CheckBoxListCell

标签 listview javafx-8

我正在尝试修改 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/

相关文章:

popup - (JavaFX) 执行操作时如何显示临时弹出窗口 (OSD)?

asp.net - 绑定(bind)到 ListView fieldName 来自资源

android - Checkedtextview 滚动 Listview 后选中/取消选中

java - 在 JavaFX 中调用 swing 软件的 main 函数将终止 JavaFX 软件

JavaFX 8 : setting style for inner VBox doesn't apply

java - 运行应用程序时更新 JavaFX 控件

android - SimpleCursorAdapter 和 ListView 问题

android - 在列表项上播放特定的声音,请单击

android - 使用卡片 View 创建 'line connected' recyclerview