1

After advice from another thread, I've been playing with the timer class without much joy. Heres my code:

 public void buttonImageReveal(ActionEvent e){

        Timer gameTimer = new Timer(100, new ActionListener() {

            public void actionPerformed(ActionEvent e) {

              repaint();
            }
          });

        String temp = e.getActionCommand();

        switch(temp){
                        case "1":
                            System.out.println("case1");                            
                            ((JButton)e.getSource()).setIcon(one);
                            gameTimer.start();
                            ((JButton)e.getSource()).setIcon(null);
                            break;

All I want is a 1 second gap between the image one been shown as the icon then that been removed. Only to happen once when the button is click. At the moment I just get a blank button when pressed?

TIA

Edit:

     public void actionPerformed(ActionEvent e) {
          System.out.println(e);
          lastImage();

        }

      });

public void buttonImageReveal(ActionEvent e){ 

        String temp = e.getActionCommand();

        switch(temp){
                        case "1":                       
                            ((JButton)e.getSource()).setIcon(one); 
                            lastBtn = ((JButton)e.getSource()); 
                            gameTimer.start();

                            break;

It now doing what it should, but the timer keeps going and going, what do you do to once your finished and want it to stop?!

James MV
  • 8,569
  • 17
  • 65
  • 96

5 Answers5

4

You set the icon, start a timer and then immediately remove the icon. The start() method from the timer is almost immediately going to return and the timer will perform its task asynchronously. You'll need to do the icon removing in the actionPerformed method.

G_H
  • 11,739
  • 3
  • 38
  • 82
  • 1
    If I do that it seems to put it into a loop, I can't figure out why? – James MV Oct 25 '11 at 15:29
  • @EricBanderhide Can you edit your post with the updated code? Paste it behind the current code to keep the original problem clear. – G_H Oct 25 '11 at 15:47
  • Read the Timer API. There is a method that allows you to control whether the Timer fires continuously or only once. – camickr Oct 25 '11 at 15:50
  • Ah yes, set it to no repeat and its all working. Thanks G_H and camickr I've been working on this for about 2 hours with no joy. Thanks! – James MV Oct 25 '11 at 15:53
1

Try to set icon to null when the timer fires. I have updated in original thread as well. So the code should be:

public void buttonImageReveal(final ActionEvent e){

        Timer gameTimer = new Timer(100, new ActionListener() {

            public void actionPerformed(ActionEvent e) {

              ((JButton)e.getSource()).setIcon(null);
            }
          });

        String temp = e.getActionCommand();

        switch(temp){
                        case "1":
                            System.out.println("case1");                            
                            ((JButton)e.getSource()).setIcon(one);
                            gameTimer.start();                                
                            break;
Ashwinee K Jha
  • 9,187
  • 2
  • 25
  • 19
1

you little bit compicated simple things,

      timer1 = new Timer(1000, new AbstractAction() {

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            random = new Random();
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    button1.setSomething(something);
                    button1.validate(); //not required for all methods
                    button1.repaint();  //not required for all methods
                }
            });
        }
    });
    timer1.setDelay(500);
    timer1.setRepeats(true);
    timer1.start();

examples here and here

don't forget to stop your Timer#stop(), or if you want to run this code only once then setRepeats(false)

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

Instead of calling repaint() 100ms later (should be 1000ms = 1s, I think) change the components 1s later. Do this by replacing repaint() with the changing method.

Sibbo
  • 3,796
  • 2
  • 23
  • 41
-1

Rather than creating a new timer could you use

Thread.sleep(1000);

To replace

gameTimer.start();
GrahamA
  • 5,875
  • 29
  • 39
  • 1
    -1 invoking Thread.sleep() on the Event Dispatch Thread will prevent the GUI from repainting itself so you still won't won't see the initial icon. – camickr Oct 25 '11 at 15:52