ComboBox を拡張し、ボタンのグラフィックをカスタマイズして、リスト内の項目が選択されているかどうかに応じてチェック済み/未チェックの状態を表示するようにしています。デフォルトでは値が選択されていないため、プロンプトテキストが表示されるだけで、「チェックされていない」グラフィックを表示したいのですが、ユーザーが値を選択するまで、どのようなグラフィックも表示されないようです。これは、グラフィックを使用する目的を無効にします...
public class ComponentComboBox extends ComboBox<String> {
String promptText;
boolean promptAsPrefix = true; // keeps the prompt as a prefix to the selected item
final ImageView checked = new ImageView( new Image( Main.actions.getClass().getResourceAsStream( "res/checked.png" ) ) );
final ImageView unchecked = new ImageView( new Image( Main.actions.getClass().getResourceAsStream( "res/unchecked.png" ) ) );
public ComponentComboBox( String promptText, boolean promptAsPrefix, int maxWidth ) {
init( promptText, promptAsPrefix, maxWidth );
}
private void init( final String promptText, final boolean promptAsPrefix, int maxWidth ) {
this.promptText = promptText;
this.promptAsPrefix = promptAsPrefix;
setPromptText( promptText );
setMaxWidth( maxWidth );
setPrefWidth( maxWidth );
Callback<ListView<String>,ListCell<String>> cb = new Callback<ListView<String>,ListCell<String>>() {
@Override public ListCell<String> call( ListView<String> p ) {
ListCell<String> ret = new ListCell<String>() {
@Override protected void updateItem( String item, boolean empty ) {
super.updateItem( item, empty );
if ( item == null || empty ) {
setGraphic( unchecked );
}
else {
setGraphic( checked );
if ( promptAsPrefix ) {
setText( promptText + " " + item );
}
}
}
};
ret.setGraphic( unchecked );
return ret;
}
};
setButtonCell( cb.call( null ) );
}
}
値が選択されていないときにカスタム グラフィックを表示することは可能ですか?