2

Any one can give a sample code or a link how to bind a JProgressbar to a bean class'property in Swing applications?

divibisan
  • 11,659
  • 11
  • 40
  • 58
Débora
  • 5,816
  • 28
  • 99
  • 171
  • what do you mean by "class property"? Anyway, nothing special in a JProgressbar - do it the same way as with any other property on any other component – kleopatra Oct 10 '11 at 08:09

1 Answers1

2

You could use JGoodies Binding to bind the progress bar to your model. But your (view-)model must fire property change events for this to work. http://www.jgoodies.com/downloads/libraries.html I can post an example code on monday.

Example:

In your 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);
}

In your View:

BeanAdapter<ViewModel> beanAdapter = new BeanAdapter<ViewModel>(viewModel, true);
Bindings.bind(progressBar, "value", beanAdapter.getValueModel(ViewModel.PROPERTY_PROGRESS));
Gandalf
  • 2,350
  • 20
  • 28