亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JavaFX ListView 中神秘的“必須指定顏色組件或名稱”錯誤

JavaFX ListView 中神秘的“必須指定顏色組件或名稱”錯誤

回首憶惘然 2023-05-10 14:20:17
我試圖通過嘗試顯示彩色矩形而不是字符串本身來自定義 Javafx 中的 ListView。public class Main extends Application {@Overridepublic void start(Stage primaryStage){    VBox vBox = new VBox();    ListView<String> listView = new ListView<>();    vBox.getChildren().add(listView);    ObservableList<String> list = FXCollections.observableArrayList("black" , "blue" , "brown" , "gold");    listView.setItems(list);    listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {        @Override        public ListCell<String> call(ListView<String> stringListView) {            return new cell();        }    });    primaryStage.setScene(new Scene(vBox,400 , 400));    primaryStage.show();}public class cell extends ListCell<String>{    Rectangle rect;    cell() {        super();        this.rect = new Rectangle(20,20);        this.rect.setFill(Color.web(getItem()));      // ERROR  ERROR  ERROR        setGraphic(this.rect);    }    @Override    protected void updateItem(String s, boolean empty) {        super.updateItem(s, empty);        if(empty)            setGraphic(null);        else             setGraphic(this.rect);    }}public static void main(String[] args) {    launch(args);}}顯然,錯誤出現在我指示為 ERROR 的行中。我稍微操縱了細胞類,它起作用了。下面是被操縱的細胞類:public class cell extends ListCell<String>{    Rectangle rect;    cell() {        super();        this.rect = new Rectangle(20,20);       // this.rect.setFill(Color.web(getItem()));        setGraphic(this.rect);    }    @Override    protected void updateItem(String s, boolean empty) {        super.updateItem(s, empty);        if(empty)            setGraphic(null);        else {            rect.setFill(Color.web(getItem()));            setGraphic(this.rect);        }    }我知道 updateItem() 會被調用很多次。我的第一個方法確實減少了 updateItem() 完成的工作,但由于某種原因它在該行中拋出錯誤。以前的方法出錯的原因可能是什么
查看完整描述

1 回答

?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

itema 的屬性最初ListCellnull。Color.web不接受null作為參數。此外,您需要能夠處理這樣一個事實,即 a 的項目ListCell可以在其生命周期內被替換,并且相同的項目可以分配給不同的單元格。ListView只創建填充視圖所需的單元格,如果例如可滾動區域的視口發生變化,則需要顯示不同的項目,并且單元格被重用以顯示更改后的項目集。

如果您擔心 中某些計算的性能updateItem,您可以將結果緩存在映射中(如果SoftReference您擔心內存消耗,可以將值包裝在 s 中)。

在這種情況下,這是沒有必要的,因為:

  1. Color.web不貴,

  2. 命名的顏色,比如你使用的項目,Map無論如何都存儲在一個地方;Color無論將相同參數傳遞給 . 的頻率如何,每種不同的命名顏色都只會創建一個實例Color.web。

順便說一句:我不建議以不能成為調用結果的方式初始化單元格updateItem。在您的情況下,graphic空單元格的屬性不null包括初始狀態。如果您擔心一致的單元格大小,最好始終保留圖形并設置其可見性:

public class cell extends ListCell<String> {

    private final Rectangle rect;


    cell() {

        super();

        this.rect = new Rectangle(20,20);

        setGraphic(this.rect);

    }


    @Override

    protected void updateItem(String s, boolean empty) {

        super.updateItem(s, empty);

        if(empty)

            rect.setVisible(false);

        else {

            rect.setFill(Color.web(getItem()));

            rect.setVisible(true);

        }

    }

}


查看完整回答
反對 回復 2023-05-10
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號