Ok, sorry, you're correct. However, this'll only work for String. If you want some more flexibility, you'll need to create a binder yourself. Say you want a label that shows the i18n value of an enum, you can easily adapt my binder to show enums.
With even more customisation, you could do something like:
A converter that you need to implement:
Code:
public interface StringConverter
{
public String convertToString(Object toConvert);
}
And hand it to the slightly altered binder and binding:
Code:
public class StringLabelBinder implements Binder
{
private StringConverter converter;
public void setConverter(StringConverter converter) { this.converter = converter; }
public StringConverter getConverter()
{
if(converter == null)
{
converter = new Converter() { public String convertToString(Object object) { return object.toString(); } }
}
return converter;
}
@Override
public Binding bind(FormModel formModel, String formPropertyPath, Map context)
{
return new StringLabelBinding(formModel, formPropertyPath, converter);
}
@Override
public Binding bind(JComponent control, FormModel formModel, String formPropertyPath, Map context)
{
if (control instanceof JTextField)
{
return new StringLabelBinding((JTextField) control, formModel, formPropertyPath, converter);
}
else
{
throw new IllegalArgumentException("This binding's control must be a JTextField");
}
}
}
Code:
public class StringLabelBinding extends CustomBinding
{
private JTextField label;
private StringConverter converter;
public StringLabelBinding(FormModel formModel, String formPropertyPath, StringConverter converter)
{
this(new JTextField(), formModel, formPropertyPath, converter);
}
public StringLabelBinding(JTextField label, FormModel formModel, String formPropertyPath, StringConverter converter)
{
super(formModel, formPropertyPath, String.class);
this.label = label;
this.converter = converter;
}
@Override
protected void valueModelChanged(Object newValue)
{
label.setText(converter.convertToString(newValue));
}
@Override
protected JComponent doBindControl()
{
return label;
}
@Override
protected void enabledChanged()
{
label.setEnabled(isEnabled());
}
@Override
protected void readOnlyChanged()
{
label.setEditable(!isReadOnly());
}
@Override
protected boolean isReadOnly()
{
return true;
}
}
So now you have a flexible solution to show any object type in a label. You only need to implement the String converter and use that in the Binder.
But again, for Strings it's overkill, prashantbhat answer will do nicely.