第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在javafx中僅更改listview中第一個(gè)單元格的背景顏色?

如何在javafx中僅更改listview中第一個(gè)單元格的背景顏色?

喵喵時(shí)光機(jī) 2021-12-10 17:09:15
如何在 JavaFX 中僅更改列表視圖中第一個(gè)單元格的背景顏色?我只想更改列表視圖中第一個(gè)單元格的背景顏色。有沒(méi)有辦法做到這一點(diǎn)。
查看完整描述

1 回答

?
嚕嚕噠

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊

您需要CellFactory在ListView. 然后我們可以確定該單元格是否屬于List您用來(lái)填充Listview. 如果是這樣,請(qǐng)僅對(duì)該單元格應(yīng)用不同的樣式。


我不知道是否有一種方法來(lái)確定第一小區(qū)的ListView,但我們一定可以捕捉在第一項(xiàng)List。


考慮以下應(yīng)用程序。我們有一個(gè)ListView只顯示字符串列表的。


我們?cè)O(shè)置自定義CellFactory的ListView,并設(shè)置單元格樣式,如果item是在第一List填充ListView。


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

    }

}

結(jié)果

http://img1.sycdn.imooc.com//61b319540001cf7b02830298.jpg

顯然,您需要進(jìn)行一些調(diào)整以匹配您的數(shù)據(jù)模型,并且僅通過(guò) a 進(jìn)行匹配String并不是最好的方法。

這不會(huì)阻止用戶選擇第一個(gè)項(xiàng)目,并且如果在構(gòu)建場(chǎng)景后對(duì)列表進(jìn)行排序,則可能無(wú)法按預(yù)期工作。

雖然這可能會(huì)回答您的直接問(wèn)題,但為了確保為用戶提供良好的體驗(yàn),還需要考慮其他事項(xiàng)。


查看完整回答
反對(duì) 回復(fù) 2021-12-10
  • 1 回答
  • 0 關(guān)注
  • 620 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)