2

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

1 に答える 1

2

消える動作はバグの可能性があります。それを JavaFX Jira にファイルして、オラクルの担当者にさらに決定してもらうことができます。ComboBox.setCellFactory(...)さらに、この動作の理由についてソース コードを調査し、回避策を見つけることができます。しかし、私の提案は、あなたの代わりにComboBox Cell の ( ListCell) 内部コンポーネントを使用することです:Labelled

@Override
public void updateItem(Label item, boolean empty) {
    super.updateItem(item, empty);
    if (item != null && !empty) {
        setText(item.getText());
        setGraphic(item.getGraphic());
    } else {
        setText(null);
        setGraphic(null);
    }
}

コードのelse部分に注意してください。ifステートメントを書くときのすべてのユースケースをカバーしてください。

于 2013-09-14T14:44:16.837 に答える