0
votes

After much help from the generous users of this website, I've managed to make my timers function properly in a program I'm designing. Here is a small snippet of the code:

//Class represents what do when a button is pressed
private static class ButtonHandler implements ActionListener { 
    public void actionPerformed (ActionEvent e) {
        them.setText("");
        final JButton button = (JButton)e.getSource();
        Timer timer = new Timer(1000, new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        final String tc = random();
                        them.setText("They chose: " + tc + "!");
                        Timer timer = new Timer(1000, new ActionListener() {
                                    @Override
                                    public void actionPerformed(ActionEvent e) {
                                        if (button == rock) {
                                            whoWins("rock", tc);
                                        } else if (button == paper) {
                                            whoWins("paper", tc);
                                        } else if (button == scissors) {
                                            whoWins("scissors", tc);
                                        }
                                        yourWins.setText("Your wins: " + yw);
                                        theirWins.setText("Their wins: " + tw);
                                    }
                                });
                        timer.setRepeats(false);
                        timer.start();

                    }
                });
        timer.setRepeats(false);     
        timer.start();
    }       


}

What I would like to do is avoid having to define multiple timers just to get them to fire in succession. If it's possible to create a timer class that I call upon when I need to have a delay, that would be best. If I need to replace "new ActionListener()" with "this" or something, please notify me. I am new to java and I'd like to figure this out so I can continue working on various home projects.

1
I would use a ScheduledExecutorService and simply schedule a new task from the previous one. Don't forget to use SwingUtilites.invokeLater to run the code - as otherwise you will violate Swing's threading policy. - Boris the Spider
@HovercraftFullOfEels, after a button press the program will sleep for 1000ms then display a message, and then sleep again and display another message. I would like to condense it so I don't need to rewrite Timer timer = new Timer etc... and just perhaps type the name of a class which handles timers. - patrickstarpants

1 Answers

2
votes

You Could...

Create a class for each action you need to perform, implementing ActionListener. You will need to supply appropriate methods to set what ever states you need to carry between classes.

Then create a instance of each Timer you need and assign it the appropriate action class (which you created previously) and use isRunning and restart when you want to use them, depending on your needs

You Could...

Create a single instance of an ActionListener and a Timer and some kind of state flag.

The ActionListener would check the state of the flag and take appropriate action based upon its value

When you need to, you would set the flags state and start the Timer.

IMHO, this makes the code rather inflexible as all the states are grouped together, rather then been isolated in there own separate blocks.

You could also use this idea, but instead of setting the flag externally, you would simply prepare the state when you start the Timer and allow the ActionListener to change the state go the flag on each tick of the Timer.

This means you could set the Timer to repeat and each tick would perform a different action, stopping the Timer within the ActionListener when your done...