how to change setIndeterminate jProgressbar in other frame

偶尔善良 提交于 2019-12-12 05:38:58

问题


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


回答1:


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)



来源:https://stackoverflow.com/questions/15826951/how-to-change-setindeterminate-jprogressbar-in-other-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!