Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Koh Khai Huat
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

code:
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]);
btn[a].addActionListener(this);
}

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++ )
{
panel[h].add(btn[noOfButton]);
noOfButton ++;
}

add(panel[h]);
}
panelBtn = new Panel(new GridLayout(1,0));
startBtn = new JButton("Start");
startBtn.addActionListener(this);

panelBtn.add(startBtn);
add(panelBtn);


run = new Thread(this);
run.start();


}

public void run(){
}

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

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

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

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

i++;
}
}

public void reset(int temp){
try{Thread.sleep(time);}
catch (Exception a)
{System.out.println("Thread error = " + a);}
btn[temp].setIcon(img[temp]);
}

public void start(){}

public void paint(){}

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

regards
kkh
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this
(I hate applets, so I changed it to an app)

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic