業務で使用する場面としては、"○○区分"というものを表現するのによく使います。
そしてそれを画面では<select>として表示することもよくあります。
このとき、画面に表示するテキストをenum自身から分離するための方法です。
enumを定義する
普通にenumを定義します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum RockPaperScissors { | |
ROCK,PAPER,SCISSORS; | |
} |
アプリケーションのプロパティファイルに表示する文字列を定義する
型名.フィールド名=文字列の形式で定義します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# for RockPaperScissors | |
RockPaperScissors.ROCK=\u30b0\u30fc | |
RockPaperScissors.PAPER=\u30d1\u30fc | |
RockPaperScissors.SCISSORS=\u30c1\u30e7\u30ad |
DropDownChoiceのコンストラクタにEnumChoiceRendererを渡す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RockPaperScissorsDropDownChoice | |
extends DropDownChoice<RockPaperScissors> { | |
public RockPaperScissorsDropDownChoice(String id) { | |
super(id, | |
Arrays.asList(RockPaperScissors.values()), | |
new EnumChoiceRenderer<RockPaperScissors>()); | |
} | |
} |
画面ごとに表示するテキストを変えたい場合、以下のようにします。
- 画面ごとにプロパティファイルを作成する
- EnumChoiceRendererのコンストラクタにPageのインスタンスを渡す