ok so I have a JButton that has an image on it,
and then after 4 seconds i want to put a differant image on it

but the problem is the first image wont load

I'm using

button1.setIcon (icon);

try
        {
            Thread.sleep (4000); //sleeping for 4 seconds
        }
        catch (InterruptedException e)
        {
        }

button1.setIcon (icon1);

I really dont get why this is hapening, the images are fine, becouse If i switch them around the second one always only displays

ok so I have a JButton that has an image on it,
and then after 4 seconds i want to put a differant image on it

but the problem is the first image wont load

I'm using

button1.setIcon (icon);

try
        {
            Thread.sleep (4000); //sleeping for 4 seconds
        }
        catch (InterruptedException e)
        {
        }

button2.setIcon (icon1);

I really dont get why this is hapening, the images are fine, becouse If i switch them around the second one always only displays

you say you want to put a different icon on the same button, but you are using two seperate buttons: button1 and button2?

you say you want to put a different icon on the same button, but you are using two seperate buttons: button1 and button2?

sorry thats wrong in the example, ill fix that, in my program it it correct
Still cant figure it out

sorry thats wrong in the example, ill fix that, in my program it it correct
Still cant figure it out

well maybe check if the icon is null? because if so then the image is not loaded and therfore maybe the path is wrong or something. It would help if i could see some code that pertains to the button and its icon creations

it probably has to do with how paintComponent gets called "randomly" , im no expert on the subject but my guess would be that the button doesn't get painted before you freeze the thread, and then by the time between the thead awakes from the sleep and it decide its got time to paint the button you already changed the icon.

it probably has to do with how paintComponent gets called "randomly" , im no expert on the subject but my guess would be that the button doesn't get painted before you freeze the thread, and then by the time between the thead awakes from the sleep and it decide its got time to paint the button you already changed the icon.

yes that may also be a problem. Try using:

button1.repaint();

to force it to repaint the button now?

well maybe check if the icon is null? because if so then the image is not loaded and therfore maybe the path is wrong or something. It would help if i could see some code that pertains to the button and its icon creations

as i said in my initial quistion, if i switch the 2 images around, its always the second one that only displays which mean both images are good...

and i tried repaint(); already, doesant help

as i said in my initial quistion, if i switch the 2 images around, its always the second one that only displays which mean both images are good...

Read up on what Philippe.Lahaie said and my comment on that
[EDIT] Hmm then im not sure. maybe try calling repaint() on the main component the button is added too, also try revalidate()

repaint() wouldnt work in this case because it jsut tells the system to schedule repainting when it feels ready , and you're sending the Thread to sleep so he wont repaint it .

but then again, im not 100% sure this is the problem yet. James would be much better placed to advise you on this.

repaint() wouldnt work in this case because it jsut tells the system to schedule repainting when it feels ready , and you're sending the Thread to sleep so he wont repaint it .

but then again, im not 100% sure this is the problem yet. James would be much better placed to advise you on this.

So if the thread starts to sleep before the Icon gets set, what should i do?, anything else i could use?, maybe make the thread sleep after the icon has been set? how do i check if icon is set?

So if the thread starts to sleep before the Icon gets set, what should i do?, anything else i could use?, maybe make the thread sleep after the icon has been set? how do i check if icon is set?

as i said i read use repaint() on the component or one within its hierarchy and aslo revalidate and paintImmediately():http://stackoverflow.com/questions/7855946/java-gui-trouble-repaint-page, http://stackoverflow.com/questions/4120528/repaint-in-a-loop and especially here:http://stackoverflow.com/questions/8125318/repaint-method-wont-repaint-my-screen -" don't use Thread.sleep(int) for Swing GUI use javax.swing.Timer instead, because during Thread.sleep(int) you GUI simply freeze, nothing else".

as i said i read use repaint() on the component or one within its hierarchy and aslo revalidate and paintImmediately():http://stackoverflow.com/questions/7855946/java-gui-trouble-repaint-page, http://stackoverflow.com/questions/4120528/repaint-in-a-loop and especially here:http://stackoverflow.com/questions/8125318/repaint-method-wont-repaint-my-screen -" don't use Thread.sleep(int) for Swing GUI use javax.swing.Timer instead, because during Thread.sleep(int) you GUI simply freeze, nothing else".

ok, awsome, so now i understand why it wouldent work and im trying to implement a swing timer

this is what I have

Timer timer;
        timer = new Timer (4000, this);
        timer.setInitialDelay (0);
        timer.start ();

I got this from google and i just changed the amount of time to 4000 which is 4 seconds, im wondering do I just Put this after I assign my first Icon, so it pauses, lets it repaint and after 4 seconds changes the icon, becouse right now nothing pauses, just go strait to second label

but i also get this error

java.lang.NullPointerException
at Program.actionPerformed(Program.java:281)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

i was gonna refer you to doc on the util.Timer class but i found this isntead which is even better for what you want to do :

swing.Timer

ok, awsome, so now i understand why it wouldent work and im trying to implement a swing timer

this is what I have

Timer timer;
        timer = new Timer (4000, this);
        timer.setInitialDelay (0);
        timer.start ();

I got this from google and i just changed the amount of time to 4000 which is 4 seconds, im wondering do I just Put this after I assign my first Icon, so it pauses, lets it repaint and after 4 seconds changes the icon, becouse right now nothing pauses, just go strait to second label

but i also get this error

java.lang.NullPointerException
at Program.actionPerformed(Program.java:281)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

read here:http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html and here:http://www.leepoint.net/notes-java/other/10time/20timer.html and here:http://www.dreamincode.net/forums/topic/69429-jframe-updating-trying-to-make-a-timer/

The null pointer is telling you a variable has not been initilized or a method is returning null on line 281 in Program class

