Class EnumStringConverter<T extends Enum<T>>

java.lang.Object
javafx.util.StringConverter<T>
com.dlsc.gemsfx.util.SimpleStringConverter<T>
com.dlsc.gemsfx.util.EnumStringConverter<T>
Type Parameters:
T - the type of the Enum to be converted.

public class EnumStringConverter<T extends Enum<T>> extends SimpleStringConverter<T>
A specialized StringConverter implementation for Enum types. This converter provides a default mechanism to format Enum values in a title case, replacing underscores with spaces and capitalizing the first letter of each word. If the enum value is null, it returns an empty string.

This class extends SimpleStringConverter and leverages EnumUtil to provide a default formatting for enum values. It can also accept custom callbacks for more specific conversion requirements.

Usage Example:

Default usage with title case formatting:

ComboBox<MyEnum> comboBox = new ComboBox<>();
comboBox.setConverter(new EnumStringConverter<>());

Custom callback for specific formatting:

ComboBox<MyEnum> comboBox = new ComboBox<>();
comboBox.setConverter(new EnumStringConverter<>(myEnum -> "Custom format: " + myEnum.name()));
  • Constructor Details

    • EnumStringConverter

      public EnumStringConverter()
      Converts an enum value to title case, replacing underscores with spaces and capitalizing the first letter of each word. If the enum value is null, returns an empty string.

      Example: 1. null -> ""

      Example: 2. MY_ENUM_VALUE -> My Enum Value

    • EnumStringConverter

      public EnumStringConverter(javafx.util.Callback<T,String> valueToStringCallback)
      Constructor that accepts a custom callback for converting enum values to strings. The provided callback should handle conversion from enum value to String, including any necessary null handling.
      Parameters:
      valueToStringCallback - The callback to convert enum value to a String.
    • EnumStringConverter

      public EnumStringConverter(javafx.util.Callback<T,String> nonNullValueCallback, String nullDefaultValue)
      Constructor that accepts a custom callback for converting non-null enum values to strings and a default string to return if the enum value is null.
      Parameters:
      nonNullValueCallback - The callback to convert non-null enum value to a String.
      nullDefaultValue - The default String value to return if the enum value is null.