简体   繁体   English

在JavaFX中使用UI显示和创建枚举的最佳实践是什么?

[英]What is the best practice to show and create an enum using UI in JavaFX?

I have a form with a ComboBox which I'd like to be populated with available values of MyEnum type: 我有一个带有ComboBox的表单,我想用MyEnum类型的可用值填充该表单:

public enum MyEnum {
  FIRST_CHOICE ("First choice"),
  SECOND_CHOICE ("Second choice");

  private String value;

  MyEnum(String value) {
    this.value = value;
  }

  public String toString() {
    return value;
  }
}

myComboBox.getItems().setAll(MyEnum.values());

Then, after making a choice and hitting a button for example, I'd like to create a new MyEnum object. 然后,例如在做出选择并按下按钮后,我想创建一个新的MyEnum对象。 The ideas I came up with are: 我想到的想法是:

Option 1: 选项1:

Get values from the ComboBox and using switch statement, create a new MyEnum regarding to the choice taken. 从ComboBox获取值,并使用switch语句,根据所选择的内容创建一个新的MyEnum

Option 2: 选项2:

Make the String values same as the enumerated ones and create MyEnum using valueOf() : 使String值与枚举值相同,并使用valueOf()创建MyEnum

public enum MyEnum {
  FIRST_CHOICE ("FIRST_CHOICE"),
  SECOND_CHOICE ("FIRST_CHOICE");

  ...
}

MyEnum myEnum = MyEnum.valueOf(myComboBox.getSelectionModel().getSelectedItem())

--------- ---------

So the first option is terrible, I know. 我知道第一种选择很糟糕。 Second is a bit better but no so user-friendly (since he will see a text like "FIRST_CHOICE") and I suppose there are better options. 第二个要好一点,但不是那么用户友好(因为他会看到类似“ FIRST_CHOICE”的文字),我想还有更好的选择。 I'd like to make it in the best possible way, with separation of View and Model as much as I can but I totally don't know how can I achieve that. 我想尽可能地做到最好,将View和Model尽可能地分开,但是我完全不知道如何实现。

I'd go with "option 3", and use the a StringConverter . 我会选择“ option 3”,并使用StringConverter This will allow you to use enum "under the hood" whilst displaying a more suitable String to the user. 这将允许您在“幕后”使用枚举,同时向用户显示更合适的String。 This will also allow different strings in different situations, eg internationalization, or different comboboxes on top of same underlying type. 这还将允许在不同情况下(例如国际化)使用不同的字符串,或在相同基础类型的顶部使用不同的组合框。

Here I've used a case statement as an example, but could look the value up from properties file etc. 在这里,我以case语句为例,但可以从属性文件等中查找值。

ComboBox<MyEnum> myComboBox = new ComboBox<>();
myComboBox.getItems().setAll(MyEnum.values());
myComboBox.setConverter(new StringConverter<MyEnum>() {

    @Override
    public String toString(MyEnum object) {
        switch (object) {
        case FIRST_CHOICE:
            return "foo";
        case SECOND_CHOICE:
            return "bar";
        default:
            break;
        }
        return null;
    }

    @Override
    public MyEnum fromString(String string) {
        return null;
    }
});

例

The value returned by the ComboBox will still be of type MyEnum, this can be demonstrated by ComboBox返回的值仍将是MyEnum类型,这可以通过以下方式证明:

myComboBox.valueProperty().addListener((obs, oldValue, newValue) -> {
    System.out.println(myComboBox.getValue());
});

Output 产量

SECOND_CHOICE
FIRST_CHOICE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM