0

i have a two frame, frameA and frameB, in frameA i add one button with name buttonA to show frameB and progressbar (setIndeterminate(false)), in frameB i add one button with name buttonB , i want to when i click buttonB, progressbar in frameA.setindeterminate(true)

in frameA

frameB b;

public frameA() {
   initComponents();
   progressbar.setIndeterminate(false);
   b = new frameB();
}

public JProgressBar getProgressbar() {
   return progressbar;
}

private void buttonAActionPerformed(java.awt.event.ActionEvent evt) 
{                                       
    b.setVisible(true);
}

in frameB i use this code in event buttonB clicked

private void buttonBActionPerformed(java.awt.event.ActionEvent evt) { 
    frameA a= new frameA();
    a.getProgressbar().setIndeterminate(true);
}

but it doesnt worked

mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

2

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)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Yee, been watching that for a while :D - it's been sitting on 49, 999 :P – MadProgrammer Apr 05 '13 at 06:30
  • and probably the time to change avatar too:-) – mKorbel Apr 05 '13 at 06:36
  • please [see very interesting post about most of us (one post is about links to tutorial, next answer with an SSCCE)](http://meta.stackexchange.com/questions/175148/are-we-an-answer-factory), ignore that this forum is very offensive with various answers with full of nonsense by ...., simple I hate this forum – mKorbel Apr 05 '13 at 06:54