1. 程式人生 > >J3003.JavaFX組件擴展(三)——EnumComboBox

J3003.JavaFX組件擴展(三)——EnumComboBox

src 枚舉值 getitem ole size 枚舉 all oid 持久

對於枚舉類,我們希望在數據庫中存放一個有意義的英文字符串,在界面上顯示一個有意義的中文字符串。所以為枚舉類設置兩個屬性,如以下DataStatusEnum(數據狀態枚舉):

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();
    }
}

DataStatusEnum中,code用於持久化到數據庫中,value用於在界面上展示。其中有四個枚舉值。

測試類:

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 TestEnumComboBox extends Application {

    public static void main(String[] args) {

        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        Label lblEnum = new Label("數據狀態:");
        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右對齊
        GridPane.setHalignment(lblEnum, HPos.RIGHT);
        // 使用Combobox充滿整個Cell
        comboEnum.setMaxWidth(Double.MAX_VALUE);
        GridPane.setHgrow(comboEnum, Priority.ALWAYS);
        // 為ComboBox賦值
        comboEnum.getItems().setAll(DataStatusEnum.values());

        // 監聽ComboBox變化
        comboEnum.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) ->
                textConsole.appendText(String.format("選中的枚舉信息,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();
    }
}

運行效果:

技術分享圖片

J3003.JavaFX組件擴展(三)——EnumComboBox