在JavaFx 8中使用自定义背景颜色创建CheckBoxListCell

时间:2016-10-03 21:48:16

标签: listview javafx-8

我正在尝试修改CheckBoxListCell的使用示例,以便为每个Cell添加自定义背景颜色。这是我迄今为止所做的,但无济于事

// 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;");
        }
    }
}

1 个答案:

答案 0 :(得分:3)

静态方法调用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;");
        }
    }
}