JavaFX の Scene Builder で作成された FXML ファイルから ComboBox のカスタム セル ファクトリを作成する際に、次の問題があります。ラベルのカスタム セル ファクトリを作成しました。ユーザーがアイテムをクリックすると、正常に機能します。y は「ボタン」エリアに表示されます。しかし、ユーザーが別のアイテムをクリックしたい場合、以前にクリックしたアイテムは消えてしまいます。
コンボボックス セル ファクトリのコードは次のとおりです。
idCardOnlineStatusComboBox.setCellFactory(new Callback<ListView<Label>, ListCell<Label>>() {
@Override public ListCell<Label> call(ListView<Label> param) {
final ListCell<Label> cell = new ListCell<Label>() {
@Override public void updateItem(Label item,
boolean empty) {
super.updateItem(item, empty);
if(item != null || !empty) {
setGraphic(item);
}
}
};
return cell;
}
});
セル工場に問題があると思いますが、どこにあるのかわかりません。
次のコードを使用して、fxml からコンボボックスを抽出します。
@FXML private ComboBox idCardOnlineStatusComboBox;
次に、コンボボックスにこれを入力します:
idCardOnlineStatusComboBox.getItems().addAll(
new Label(Resource.getStringFor("MainForm.Pane.MenuBar.Vortex.OnlineStatus.Online.Title"), new ImageView(onlineImg)),
new Label(Resource.getStringFor("MainForm.Pane.MenuBar.Vortex.OnlineStatus.Away.Title"), new ImageView(awayImg)),
new Label(Resource.getStringFor("MainForm.Pane.MenuBar.Vortex.OnlineStatus.DoNotDisturb.Title"), new ImageView(doNotDisturbImg)),
new Label(Resource.getStringFor("MainForm.Pane.MenuBar.Vortex.OnlineStatus.Invisible.Title"), new ImageView(offlineImg)),
new Label(Resource.getStringFor("MainForm.Pane.MenuBar.Vortex.OnlineStatus.Offline.Title"), new ImageView(offlineImg))
);