Make a swing thread that show a “Please Wait” JDialog

前端 未结 3 1084
不思量自难忘°
不思量自难忘° 2020-12-09 19:10

The problem is this:
I\'ve a swing application running, at a certain point a dialog requires to insert username and password and to press \"ok\".
I would like tha

3条回答
  •  春和景丽
    2020-12-09 19:44

    public void okButtonActionPerformed(ActionEvent e) {
    
        final JDialog loading = new JDialog(parentComponent);
        JPanel p1 = new JPanel(new BorderLayout());
        p1.add(new JLabel("Please wait..."), BorderLayout.CENTER);
        loading.setUndecorated(true);
        loading.getContentPane().add(p1);
        loading.pack();
        loading.setLocationRelativeTo(parentComponent);
        loading.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        loading.setModal(true);
    
        SwingWorker worker = new SwingWorker() {
            @Override
            protected String doInBackground() throws InterruptedException 
                /** Execute some operation */   
            }
            @Override
            protected void done() {
                loading.dispose();
            }
        };
        worker.execute();
        loading.setVisible(true);
        try {
            worker.get();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
    

提交回复
热议问题