Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.*;
- import javax.swing.*;
- public class TestActionDisable {
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- final JButton btn = new JButton(new MyAction());
- JPanel panel = new JPanel() {{add(btn);}};
- JOptionPane.showMessageDialog(null, panel);
- }
- });
- }
- }
- class MyAction extends AbstractAction {
- private static final int TIMER_DELAY = 10 * 1000;
- private Timer timer = new Timer(TIMER_DELAY, new TimerListener());
- public MyAction() {
- super("Push Me");
- putValue(MNEMONIC_KEY, KeyEvent.VK_P);
- timer.setRepeats(false);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- System.out.println("Button Pushed!");
- setEnabled(false);
- if (!timer.isRunning()) {
- timer.start();
- }
- }
- private class TimerListener implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- MyAction.this.setEnabled(true);
- }
- }
- }
Add Comment
Please, Sign In to add comment