あなたが書いた場合、私は考えることができる唯一の例外はある(などListView
、TableView
、などのコントロールのためにデータ型として(例えば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 ;
}
私は今、項目を選択することができませんなぜ何らかの理由がありますこのリストに? – Philayyy
はい、 'super.updateItem(...)'への呼び出しを忘れました。今修正されました。 –
Jamesさん、もう一度大きな助けになりました – Philayyy