简体   繁体   English

JDialog停止执行父JFrame

[英]JDialog Stops execution of parent JFrame

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.? 我有一个gif动画图像,显示jDialog内的无限圆加载进度...但是问题是当我加载此jDialog时,父框架代码停止了吗? 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);
  }

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.. 如果我更改JDiaog的ModalityType.MODELESS,它不会停止执行代码,但不会显示进度条。

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. 很可能您遇到了线程问题,即您在Swing事件线程上运行了长时间运行的任务,从而阻止了事件线程更新GUI。 The solution is to use a background thread such as one provided by a SwingWorker. 解决方案是使用后台线程,例如SwingWorker提供的后台线程。

My guess is that the offending line is this one: 我的猜测是,令人讨厌的一句话就是:

JSONArray jArray = asyncService.sendRequest(postPairs);

So again, do this in a background thread. 因此,再次在后台线程中执行此操作。 For more on this, please check out this link: Concurrency in Swing 有关更多信息,请查看此链接: Swing中的并发

For example: 例如:

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();
         }
      });
   }
}

The line pbDialog.setVisible(true) will block until the dialog is dismissed in the case of a modal dialog. 在模式对话框的情况下,行pbDialog.setVisible(true)将阻塞,直到关闭该对话框为止。 If you want to have the dialog open without blocking you have to make it non-modal. 如果要打开对话框而不阻塞,则必须使其为非模态对话框。 Whatever stops your animation from working is probably due to writing synchronous code to repaint the dialog. 使动画无法正常工作的原因可能是由于编写了同步代码以重画对话框。 You need to leverage the EventQueue to redraw the frames periodically. 您需要利用EventQueue定期重绘帧。 You could use a separate thread, or you could use some simpler code that doesn't require a thread like this: 您可以使用单独的线程,也可以使用不需要以下线程的更简单的代码:

public void paintComponent(Graphics g) {
    if( animate ) {
       Graphics2D g2d = (Graphics2D)g;
       BufferedImage frame = frames[currentFrame];
       g2d.drawImage(frame, null, x, y);
       frame.draw( g );
       currentFrame = (currentFrame + 1) % frames.length;
       repaint(); // this call will schedule a repaint at some point later.
    }
}

That requires no threads which is a good thing in terms of resources, and less likely to screw up and violate swing's single thread rule. 不需要线程,这在资源方面是一件好事,并且不太可能搞砸和违反swing的单线程规则。 You can also look at: 您还可以查看:

http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html#drawImage(java.awt.Image , java.awt.geom.AffineTransform, java.awt.image.ImageObserver) http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html#drawImage(java.awt.Image,java.awt.geom.AffineTransform,java.awt.image.ImageObserver

For performing animations across things like animated gifs. 用于在动画gif之类的东西上执行动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM