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
ConstructorsConstructorDescriptionSimpleStringConverter(javafx.util.Callback<T, String> valueToStringCallback) Constructor that requires callers to handle null values themselves.SimpleStringConverter(javafx.util.Callback<T, String> nonNullValueCallback, String nullDefaultValue) Constructor that automatically handles null values by returning a default null value string. -
Method Summary
-
Constructor Details
-
SimpleStringConverter
public SimpleStringConverter() -
SimpleStringConverter
Constructor that requires callers to handle null values themselves. The provided callback should handle conversion from value to String, including any necessary null handling.- Parameters:
valueToStringCallback- The callback to convert 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 value is null, the specified nullDefaultValue is returned instead of throwing an error or returning null.- Parameters:
nonNullValueCallback- The callback to convert non-null value to a String.nullDefaultValue- The default String value to return if the value is null.
-
-
Method Details
-
toString
- Specified by:
toStringin classjavafx.util.StringConverter<T>
-
fromString
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:
fromStringin classjavafx.util.StringConverter<T>- Parameters:
s- the string to be converted to an object.- Returns:
- always returns null.
-