「FXMLでComboBoxの選択肢を列挙型するには」の版間の差分
提供: tknotebook
| 29行: | 29行: | ||
# 列挙型が変更可能なら、列挙型の個々の要素の toString() メソッドを override する。 | # 列挙型が変更可能なら、列挙型の個々の要素の toString() メソッドを override する。 | ||
# ComboBox の セルファクトリークラスとリストセルクラスを書く。 | # ComboBox の セルファクトリークラスとリストセルクラスを書く。 | ||
| + | |||
| + | 後者は結構大変で、こんな感じになります。 | ||
| + | |||
| + | リストセルクラス | ||
| + | public class BlurTypeCell extends ListCell<BlurType> { | ||
| + | @Override | ||
| + | protected void updateItem(BlurType item, boolean empty) { | ||
| + | super.updateItem(item, empty); | ||
| + | if (empty) { | ||
| + | setText("なし"); | ||
| + | setGraphic(null); | ||
| + | } else { | ||
| + | setGraphic(null); | ||
| + | if (item == BlurType.ONE_PASS_BOX) { | ||
| + | setText("ワンパス"); | ||
| + | } else if (item == BlurType.TWO_PASS_BOX) { | ||
| + | setText("ツーパス"); | ||
| + | } else if (item == BlurType.THREE_PASS_BOX) { | ||
| + | setText("スリーパス"); | ||
| + | } else if (item == BlurType.GAUSSIAN) { | ||
| + | setText("ガウシアン"); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | セルファクトリークラス | ||
| + | public class BlurTypeCellFactory implements | ||
| + | Callback<ListView<BlurType>, ListCell<BlurType>> { | ||
| + | |||
| + | @Override | ||
| + | public ListCell<BlurType> call(ListView<BlurType> param) { | ||
| + | return new BlurTypeCell(); | ||
| + | |||
| + | } | ||
| + | |||
| + | ComboBox の FXML | ||
| + | <ComboBox> | ||
| + | <value> | ||
| + | <BlurType fx:value="THREE_PASS_BOX"/> | ||
| + | </value> | ||
| + | <cellFactory> | ||
| + | <BlurTypeCellFactory/> | ||
| + | </cellFactory> | ||
| + | <buttonCell> | ||
| + | <BlurTypeCell/> | ||
| + | </buttonCell> | ||
| + | <items> | ||
| + | <FXCollections fx:factory="observableArrayList"> | ||
| + | <BlurType fx:value="ONE_PASS_BOX" /> | ||
| + | <BlurType fx:value="TWO_PASS_BOX" /> | ||
| + | <BlurType fx:value="THREE_PASS_BOX" /> | ||
| + | <BlurType fx:value="GAUSSIAN" /> | ||
| + | </FXCollections> | ||
| + | </items> | ||
| + | </ComboBox> | ||
2016年6月20日 (月) 00:18時点における版
メインページ>コンピュータの部屋#JavaFX>JavaFX Tips
ComboBoxの選択肢を 列挙型のすると、ComboBoxのvalueプロパティを直接列挙型の プロパティにバインドできるので便利な場合があります。
BlurTypeを列挙型とすると、FXML で、ComboBoxをこんな具合に宣言すればOKです。
<ComboBox>
<value>
<BlurType fx:value="THREE_PASS_BOX"/>
</value>
<items>
<FXCollections fx:factory="observableArrayList">
<BlurType fx:value="ONE_PASS_BOX" />
<BlurType fx:value="TWO_PASS_BOX" />
<BlurType fx:value="THREE_PASS_BOX" />
<BlurType fx:value="GAUSSIAN" />
</FXCollections>
</items>
</ComboBox>
fx:value 属性はノードのクラスの valueOfメソッドで文字列からインスタンスの作成を指示します。
実行時、ComboBoxに表示される各項目の文字列は、列挙名そのままです。
列挙名がComboBoxで表示するのにふさわしくない名前ならば、対策は2つあります。
- 列挙型が変更可能なら、列挙型の個々の要素の toString() メソッドを override する。
- ComboBox の セルファクトリークラスとリストセルクラスを書く。
後者は結構大変で、こんな感じになります。
リストセルクラス
public class BlurTypeCell extends ListCell<BlurType> {
@Override
protected void updateItem(BlurType item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText("なし");
setGraphic(null);
} else {
setGraphic(null);
if (item == BlurType.ONE_PASS_BOX) {
setText("ワンパス");
} else if (item == BlurType.TWO_PASS_BOX) {
setText("ツーパス");
} else if (item == BlurType.THREE_PASS_BOX) {
setText("スリーパス");
} else if (item == BlurType.GAUSSIAN) {
setText("ガウシアン");
}
}
}
}
セルファクトリークラス
public class BlurTypeCellFactory implements
Callback<ListView<BlurType>, ListCell<BlurType>> {
@Override
public ListCell<BlurType> call(ListView<BlurType> param) {
return new BlurTypeCell();
}
ComboBox の FXML
<ComboBox>
<value>
<BlurType fx:value="THREE_PASS_BOX"/>
</value>
<cellFactory>
<BlurTypeCellFactory/>
</cellFactory>
<buttonCell>
<BlurTypeCell/>
</buttonCell>
<items>
<FXCollections fx:factory="observableArrayList">
<BlurType fx:value="ONE_PASS_BOX" />
<BlurType fx:value="TWO_PASS_BOX" />
<BlurType fx:value="THREE_PASS_BOX" />
<BlurType fx:value="GAUSSIAN" />
</FXCollections>
</items>
</ComboBox>