netbeans IDEを使用してBeanバインディングを使用しようとしています。ラベルのテキストを更新したいと思います。これが私が作成したBeanです。
public class SystemTimeBean implements Serializable {
public static final String PROP_SAMPLE_PROPERTY = "systemTime";
private String systemTime;
private PropertyChangeSupport propertySupport;
public SystemTimeBean() {
propertySupport = new PropertyChangeSupport(this);
}
public String getSystemTime() {
return systemTime;
}
public void setSystemTime(String value) {
String oldValue = systemTime;
systemTime = value;
propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, systemTime);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertySupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertySupport.removePropertyChangeListener(listener);
}
}
public class SystemTimeModel {
private long systemTime;
private SystemTimeBean bean;
public SystemTimeModel() {
bean = new SystemTimeBean();
}
public long getSystemTime() {
return systemTime;
}
public void setSystemTime(long systemTime) {
this.systemTime = systemTime;
bean.setSystemTime(Long.toString(systemTime));
}
}
私のJFrameでバインドするためのコード
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
systemTimeBean1 = new beansbindingapp.SystemTimeBean();
lblBinding = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, systemTimeBean1, org.jdesktop.beansbinding.ELProperty.create("${systemTime}"), lblBinding, org.jdesktop.beansbinding.BeanProperty.create("text"));
bindingGroup.addBinding(binding);
bindingGroup.bind();
および Main.class
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new SystemTimeFrame();
frame.setVisible(true);
}
});
SystemTimeModel time = new SystemTimeModel();
for(int i = 0; i < 10; i++) {
time.setSystemTime(System.currentTimeMillis());
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
ラベルは更新されません。手伝ってくれてありがとう。