我有一个挥杆应用程序。我正在尝试展示装载玻璃窗格 从数据库中读取数据。但这种行为并不是我所期待的。
public class Sample {
JFrame frame;
JPanel loadingPanel;
JLabel iconLabel;
JPanel glassPane;
private PropertyChangeSupport changeListeners = new PropertyChangeSupport(this);
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Sample window = new Sample ();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Sample () {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setSize(new Dimension(500,500));
frame.getContentPane().add(panel);
panel.setLayout(null);
// ---------Select button--------------------------------
JButton btnSelect = new JButton("Select");
btnSelect.addActionListener(loadDataActionListner);
btnSelect.setBounds(132, 7, 137, 32);
btnSelect.setFocusPainted(false);
panel.add(btnSelect);
glassPane = new GlassPanel();
glassPane.setOpaque(false);
glassPane.setLayout(new GridBagLayout());
glassPane.add(new JLabel("loading... "));
frame.setGlassPane(glassPane);
ChangeListener listener = new ChangeListener(this);
changeListeners.addPropertyChangeListener(listener);
}
public void changeVisibility(boolean visibility){
glassPane.setVisible(visibility);
frame.validate();
frame.repaint();
}
ActionListener loadDataActionListner = new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
changeListeners.firePropertyChange("visibility", false, true); //Line 01
Response response = DataManager.getInstance().loadData("myfile.txt");
changeListeners.firePropertyChange("visibility", true, false);
}
}
});
}
};
}
public class GlassPaneUpdateWorker extends SwingWorker<Object, Boolean> {
private boolean visibility;
private Sample sample;
public GlassPaneUpdateWorker(Sample sample) {
this.sample = sample;
}
@Override
protected Object doInBackground() throws Exception {
publish(visibility);
return null;
}
@Override
protected void process(List<Boolean> chunks) {
if (chunks != null && !chunks.isEmpty()) {
sample.changeVisibility(chunks.get(0));
}
}
protected void changeVisibility(boolean visibility) {
this.visibility = visibility;
}
}
public class ChangeListener implements PropertyChangeListener{
private Sample sample;
public ChangeListener(Sample sample) {
this.sample = sample;
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
GlassPaneUpdateWorker glassPaneUpdater = new GlassPaneUpdateWorker(sample);
glassPaneUpdater.changeVisibility((Boolean)evt.getNewValue());
glassPaneUpdater.execute();
}
}
在actionPerformed方法中执行所有代码块后,将显示加载玻璃面板。如何更改此设置,以便在“执行第01行”之后更新UI。?
答案 0 :(得分:1)
我能够通过使用SwingWorker完成这项工作,如下所示,没有任何监听器。
ActionListener loadDataActionListner = new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
SwingWorker<?, ?> worker = new SwingWorker<Void, Integer>() {
protected Void doInBackground() {
showLoadingPanel();
Response response = DataManager.getInstance().loadData("myfile.txt");
hideLoadingPanel();
//Do something..
return null;
}
};
worker.execute();
}
}
});
}
};