如何在javafx中仅更改listview中第一个单元格的背景色?

如何在javafx中仅更改listview中第一个单元格的背景色?,java,listview,javafx,Java,Listview,Javafx,如何在JavaFX中仅更改listview中第一个单元格的背景色?我只想更改listview中第一个单元格的背景色。有没有办法做到这一点 您需要在列表视图上实现自定义的CellFactory。然后,我们可以确定该单元格是否属于用于填充列表视图的列表中的第一项。如果是,请仅对该单元格应用不同的样式 我不知道是否有办法确定列表视图的第一个单元格,但我们肯定可以捕获列表中的第一项 考虑以下应用程序。我们有一个只显示字符串列表的ListView 我们在列表视图上设置自定义单元工厂,如果项是列表中填充列表

如何在JavaFX中仅更改listview中第一个单元格的背景色?我只想更改listview中第一个单元格的背景色。有没有办法做到这一点

您需要在
列表视图
上实现自定义的
CellFactory
。然后,我们可以确定该单元格是否属于用于填充
列表视图的
列表中的第一项。如果是,请仅对该单元格应用不同的样式

我不知道是否有办法确定
列表视图
的第一个单元格,但我们肯定可以捕获
列表
中的第一项

考虑以下应用程序。我们有一个只显示字符串列表的
ListView

我们在
列表视图
上设置自定义
单元工厂
,如果
列表
中填充
列表视图
的第一项,则设置单元样式


导入javafx.application.application;
导入javafx.geometry.Insets;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.ListCell;
导入javafx.scene.control.ListView;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类主扩展应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段primaryStage){
//简单接口
VBox根=新的VBox(5);
根。设置填充(新插图(10));
根部设置对齐(位置中心);
//创建ListView
ListView ListView=新建ListView();
setAll(“标题”、“一”、“二”、“三”、“四”、“五”);
//为ListView设置CellFactory
listView.setCellFactory(列表->{
ListCell=新ListCell(){
@凌驾
受保护的void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
if(空| |项==null){
//此单元格中没有要显示的项目,请将其留空
设置图形(空);
//从单元格中清除样式
设置样式(空);
}否则{
//如果该项等于列表中的第一项,请设置样式
if(item.equalsIgnoreCase(list.getItems().get(0))){
//将背景色设置为蓝色
设置样式(“-fx背景色:蓝色;-fx文本填充:白色”);
}
//最后,在单元格中显示项目文本
setText(项目);
}
}
};
返回单元;
});
root.getChildren().add(listView);
//上台
primaryStage.setScene(新场景(根));
primaryStage.show();
}
}
结果


显然,您需要进行一些调整以匹配您的数据模型,而仅仅通过
字符串进行匹配并不是最好的方法

这不会阻止用户选择第一个项目,如果在构建场景后对列表进行排序,则可能无法按预期工作


虽然这可能会回答你的直接问题,但是为了保证用户的良好体验,还有其他一些事情要考虑。

当第一个单元格被选中时,你希望它的颜色是什么?谢谢你的回复。否,我想在不被选中的情况下显示单元格中的颜色。所以,我的意思是,每当有人看到我的listview时,他们会发现第一个单元格始终是蓝色的。对,我的意思是,当实际选中该单元格时,您希望发生什么?您是否希望阻止JavaFX突出显示该单元格,并保留手动设置的任何颜色?在我看来,这不是一个直观的设计,因为如果用户单击第一个单元格,他们会期待一些视觉反馈……因此,我在listview的第一个单元格中有一个名为posts的文本,在下面的单元格中,我拥有所有的帖子。但是,由于Post是listview的标题,我希望第一个单元格被着色,使其看起来很好。正确-但是您希望在单元格为空(或项null,不一定相同)时再次删除样式。没有办法抓住手机,事实上,你甚至不能尝试,因为当手机被使用或移除时,没有任何控制。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // Simple interface
        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        // Create the ListView
        ListView<String> listView = new ListView<>();
        listView.getItems().setAll("Title", "One", "Two", "Three", "Four", "Five");

        // Set the CellFactory for the ListView
        listView.setCellFactory(list -> {
            ListCell<String> cell = new ListCell<String>() {
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty || item == null) {
                        // There is no item to display in this cell, so leave it empty
                        setGraphic(null);

                        // Clear the style from the cell
                        setStyle(null);
                    } else {
                        // If the item is equal to the first item in the list, set the style
                        if (item.equalsIgnoreCase(list.getItems().get(0))) {
                            // Set the background color to blue
                            setStyle("-fx-background-color: blue; -fx-text-fill: white");
                        }
                        // Finally, show the item text in the cell
                        setText(item);

                    }
                }
            };
            return cell;
        });

        root.getChildren().add(listView);

        // Show the Stage
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}