KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I need to stop user making multiple clicks on a JButton while the first click still execute. I was able to came with a solution for this issue but I do not completelly understand why it's working. Bellow I posted the code (trimmed to a minimum) that works and the one that does not work. In first example (good) if you run it and click the button multiple times only one action is considered as for the second example (bad) if you click the mouse multiple times you get action executed at least twice. The second (bad) example simply does not use invokeLater() method. Where the difference in behaviour cames from? import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; public class TestButtonTask { public static void main(String[] args) { final JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); final JButton task = new JButton("Test"); task.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { long t = System.currentTimeMillis(); System.out.println("Action received"); task.setText("Working..."); task.setEnabled(false); SwingUtilities.invokeLater(new Thread() { @Override public void run() { try { sleep(2 * 1000); } catch (InterruptedException ex) { Logger.getLogger(TestButtonTask.class.getName()).log(Level.SEVERE, null, ex); } SwingUtilities.invokeLater(new Runnable() { public void run() {
Tags (comma-separated)
Save Edits
Cancel