fubarable

TestActionDisable.java

Nov 1st, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. import java.awt.event.*;
  2. import javax.swing.*;
  3.  
  4. public class TestActionDisable {
  5.    public static void main(String[] args) {
  6.       SwingUtilities.invokeLater(new Runnable() {
  7.          public void run() {
  8.             final JButton btn = new JButton(new MyAction());
  9.             JPanel panel = new JPanel() {{add(btn);}};
  10.             JOptionPane.showMessageDialog(null, panel);
  11.          }
  12.       });      
  13.    }
  14. }
  15.  
  16. class MyAction extends AbstractAction {
  17.    private static final int TIMER_DELAY = 10 * 1000;
  18.    private Timer timer = new Timer(TIMER_DELAY, new TimerListener());
  19.  
  20.    public MyAction() {
  21.       super("Push Me");
  22.       putValue(MNEMONIC_KEY, KeyEvent.VK_P);
  23.       timer.setRepeats(false);
  24.    }
  25.    
  26.    @Override
  27.    public void actionPerformed(ActionEvent e) {
  28.       System.out.println("Button Pushed!");
  29.       setEnabled(false);
  30.      
  31.       if (!timer.isRunning()) {
  32.          timer.start();
  33.       }
  34.    }
  35.    
  36.    private class TimerListener implements ActionListener {
  37.       @Override
  38.       public void actionPerformed(ActionEvent e) {
  39.          MyAction.this.setEnabled(true);
  40.       }
  41.    }
  42. }
Add Comment
Please, Sign In to add comment