简体   繁体   English

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

[英]How can I change the background color of only the first cell in listview in javafx?

How can I change the background color of only the first cell in listview in JavaFX?I only want to change the background color of the first cell in the listview. 我如何只更改JavaFX中列表视图中第一个单元格的背景颜色?我只想更改列表视图中第一个单元格的背景颜色。 Is there any way to do this. 有什么办法可以做到这一点。

You would need to implement a custom CellFactory on the ListView . 您将需要在ListView上实现自定义CellFactory We can then determine if the cell belongs to the first item in the List you used to populate the Listview . 然后,我们可以确定该单元格是否属于用于填充ListviewList的第一项。 If so, apply a different style to just that cell. 如果是这样,则仅对该单元格应用其他样式。

I am not aware if there is a way to determine the first cell of a ListView , but we can certainly capture the first item in a List . 我不知道是否有办法确定ListView的第一个单元格 ,但是我们当然可以捕获List的第一个项目。

Consider the following application. 考虑以下应用程序。 We have a ListView that just displays a list of strings. 我们有一个ListView仅显示字符串列表。

We set a custom CellFactory on the ListView and set the cell style if the item is the first in the List populating the ListView . 我们在ListView上设置自定义CellFactory ,并设置单元格样式(如果该项是填充ListViewList的第一个item )。


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();
    }
}

The Result 结果

截图


Obviously, you'll need to make some adjustments to match your data model and just matching by a String would not be the best approach. 显然,您需要进行一些调整以匹配您的数据模型,仅通过String匹配并不是最佳方法。

This does not prevent the user from selecting the first item and may not work as expected if the list is sorted after building the Scene. 这不会阻止用户选择第一项,并且如果在构建场景后对列表进行了排序,则可能无法按预期工作。

While this may answer your direct question, there are other things to consider in order to ensure a good experience for the user. 尽管这可能会回答您的直接问题,但为了确保用户的良好体验,还需要考虑其他事项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM