File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Why can't my button's Image Icon change when i set Icon??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why can Watch "Why can New topic

Why can't my button's Image Icon change when i set Icon???

Koh Khai Huat
Ranch Hand

Joined: Aug 05, 2005
Posts: 100
Hi guys,

I am here to ask for advice and suggestion the problem is that in my program i let the user click on the start button the the program will randomly select 2 button from 12 and randomly set the Image Icon of that 2 button to another Image. And when the 1st button's image icon change i added a thread and put in a delay after some time that button change and the next button will also go trough the same thing after that all will be set back to normal. The problem is when i set the Button's image icon to another image the applet change it until the method have finish running the code in side it. How do u let the image icon refresh once i set it???

I am open to all suggestion and advice thanks in advance.

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
import java.util.Random;

public class color extends Applet implements ActionListener, Runnable
public JButton btn[] = new JButton[13];
public JButton startBtn;
public Panel panel[] = new Panel[4];
public Panel panelBtn;
public static int noOfButton = 0;

public ImageIcon img[] = new ImageIcon[13];
public ImageIcon img1[] = new ImageIcon[3];

public Random rand = new Random();

private Thread run ;

private int time = 1000;

private boolean sleep = false;

public void init(){

img1[0] = new ImageIcon("UBlue.gif");
img1[1] = new ImageIcon("Red.gif");
img1[2] = new ImageIcon("Yellow.gif");
//img1[3] = new ImageIcon("AGreen.gif");

for (int a =0 ; a < 12 ;a++ )
img[a] = new ImageIcon("Bar1.gif");
btn[a] = new JButton(img[a]);

for (int h = 0 ; h < 4 ; h++ )
panel[h] = new Panel();
panel[h].setLayout(new GridLayout(1,0));

for (int v = 0; v < 3 ; v++ )
noOfButton ++;

panelBtn = new Panel(new GridLayout(1,0));
startBtn = new JButton("Start");


run = new Thread(this);


public void run(){

public void randomDisplay(int numberOfFlash){
int i =0;
if(1 < numberOfFlash)
int colorTemp = rand.nextInt(3);
int temp = rand.nextInt(12);

System.out.println("Thread start counting");

catch (Exception a)
{System.out.println("Thread error = " + a);}

System.out.println("thread stop counting");


public void reset(int temp){
catch (Exception a)
{System.out.println("Thread error = " + a);}

public void start(){}

public void paint(){}

public void actionPerformed(ActionEvent e){
if (e.getSource() == startBtn)
System.out.println("Btn pressed called");


if (!WorkHard){
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
try this
(I hate applets, so I changed it to an app)

I agree. Here's the link:
subject: Why can't my button's Image Icon change when i set Icon???
It's not a secret anymore!