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
ConstructorsConstructorDescriptionCreates a newSimpleStringConverterwith a default callback: If the object is non-null, itstoString()method is used. If the object is null, an empty string ("") is returned.SimpleStringConverter(javafx.util.Callback<T, String> valueToStringCallback) Creates a newSimpleStringConverterthat 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
-
Constructor Details
-
SimpleStringConverter
public SimpleStringConverter()Creates a newSimpleStringConverterwith a default callback:- If the object is non-null, its
toString()method is used. - If the object is null, an empty string ("") is returned.
- If the object is non-null, its
-
SimpleStringConverter
Creates a newSimpleStringConverterthat 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,
valueToStringCallbackis 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
- If the object is non-null,
-
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
nullDefaultValueis 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
-
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.
-