如何将JProgressbar绑定到Bean类的属性

时间:2011-10-09 12:12:27

标签: java swing desktop-application beans-binding

任何人都可以提供示例代码或链接如何将JProgressbar绑定到Swing应用程序中的bean类属性?

1 个答案:

答案 0 :(得分:2)

您可以使用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));