I wrote a simple class to bind value change of a component to my bean. Someone must have done it many x 10000... times before. :p
Hope it helps.
Require:
common-beanutils
package com.dummy;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JSpinner;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.JTextComponent;
import org.apache.commons.beanutils.BeanUtils;
public class BeanBinder {
public static void bind(final JSpinner jSpinner, final Object object,
final String attribute) {
jSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent arg0) {
try {
BeanUtils.setProperty(object, attribute,
jSpinner.getValue());
System.out.println(object);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
});
}
public static void bind(final JTextComponent textComponent,
final Object object, final String attribute) {
textComponent.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent arg0) {
try {
BeanUtils.setProperty(object, attribute,
textComponent.getText());
System.out.println(object);
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (InvocationTargetException e1) {
e1.printStackTrace();
}
}
});
}
}
Usage: