在JavaFx 8中创建具有自定义背景颜色的复选框列表单元格

4

我正在尝试修改一个使用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;");
        }
    }
}

看到一个写得很好的问题,其中包括一个实际的 [MCVE] 来演示问题,真是一种令人愉悦的变化。点赞。 - James_D
感谢您的回答和点赞。公平起见,这个例子是从这本书中获取的。 - Yannis P.
1个回答

4

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

现在明白了!我打算使用自定义类,谢谢! - Yannis P.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接