This...
private void buttonBActionPerformed(java.awt.event.ActionEvent evt) {
frameA a= new frameA();
a.getProgressbar().setIndeterminate(true);
}
Isn't going to work, you've just created another instance of frameA
that's not visible. It has no relationship to the frame that is currently open.
There are any number of ways you could achieve this...
You could...
Pass a reference of frameA
to frameB
as part of the constructor call for frameB
. Then in you actionPerformed
method, you would simply use this reference to change the progress bar state.
But this would create a tight coupling between frameA
and frameB
which would greatly reduce the re-use of frameB
You could...
Provide a means by which an interested party could attach an ActionListener
to frameB
which would be triggered when the button is clicked.
This assumes a work flow and exposes components to outside sources (via the ActionEvent#getSource
), which could allow people to change your component...
You probably should...
Fire a PropertyChanged
event.
This is probably the easiest and safest of all the options I've come up with. Using a property change listener in this manner means you don't need to expose the JProgressBar
, the JButton
or produce a tight link between the two frames.
Property change support is built in to Container
so all components/controls have it.
For example, you would attach a PropertyChangeListener
to b
when you construct it.
b = new frameB();
b.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("progressState")) {
progressbar.setIndeterminate((Boolean)evt.getNewValue());
}
}
});
Add in bFrame
's actionPerformed
method, you would simple call...
firePropertyChange("progressState", false, true);
When you want to set the indeterminate state (you could swap the boolean
values to reset it if you wanted to)