Top Description Fields Constructors Methods
com.dlsc.gemsfx.util

public Class SimpleStringConverter<T>

extends StringConverter<T>
Class Inheritance
Known Direct Subclasses
com.dlsc.gemsfx.util.EnumStringConverter
Type Parameters
<T>
the type of the object to be converted.
Imports
javafx.util.Callback, .StringConverter, java.util.Optional

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, ""));

Field Summary

Modifier and TypeField and Description
private final Callback<T, String>

Constructor Summary

AccessConstructor and Description
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.
public
SimpleStringConverter(Callback<T, String>
the callback to convert a non-null value to a String
valueToStringCallback
)

Creates a new SimpleStringConverter that uses the given callback to convert non-null values to strings.

public
SimpleStringConverter(Callback<T, String>
The callback to convert a non-null value to a String.
nonNullValueCallback
,
String
The default String value to return if the value is null.
nullDefaultValue
)

Constructor that automatically handles null values by returning a default null value string.

Method Summary

Modifier and TypeMethod and Description
public T

Returns:

always returns null.
fromString
(String
the string to be converted to an object.
s
)

Implements abstract javafx.util.StringConverter.fromString.

This method is not implemented and always returns null.

public String
toString(T object)

Implements abstract javafx.util.StringConverter.toString.

Converts the object provided into its string form.

Field Detail

valueToStringCallbackback to summary
private final Callback<T, String> valueToStringCallback

Constructor Detail

SimpleStringConverterback to summary
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.
SimpleStringConverterback to summary
public SimpleStringConverter(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:Callback<T, String>

the callback to convert a non-null value to a String

SimpleStringConverterback to summary
public SimpleStringConverter(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:Callback<T, String>

The callback to convert a non-null value to a String.

nullDefaultValue:String

The default String value to return if the value is null.

Method Detail

fromStringback to summary
public T fromString(String s)

Implements abstract javafx.util.StringConverter.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.

Parameters
s:String

the string to be converted to an object.

Returns:T

always returns null.

Annotations
@Override
toStringback to summary
public String toString(T object)

Implements abstract javafx.util.StringConverter.toString.

Doc from javafx.util.StringConverter.toString.

Converts the object provided into its string form. Format of the returned string is defined by the specific converter.

Annotations
@Override