Listview 在JavaFX8中创建具有自定义背景颜色的CheckBoxListCell
我试图修改CheckBoxListCell的使用示例,以便为每个单元格添加自定义背景色。这是我到目前为止所做的,但没有用Listview 在JavaFX8中创建具有自定义背景颜色的CheckBoxListCell,listview,javafx-8,Listview,Javafx 8,我试图修改CheckBoxListCell的使用示例,以便为每个单元格添加自定义背景色。这是我到目前为止所做的,但没有用 // ListViewCheckBoxEditing.java package application; import java.util.HashMap; import java.util.Map; import javafx.application.Application; import javafx.beans.property.SimpleBooleanProper
// 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;");
}
}
}
//ListViewCheckBoxEditing.java
包装申请;
导入java.util.HashMap;
导入java.util.Map;
导入javafx.application.application;
导入javafx.beans.property.SimpleBoleAnProperty;
导入javafx.beans.value.observeValue;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.control.ListView;
导入javafx.scene.control.SelectionMode;
导入javafx.scene.control.cell.CheckBoxListCell;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
导入javafx.util.Callback;
公共类ListViewCheckBoxEditing扩展了应用程序{
Map Map=newhashmap();
公共静态void main(字符串[]args){
应用程序启动(args);
}
@凌驾
公众假期开始(阶段){
//使用ListView项作为其键和
//将其选定状态作为值
map.put(“苹果”,新SimpleBoleAnProperty(假));
map.put(“香蕉”,新SimpleBoleAnProperty(假));
map.put(“甜甜圈”,新的SimpleBoleAnProperty(false));
map.put(“Hash Brown”,新的SimpleBoleAnProperty(false));
ListView早餐=新建ListView();
早餐。套餐尺寸(200120);
早餐。设置可编辑(真);
breakfasts.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
//将地图中的所有键作为项目添加到ListView
breakfasts.getItems().addAll(map.keySet());
//创建回调对象
回调itemToBoolean=(字符串项)->map.get(项);
//将单元格工厂设置为我的CheckBoxListCell实现
早餐。setCellFactory(MyCell.forListView(itemToBoolean));
按钮printBtn=新按钮(“打印选择”);
设置操作(e->printSelection());
VBox根=新VBox(新标签(“早餐:”)、早餐、打印BTN);
场景=新场景(根);
舞台场景;
stage.setTitle(“使用ListView单元工厂”);
stage.show();
}
公共无效打印选择(){
System.out.println(“选定项:”);
for(字符串键:map.keySet()){
ObservableValue=map.get(键);
if(value.getValue()){
系统输出打印项次(键);
}
}
System.out.println();
}
公共类MyCell扩展了CheckBoxListCell{
公共迈塞尔(){
超级();
}
@凌驾
public void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
//我希望下面的方法能奏效
setStyle(“-fx背景色:黄色;”);
}
}
}
静态方法调用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 ;
});
看到一个写得很好的问题,包括一个实际的问题来说明这个问题,这真是一个令人愉快的变化。向上投票。谢谢你的回答和向上投票。公平地说,这个例子是从现在就有意义的黑客!我打算去海关上课,谢谢!
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;");
}
}
}