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

前端 未结 3 1082
不思量自难忘°
不思量自难忘° 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:35

    A variation of the above answer

    It's an easy and replicable way to do...

    //This code goes inside your button action   
    DialogWait wait = new DialogWait();
    
    SwingWorker mySwingWorker = new SwingWorker() {
    
        @Override
        protected Void doInBackground() throws Exception {
    
            //Here you put your long-running process...
    
            wait.close();
            return null;
        }
    };
    
    mySwingWorker.execute();
    wait.makeWait("Test", evt);
    //end
    
    
    //Create this class on your project
    class DialogWait {
    
    private JDialog dialog;
    
    public void makeWait(String msg, ActionEvent evt) {
    
        Window win = SwingUtilities.getWindowAncestor((AbstractButton) evt.getSource());
        dialog = new JDialog(win, msg, Dialog.ModalityType.APPLICATION_MODAL);
    
        JProgressBar progressBar = new JProgressBar();
        progressBar.setIndeterminate(true);
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(progressBar, BorderLayout.CENTER);
        panel.add(new JLabel("Please wait......."), BorderLayout.PAGE_START);
        dialog.add(panel);
        dialog.pack();
        dialog.setLocationRelativeTo(win);
           dialog.setVisible(true);
       }
    
       public void close() {
           dialog.dispose();
       }
    }
    

提交回复
热议问题