To set delay on a button click in java?

2019-05-08 16:17发布

I have a save button in a JFrame ;on clicking save the 'save' text sets to 'saving....'; I need to set that text as 'saved' after a delay of 10 seconds.How is it possible in java? Please help...

try {
    Thread.sleep(4000);
} catch (InterruptedException e) {

    e.printStackTrace();
}

This is what i did...but this wont shows as 'saving' during that delayed time.

3条回答
欢心
2楼-- · 2019-05-08 16:29

This question & first 3 answers are heading down the wrong track.

  • Use a JProgressBar to show something is happening. Set it to indeterminate if the length of the task is not known, but presumably you know how much needs to be saved and how much is currently saved.
  • Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Use a SwingWorker for long running tasks. See Concurrency in Swing for more details.
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-05-08 16:39

The best is to use a timer and its method execute with a delay : http://developer.apple.com/library/mac/documentation/java/reference/javase6_api/api/java/util/Timer.html#schedule(java.util.TimerTask, long). Use a timertask to wrap your runnable and that's it.

查看更多
SAY GOODBYE
4楼-- · 2019-05-08 16:50

If you want to provide the user with visual feedback that something is going on (and maybe give some hint about the progress) then go for JProgressBar and SwingWorker (more details).

If on the other hand you want to have a situation, when user clicks the button and the task is supposed to run in the background (while the user does other things), then I would use the following approach:

button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {                                          
        button.setEnabled(false); // change text if you want
        new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() throws Exception {
                // Do the calculations
                // Wait if you want
                Thread.sleep(1000);
                // Dont touch the UI
                return null;
            }
            @Override
            protected void done() {
                try {
                    get();
                } catch (Exception ignore) {
                } finally {
                    button.setEnabled(true); // restore the text if needed
                }
            }                    
        }.execute();
    }
});

Finally, the initial solution that was using the Swing specific timer:

final JButton button = new JButton("Save");
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {                                          
        // Take somehow care of multiple clicks
        button.setText("Saving...");
        final Timer t = new Timer(10000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                button.setText("Saved");
            }
        });
        t.setRepeats(false);
        t.start();
    }
});
查看更多
登录 后发表回答