JDialog停止执行父JFrame [英] JDialog Stops execution of parent JFrame
问题描述
我有一个gif动画图像,它在jDialog中显示无限循环加载进度...但问题是当我加载这个jDialog时,父帧代码停止。怎么做...我的代码... ...
I have a gif animation image that is showing infinite circle loading progress inside a jDialog...But problem is when i load this jDialog the parent frame codes is halt.? how to do this..here is my code..
ProgressDialouge pbDialog = new ProgressDialouge(this);
pbDialog.setVisible(true);
pbDialog.toFront();
postPairs.add(new BasicNameValuePair("PATH","authenticateUser.idoc"));
postPairs.add(new BasicNameValuePair("user_email",email));
postPairs.add(new BasicNameValuePair("user_password",password));
JSONArray jArray = asyncService.sendRequest(postPairs);
if(jArray != null){
new NewJFrame().setVisible(true);
this.setVisible(false);
}
如果我改变我的JDiaog的ModalityType.MODELESS而不是停止执行代码,但它也没有显示进度条..
if i change my JDiaog's ModalityType.MODELESS than it does't stop the execution of code but it's also not showing the progress bar..
推荐答案
你极有可能遇到线程问题在Swing事件线程上重新运行长时间运行的任务,防止事件线程更新GUI。解决方案是使用后台线程,例如SwingWorker提供的后台线程。
In all likelihood you've got a threading issue where you're running a long-running task on the Swing event thread, preventing the event thread from updating the GUI. The solution is to use a background thread such as one provided by a SwingWorker.
我的猜测是违规行是这一行:
My guess is that the offending line is this one:
JSONArray jArray = asyncService.sendRequest(postPairs);
再次,在后台线程中执行此操作。有关这方面的更多信息,请查看以下链接: Swing中的并发
So again, do this in a background thread. For more on this, please check out this link: Concurrency in Swing
例如:
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class ShowSwingWorker {
private JPanel mainPanel = new JPanel();
private JButton myBtn = null;
private ProgressDialouge pbDialog = null;
public ShowSwingWorker() {
myBtn = new JButton(new AbstractAction("Push Me") {
@Override
public void actionPerformed(ActionEvent evt) {
JButton source = (JButton) evt.getSource();
source.setEnabled(false); // disable button
Window win = SwingUtilities.getWindowAncestor(source);
new MySwingWorker().execute(); // start background thread
if (pbDialog == null) {
pbDialog = new ProgressDialouge(win);
pbDialog.pack();
pbDialog.setLocationRelativeTo(win);
Point loc = pbDialog.getLocation();
pbDialog.setLocation(loc.x - 100, loc.y - 100);
}
pbDialog.setVisible(true);
// pbDialog.toFront();
}
});
mainPanel.add(myBtn);
}
public JComponent getMainPanel() {
return mainPanel;
}
private class MySwingWorker extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
Thread.sleep(4000); // emulate a long-running task
// postPairs.add(new BasicNameValuePair("PATH",
// "authenticateUser.idoc"));
// postPairs.add(new BasicNameValuePair("user_email", email));
// postPairs.add(new BasicNameValuePair("user_password", password));
// JSONArray jArray = asyncService.sendRequest(postPairs);
// if (jArray != null) {
// new NewJFrame().setVisible(true);
//
// this.setVisible(false);
// }
return null;
}
@Override
protected void done() {
// Here you change your display.
// you were swapping JFrames, but I recommend that you instead change views.
myBtn.setEnabled(true);
pbDialog.setVisible(false);
}
}
private class ProgressDialouge extends JDialog {
public ProgressDialouge(Window win) {
super(win, "MyDialog", ModalityType.APPLICATION_MODAL);
JProgressBar pBar = new JProgressBar();
pBar.setIndeterminate(true);
add(pBar);
}
}
private static void createAndShowGUI() {
ShowSwingWorker paintEg = new ShowSwingWorker();
JFrame frame = new JFrame("ShowSwingWorker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(paintEg.getMainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
这篇关于JDialog停止执行父JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!