read here:http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html and here:http://www.leepoint.net/notes-java/other/10time/20timer.html and here:http://www.dreamincode.net/forums/topic/69429-jframe-updating-trying-to-make-a-timer/

The null pointer is telling you a variable has not been initilized or a method is returning null on line 281 in Program class

I dont know why i dont understand this

So i want I want the timer to execute once so this is my code

Timer timer;
        timer = new Timer (4000,this);
        timer.setRepeats(false);        
        timer.start ();

So the timer should time 4 seconds for this method, i think
and its not suposed to repeat
and i start it,
so i start my program and it doesant pause at all
what am i doing wrong, same thing, the second Icon imediaetly gets set after the first now, should wait 4 seconds

For Timer you need at least the following:

1) Add your import APIs

import java.util.*; // need this for the timer "java.util.Timer;" works too
import java.awt.event.*; // need this for your actionListener "java.awt.event.ActionListener;" works too)

2) Create your Timer (Milliseconds, action)

Timer timer = new Timer(500, new ActionListener() { 
public void actionToDo(ActionEvent e) {
    //Your action code here 
}
}); // closes timer's loaded constructor

3) Start your timer

timer.start(); // start now, wait 500 milliseconds and do my action code!
timer.stop(); // stops your timer

Thanks alot, but now im getting this error,

The abstract method "void actionPerformed(java.awt.event.ActionEvent $1);", inherited from type "java.awt.event.ActionListener", is not implemented in the non-abstract class "Program$1".

what does this mean?

Thanks alot, but now im getting this error,

The abstract method "void actionPerformed(java.awt.event.ActionEvent $1);", inherited from type "java.awt.event.ActionListener", is not implemented in the non-abstract class "Program$1".

what does this mean?

you know what i've been trying to use that code its also a bit bad and not working any more try here i've tested this:

//declare the first image her jButton1.setIcon(icon);
 int delay = 5000;   // delay for 5 sec.
Timer timer = new Timer();

timer.schedule(new TimerTask() {
        public void run() {//method to do after 5 seconds here cant change its name though we are using that method in the TimerTask
            System.out.println("Hello");
           //declare the first image her jButton1.setIcon(icon2);
        }
    }, delay);

This should offer more info on the methods:http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html,http://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html

you know what i've been trying to use that code its also a bit bad and not working any more try here i've tested this:

int delay = 5000;   // delay for 5 sec.
Timer timer = new Timer();

timer.schedule(new TimerTask() {
        public void run() {//method to do after 5 seconds here cant change its name though we are using that method in the TimerTask
            System.out.println("Hello");
        }
    }, delay);

This should offer more info on the methods:http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html,http://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html

doesant this timer do the task over and over again, and btw, this gives me the following errors, i forgot to tell you im using java 1.4.2

i get these errors

No applicable overload was found for a constructor of type "javax.swing.Timer". Perhaps you wanted the overloaded version "Timer(int $1, java.awt.event.ActionListener $2);" instead?


and

Type TimerTask was not found.

I really dont know whats going on, and I think I'm starting to be a pain in the arse

doesant this timer do the task over and over again, and btw, this gives me the following errors, i forgot to tell you im using java 1.4.2

i get these errors

No applicable overload was found for a constructor of type "javax.swing.Timer". Perhaps you wanted the overloaded version "Timer(int $1, java.awt.event.ActionListener $2);" instead?


and

Type TimerTask was not found.

I really dont know whats going on, and I think I'm starting to be a pain in the arse

no but there is a method that will do the task repeatedly and the problem is because you are using an old version of java, then my other method would work, i think but i would strongly suggest updating your java, not only will you have new methods available to you, but also you wont get viruses as easily java has a lot of holes

Thanks alot, but now im getting this error,

The abstract method "void actionPerformed(java.awt.event.ActionEvent $1);", inherited from type "java.awt.event.ActionListener", is not implemented in the non-abstract class "Program$1".

what does this mean?

as for this message:"void actionPerformed(java.awt.event.ActionEvent $1);", inherited from type "java.awt.event.ActionListener", is not implemented in the non-abstract class "Program$1".

By your main class in which the action listener resides put:

public class Program implements ActionListener {
}

as for this message:"void actionPerformed(java.awt.event.ActionEvent $1);", inherited from type "java.awt.event.ActionListener", is not implemented in the non-abstract class "Program$1".

By your main class in which the action listener resides put:

public class Program implements ActionListener {
}

i already have that, but now i have 2 action listeners, is that what you meant, or am i only suposed to have 1, but i already have one

i already have that, but now i have 2 action listeners, is that what you meant, or am i only suposed to have 1, but i already have one

no i dont think you can have two action listners in the same class? i guess you will have to get the event that occurs name and check if its the timer? im not sure

well, acually the action listener is making the method go with the timer and the new actionlistener, so instead of making it go to that method, I should just put the timer in the action listener?

well, acually the action listener is making the method go with the timer and the new actionlistener, so instead of making it go to that method, I should just put the timer in the action listener?

seems okay to me check here especially the second solution as the first wont work unless you update java:http://stackoverflow.com/questions/6912615/changing-jbutton-text-periodically this may also be of interest for multiple action listeners but definitely not suggested:http://stackoverflow.com/questions/8457802/multiple-java-actionlisteners and http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

seems okay to me check here especially the second solution as the first wont work unless you update java:http://stackoverflow.com/questions/6912615/changing-jbutton-text-periodically this may also be of interest for multiple action listeners but definitely not suggested:http://stackoverflow.com/questions/8457802/multiple-java-actionlisteners and http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

kk, I'll look into it tonight, be back tomorrow

kk, I'll look into it tonight, be back tomorrow

ok, since this is all about timers i started a new thread on timers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.