How to Delay MessageDialogBox in Java?

前端 未结 2 1110
孤独总比滥情好
孤独总比滥情好 2021-01-20 11:47

So in this chunk of code:

//Actions performed when an event occurs.
    public void actionPerformed(ActionEvent event) 
    {
        String command = event.         


        
相关标签:
2条回答
  • 2021-01-20 12:10

    You should use a Swing Timer with a delay, instead of using your own Thread and Runnable for this.

    You can use Swing timers in two ways:

    • To perform a task once, after a delay. For example, the tool tip manager uses Swing timers to determine when to show a tool tip and when to hide it.
    • To perform a task repeatedly. For example, you might perform animation or update a component that displays progress toward a goal.

    An example from the documentation:

      int delay = 1000; //milliseconds
      ActionListener taskPerformer = new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              //...Perform a task...
          }
      };
      Timer myTimer = new Timer(delay, taskPerformer);
      myTimer.setRepeats(false);
      myTimer.start();
    
    0 讨论(0)
  • 2021-01-20 12:11

    You can use the SwingWorker.

    Have a look here, java tutorial.

    SwingWorker worker = new SwingWorker<Void, Void>() {
        @Override
        public Void doInBackground() {
            FileConverter fc = new FileConverter();
            return null;
        }
    
        @Override
        public void done() {
            JOptionPane.showMessageDialog(this, "Step 1 Complete!", "Validation", JOptionPane.INFORMATION_MESSAGE);
        }
    };
    
    0 讨论(0)
提交回复
热议问题