• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help!! Runnable Interface Problem!!!  RSS feed

 
kei hosima
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I'm working on this for long time, and I have out of idea whatelse I should do to fix my code.
Hope someone could give me some adivce.
What I'm trying to do is this:
Define non-public class named MyLabel that encapsulates a customized Label component. It should extend the Label class and implement the Runnable interface in order to be run as a thread. A MyLabel object must display text that changes its color and font at a specified interval. If an attempt is made to set a null text or an interval that is less than zero, the corresponding "set" method must throw an Exception object having an appropriate error message.
The following is the part of my code.
[code]
private MyLabel myLabel;public void init() { myLabel = new MyLabel();// getting error message. don't know what to put after MyLabel. myLabel.setFont(new Font ("Serif", Font.PLAIN, 12));
myLabel.setForeground(Color.red); myLabel.setBackground(Color.black); c.gridx=0;
c.gridy=1;
c.gridheight=1; c.gridwidth=GridBagConstraints.REMAINDER;
add(myLabel, c);
public void actionPerformed(ActionEvent e) { // if statemnets......
else
{
try{
m.put(new String(nameField.getText()),
new Employee(nameField.getText(), Double.parseDouble(payRateField.getText().trim())));
msg.setText("The employee has been added to the collection.");
checkLabel(myLabel);
}catch (Exception err){
System.out.println(err.getMessage());
}}}

public void checkLabel(MyLabel myLabel)throws Exception{
msg.setText("LabelTEST");}
class MyLabel extends Label implements Runnable{
int interval;
String myLabel;
MyLabel (String myLabel, int blinkSpeed){
Thread t = new Thread (this);
t.start();}
public void run() {
for(int i=0; i<100; i++) {
try{
Thread.sleep((int)(Math.random() * 1));
} catch( InterruptedException e ) { System.out.println("Interrupted Exception caught");
}
Color oldForeground = getForeground(); setForeground(getBackground());
setBackground(oldForeground);
}}
public void setMsg (String newMsgLabel){ myLabel = newMsgLabel;
throw new NullPointerException();}
public void setInterval (int newInterval){ interval = newInterval;
throw new NumberFormatException();}}

myLabel doesn't do anything. It does not blink at all.
What am I doing wrong?
Sorry for this long question... and thank you for reading everything.
Please give me some adivce.
I would very very appreciate your help!
Thanks in advance.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question: how long is your thread sleeping for?
Hint: check the Math.random and Thread.sleep APIs
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!