我正在使用Metawidget在GUI中自動查看/編輯對象中的值。我可以綁定對象的初始值,並在各自的GUI組件中查看它們。但是,當我更改GUI中的值時,這些更改不會同步回到對象。這或多或少有文件記錄here(已棄用)和here。使用Metawidget在GUI中使用值同步對象
這是我的目標:
public static class Person {
private String mName;
public String getName() { return this.mName; }
public void setName(String name) { this.mName = name; }
@UiAction
public void showPersonObject() {
JOptionPane.showMessageDialog(frame, this.mName);
}
@UiAction
public void bind() {
metawidget.getWidgetProcessor(
BeansBindingProcessor.class)
.save(metawidget);
}
}
這裏是我的主要方法,其中metawidget配置:
public static void main(String[] args) {
// Person
Person person = new Person();
person.setName("A cool name");
// Metawidget
metawidget = new SwingMetawidget();
metawidget.setInspector(new CompositeInspector(
new CompositeInspectorConfig().setInspectors(
new PropertyTypeInspector(),
new MetawidgetAnnotationInspector(),
new BeanValidationInspector())));
metawidget.addWidgetProcessor(
new BeansBindingProcessor(
new BeansBindingProcessorConfig().setUpdateStrategy(
UpdateStrategy.READ_WRITE)));
metawidget.setToInspect(person);
// Create Frame
...
}
在documentation據說:
如果設置爲READ或READ_WRITE(默認爲READ_ONCE),檢查對象 必須提供Pr opertyChangeSupport。如果設置爲 READ_WRITE,更新到UI自動sync'ed回 setToInspect,否則客戶端必須手動調用保存:
myMetawidget.getWidgetProcessor(BeansBindingProcessor.class).save(myMetawidget)
我試着將UpdateStrategy設置爲READ和/或READ_WRITE,和/或調用BeansBindingProcessor上的save()。我也試圖提供的PropertyChangeSupport到人對象(我認爲它指的this),這是一樣的添加以下代碼:
private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
public void addPropertyChangeListener(PropertyChangeListener listener) {
this.pcs.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
this.pcs.removePropertyChangeListener(listener);
}
public void setName(String name) {
String oldName = this.mName;
this.mName = name;
this.pcs.firePropertyChange("name", oldName, mName);
}
然而,人對象總是保持原始值。
在此先感謝。