任何人都可以给出示例代码或链接,以了解如何在Swing应用程序中将JProgressbar绑定到bean类的属性吗?
最佳答案
您可以使用JGoodies Binding将进度条绑定到模型。但是,您的(view-)模型必须触发属性更改事件才能起作用。
http://www.jgoodies.com/downloads/libraries.html我可以在星期一发布示例代码。
例:
在您的ViewModel中:
private final PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
private int progress;
public void addPropertyChangeListener(PropertyChangeListener listener)
{
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener)
{
changeSupport.removePropertyChangeListener(listener);
}
public int getProgress()
{
return progress;
}
public static final String PROPERTY_PROGRESS = "progress";
public void setProgress(int progress)
{
int old = this.progress;
this.progress = progress;
changeSupport.firePropertyChange(PROPERTY_PROGRESS, old, progress);
}
在您看来:
BeanAdapter<ViewModel> beanAdapter = new BeanAdapter<ViewModel>(viewModel, true);
Bindings.bind(progressBar, "value", beanAdapter.getValueModel(ViewModel.PROPERTY_PROGRESS));