For an enumeration class, we want to store a meaningful English string in the database and display a meaningful Chinese string on the interface. So set two properties for the enumeration class, such as the following Datastatusenum (data State enumeration):
package com.lirong.javafx.demo.j3003;/** * <p>Title: 平台公共 -- 值对象</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2015</p> * <p>Compary: lrJAP.com</p> * * @author yujj * @version 1.1.1 * @date 2017-12-04 */public enum DataStatusEnum { INIT("Init", "初始化"), NORMAL("Normal", "正常"), FROZEN("Frozen", "冻结"), DISCARD("Discard", "作废"); private String code; private String value; DataStatusEnum(String code, String value) { this.code = code; this.value = value; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public String toString() { return getValue(); }}
In Datastatusenum, code is used to persist to the database, and value is used to display on the interface. There are four enumeration values.
Test class:
Package Com.lirong.javafx.demo.j3003;import Javafx.application.application;import Javafx.geometry.hpos;import Javafx.geometry.insets;import Javafx.scene.scene;import Javafx.scene.control.combobox;import Javafx.scene.control.label;import Javafx.scene.control.textarea;import javafx.scene.layout.ColumnConstraints; Import Javafx.scene.layout.gridpane;import javafx.scene.layout.priority;import javafx.stage.stage;/** * <p> Title:lirong Java Application platform</p> * Description: <br> * copyright:corprights lrjap.com<br> * company:lrjap.com<br> * * @author YUJJ * @version 1.1.1 * @date 2018-04-29 * @since 9.0.4 */public class Testenumco Mbobox extends application {public static void main (string[] args) {launch (args); } @Override public void start (Stage primarystage) throws Exception {label lblenum = new Label ("Data state:"); combobox<datastatusenum> comboenum = new combobox<> (); TextArea textconsole = new TextArea(); Gridpane Gridpane = new Gridpane (); Gridpane.setpadding (New Insets (10)); Gridpane.setvgap (10); Gridpane.sethgap (10); columnconstraints col1 = new Columnconstraints (); Col1.setpercentwidth (40); Columnconstraints col2 = new Columnconstraints (); Col2.setpercentwidth (60); Gridpane.getcolumnconstraints (). AddAll (col1, col2); Gridpane.addrow (0, Lblenum, comboenum); Label right-aligned gridpane.sethalignment (Lblenum, hpos.right); Use a ComboBox to fill the entire cell comboenum.setmaxwidth (double.max_value); Gridpane.sethgrow (Comboenum, priority.always); Assigns a value of Comboenum.getitems () to the ComboBox. SetAll (Datastatusenum.values ()); Monitor ComboBox Change Comboenum.getselectionmodel (). Selecteditemproperty (). AddListener (Observable, OldValue, NewValue)- > Textconsole.appendtext (String.Format ("Checked enumeration information, code=%s, value=%s. %s ", Newvalue.getcode (), Newvalue.getvalue (), System.getproperty (" lIne.separator "))); Gridpane.add (textconsole, 0, 1, 2, 2); Scene scene = new Scene (Gridpane, 400, 300); Primarystage.setscene (Scene); Primarystage.show (); }}
Operating effect:
J3003. JavaFX Component Extensions (iii)--enumcombobox