Class SimpleStringConverter<T>

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

public class SimpleStringConverter<T> extends javafx.util.StringConverter<T>
A generic StringConverter implementation primarily used for displaying objects in the UI. This class provides flexible mechanisms for converting objects to Strings using custom callbacks. It also supports handling null values by returning a default string for null values.

This converter is typically used to format objects as strings for display purposes, with optional handling for null values. The conversion from string back to object is not usually required, hence the fromString(String) method returns null.

Usage Example:

Before using SimpleStringConverter:

comboBox.setConverter(new StringConverter<>() {
    @Override
    public String toString(Status status) {
        if (status != null) {
            return status.getDescription();
        }
        return "";
    }

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

After using SimpleStringConverter:

comboBox.setConverter(new SimpleStringConverter<>(Status::getDescription, ""));
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new SimpleStringConverter with a default callback: If the object is non-null, its toString() method is used. If the object is null, an empty string ("") is returned.
    SimpleStringConverter(javafx.util.Callback<T,String> valueToStringCallback)
    Creates a new SimpleStringConverter that uses the given callback to convert non-null values to strings.
    SimpleStringConverter(javafx.util.Callback<T,String> nonNullValueCallback, String nullDefaultValue)
    Constructor that automatically handles null values by returning a default null value string.
  • Method Summary

    Modifier and Type
    Method
    Description
    This method is not implemented and always returns null.
    toString(T object)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SimpleStringConverter

      public SimpleStringConverter()
      Creates a new SimpleStringConverter with a default callback:
      • If the object is non-null, its toString() method is used.
      • If the object is null, an empty string ("") is returned.
    • SimpleStringConverter

      public SimpleStringConverter(javafx.util.Callback<T,String> valueToStringCallback)
      Creates a new SimpleStringConverter that uses the given callback to convert non-null values to strings. This internally leverages the two-argument constructor with an empty string ("") as the default for null values.
      • If the object is non-null, valueToStringCallback is invoked to produce the string.
      • If the object is null, an empty string ("") is returned directly (the callback is not called).
      Parameters:
      valueToStringCallback - the callback to convert a non-null value to a String
    • SimpleStringConverter

      public SimpleStringConverter(javafx.util.Callback<T,String> nonNullValueCallback, String nullDefaultValue)
      Constructor that automatically handles null values by returning a default null value string.
      • If the object is non-null, the provided callback is used to convert it.
      • If the object is null, the specified nullDefaultValue is returned.
      Parameters:
      nonNullValueCallback - The callback to convert a non-null value to a String.
      nullDefaultValue - The default String value to return if the value is null.
  • Method Details

    • toString

      public String toString(T object)
      Specified by:
      toString in class javafx.util.StringConverter<T>
    • fromString

      public T fromString(String s)
      This method is not implemented and always returns null.

      Since the primary use of this converter is to display objects as strings in the UI, converting strings back to objects is not required. Therefore, this method simply returns null.

      Specified by:
      fromString in class javafx.util.StringConverter<T>
      Parameters:
      s - the string to be converted to an object.
      Returns:
      always returns null.