在Java中创建等待消息当线程完成时

时间:2017-01-23 03:56:36

标签: java swing jframe thread-safety jdialog

在我的程序中,用户单击一个应该访问和解压缩文件的按钮。不幸的是,访问和解压缩文件需要很长时间,我想创建一条消息,说“请等一下”#34;。但是,每当我在按钮侦听器内的jframe中创建消息时,JFrame都是黑色的,直到结束才会填充(可能是线程原因)。我也试过创建一个JDialog但是这会停止执行其余的程序,直到用户说好了等待更多时间。到目前为止,我已经尝试过非模态的JDialogs。我可以创建另一个监听器,但是,我希望在检索文件时关闭窗口。 这是代码:

AttendanceDriver制作并检索文件 评论代码包含我尝试过的一些内容

有谁知道这样做的方法吗?

btnSubmit.addActionListener(new ActionListener() {


            public void actionPerformed(ActionEvent e) {

                dispose();

                    /*JOptionPane.showMessageDialog(DaySelectDialog.this,
                            "Please Wait while we try to generate your report",
                            "Lakeside Attendance Report",
                            JOptionPane.INFORMATION_MESSAGE);*/

                    succeeded = true;
                    //dispose();

                /*try {
                    SwingUtilities.invokeAndWait(new Runnable() {
                        public void run() {
                            JOptionPane op = new JOptionPane("Hi..",JOptionPane.INFORMATION_MESSAGE);
                            op.setMessage("PLEase say something");
                            JDialog dialog = op.createDialog("Break");
                            dialog.setAlwaysOnTop(true);
                            dialog.setModal(false);

                           // dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                            dialog.setVisible(true);
                        }
                    });
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                } catch (InvocationTargetException e1) {
                    e1.printStackTrace();
                }*/



               /* Thread t = new Thread(new Runnable(){
                    public void run(){



                       // JOptionPane.showMessageDialog(null, "Hello");
                    }
                });
                t.start();*/

                /*EventQueue.invokeLater(new Runnable(){
                    @Override
                    public void run() {
                        JOptionPane op = new JOptionPane("Hi..",JOptionPane.INFORMATION_MESSAGE);
                        JDialog dialog = op.createDialog("Break");
                        dialog.setAlwaysOnTop(true);
                        dialog.setModal(false);
                        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                        dialog.setVisible(true);
                    }
                });*/




                 //JFrame way 

                 //   JFrame waitNote = new JFrame();
                  // JLabel waitText = new JLabel("Please Wait While we generate report");
                  // waitNote.add(waitText);
                    //waitNote.setText("hello");
                  // waitNote.setVisible(true);
               /* JOptionPane.showOptionDialog(null, "Hello","Empty?", JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);
                dispose();*/
                   // parent.reconfig;

                      //run Actual Program
                 // TODO catch exception if file is not generated successfully then go back to day select
                      String[] arguments = new String[] {"123"};
                      AttendanceDriver.main(arguments);

                      //view txt file automaticly in Desktop
                      //added by Hans
                      //DaySelectDialog.class.getClassLoader().getResourceAsStream()
                      File attendancetxt = new File(AttendanceDriver.getReportName());
                      try {
                          Desktop.getDesktop().open(attendancetxt);
                      } catch (IOException en) {
                          en.printStackTrace();
                      }






                   /* JOptionPane.showMessageDialog(DaySelectDialog.this,
                            "There has been an error creating the report.",
                            "Lakeside Attendance Report",
                            JOptionPane.ERROR_MESSAGE);
                    // reset username and password
                    succeeded = false;*/


            }
        });

0 个答案:

没有答案