2016-10-17 8 views
0

条件に基づいてリストビューで個々のセルのスタイルを設定しようとしています。リストビューはラベルタイプであり、ラベルはリストビューに挿入されるときに別のクラスに基づいて条件が決定されます。JavaFXの条件に基づいてリストビューセルのスタイルを設定する方法

現在、リストビューで各ラベルの背景色を設定しようとしましたが、領域全体をカバーしていません(画像参照)。

enter image description here

また、私はこの記事を研究してきましたが、それは、文字列で運ばれていないリストビューのセルを強調表示するための識別子として私の場合に適していません。これまで

How do I dynamically change the background of an item in a listview in JavaFX

現在のコード:

for (Consultation c : notifications){ 
      Label l = new Label(": consult reminder - " + c.getReminderTime()); 

      if(c.getReminderRead() == 1){ //if the consultation reminder hasn't been read 
       l.setStyle("-fx-background-color: #33CEFF"); 
       counter = counter + 1; 
      } 


      notificationList.getItems().add(l); 
} 

任意のアイデア?

答えて

2

あなたが書いた場合、私は考えることができる唯一の例外はある(などListViewTableView、などのコントロールのためにデータ型として(例えばLabelなど)ノードのサブクラスを使用することが常に基本的に誤りでありますデータが実際のGUIノードであったシーンビルダなどのGUIビルダー。それでもうまくいかない場合もあります。)GUIノードクラスは非常に高価です。彼らは通常、スタイリングやCSSの解析、イベントリスナーなどと関連した数百のプロパティとオーバーヘッドを持っています。実際のデータクラスConsultationと比較してください。おそらくダース以下のプロパティを持ち、他のオーバーヘッドはありません。 Labelまたは他のノードクラスをListViewのデータ型として使用する場合は、の項目ごとにノードを1つ作成します。 ListViewの全体のポイントは、可視のセルのノードのみを作成し、それらのセルを再利用することです。だから、あなたが大きなリストを持っているなら、あなたのアプローチはパフォーマンス上の問題につながるでしょう。 (他のポイントは、あなたがモデル(データ)からビュー(プレゼンテーション)の分離に違反していることである。)

ですから、本当にここListView<Consultation>を持っている、とするセルファクトリを使用する必要がありますセルのテキストとスタイルを設定します。

ListView<Consultation> notificationList = ... ; 

notificationList.setCellFactory(lv -> new ListCell<Consultation>() { 
    @Override 
    protected void updateItem(Consultation c, boolean empty) { 
     super.updateItem(c, empty); 
     if (empty) { 
      setText(null); 
      setStyle(""); 
     } else { 
      setText(": consult reminder - " + c.getReminderTime()); 

      // Are you REALLY using an int for the reminderRead property??!? 
      // Surely this should be a boolean... 

      if (c.getReminderRead() == 1) { // I feel dirty just writing that 
       setStyle("-fx-background-color: #33CEFF"); 
      } else { 
       setStyle(""); 
      } 
     } 
    } 
}); 

notificationList.getItems().setAll(notifications); 

外部CSSファイルにスタイルを組み込むのが少し良いです。

ListView<Consultation> notificationList = ... ; 

PseudoClass reminderRead = PseudoClass.getPseudoClass("reminder-read"); 

notificationList.setCellFactory(lv -> new ListCell<Consultation>() { 
    @Override 
    protected void updateItem(Consultation c, boolean empty) { 
     super.updateItem(c, empty); 
     if (empty) { 
      setText(null); 
     } else { 
      setText(": consult reminder - " + c.getReminderTime()); 
     } 

     // assumes Consultation.getReminderRead() returns a boolean... 
     pseudoClassStateChanged(reminderRead, c != null && c.getReminderRead()) 
    } 
}); 

notificationList.getItems().setAll(notifications); 

今、あなたが行うことができますあなたの外部CSSファイルに:あなたは、オンとオフの異なるスタイルをオンにするCSS疑似クラスを使用することができます

.list-cell:reminder-read { 
    -fx-background-color: #33CEFF ; 
} 
+0

私は今、項目を選択することができませんなぜ何らかの理由がありますこのリストに? – Philayyy

+0

はい、 'super.updateItem(...)'への呼び出しを忘れました。今修正されました。 –

+0

Jamesさん、もう一度大きな助けになりました – Philayyy

関連する問題