我在包含进度栏的MainPanel
中有一个静态布尔变量inProgress。通讯器类将此变量的值更改为true false。
propertyListener如下
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("Property change check none");
if(true ==MainPanel.inProgress){
progressInformationPanel.getProgressBar().setIndeterminate(true);
progressInformationPanel.getCurrentProcesLabel().setText("Processing...");
}
else if(false ==MainPanel.inProgress){
System.out.println("Property change check none" + false);
progressInformationPanel.getProgressBar().setIndeterminate(false);
progressInformationPanel.getCurrentProcesLabel().setText("Finished....");
}
}
});
沟通者类是观察者。那是在听其他课程。现在,只要有进度通信器类就更新MainPanel的变量,但是
propertyChangeListener
不起作用。我不知道为什么会这样。也请在这里查看我链接的Question这是SSCCE
import java.awt.GridBagLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class MainFrame extends JFrame{
public static boolean inProgress = true;
private JProgressBar bar;
public Communicator diplayFacade;
public MainFrame() {
this.setSize(500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
diplayFacade = new Communicator();
bar = new JProgressBar();
setLayout(new GridBagLayout());
add(bar);
addProgressListener();
}
public void addProgressListener(){
bar.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if(true == MainFrame.inProgress){
System.out.println("Property change check none" + true);
bar.setIndeterminate(true);
}
else if(false ==MainFrame.inProgress){
System.out.println("Property change check none" + false);
bar.setIndeterminate(false);
}
}
});
}
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.diplayFacade.methodCalled();
}
}
和传播者阶层
import javax.swing.JOptionPane;
public class Communicator {
public void methodCalled(){
JOptionPane.showInputDialog("Hi");
MainFrame.inProgress = true;
}
}
最佳答案
问题是,我认为您不了解PropertyChangeListener
的实际作用。 PropertyChangeListener
监视对其注册的Object
属性的更改。
该通知不会偶然或自动发生,您通常需要通过PropertyChangeSupport
提供它,该通知可帮助您管理PropertyChangeListener
并触发事件...
通过将PropertyChangeListener
添加到JProgressBar
,可以监视对它的更改,而不是对框架的更改。同样,通过从JProgressBar
内部修改PropertyChangeListener
的状态,您可能会陷入无限循环,因为对象试图一遍又一遍地通知您已经进行的更改... 。
首先,您确实应该避免以这种方式使用static
,最好有一个实例变量和一个setter来更改其状态。您需要将框架的引用传递给有兴趣更改状态的人员,这又引发了另一个问题,您确实不想不必要地公开程序的元素,因此最好在某些情况下包含此功能。一种界面,例如...
public interface Progressable {
public void addPropertyChangeListener(PropertyChangeListener listener);
public void removePropertyChangeListener(PropertyChangeListener listener);
public void setInProgress(boolean inProgress);
public boolean isInProgress();
}
然后,您实现此接口。然后,
Communicator
将引用此接口的实例,并根据需要调用setInProgress
,例如...import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MainFrame extends JFrame implements Progressable {
private JProgressBar bar;
public Communicator diplayFacade;
private boolean inProgress;
public MainFrame() {
diplayFacade = new Communicator(this);
bar = new JProgressBar();
setLayout(new GridBagLayout());
add(bar);
addPropertyChangeListener("inProgress", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (isInProgress()) {
bar.setIndeterminate(true);
} else {
bar.setIndeterminate(false);
}
}
});
}
@Override
public void setInProgress(boolean value) {
if (inProgress != value) {
inProgress = value;
firePropertyChange("inProgress", !inProgress, inProgress);
}
}
@Override
public boolean isInProgress() {
return inProgress;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new MainFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
关于java - 在这种情况下,ProgressBarpropychangeListener不更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26249944/