J3003.JavaFX组件扩展(三)——EnumComboBox

时间:2022-03-08 05:28:08

对于枚举类,我们希望在数据库中存放一个有意义的英文字符串,在界面上显示一个有意义的中文字符串。所以为枚举类设置两个属性,如以下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