I have another question for this forum. I am writing a small application with Java in which I have multiple Threads. I have two Threads that are in two different classes. I have one Thread (thread1) dedicated to defining all of the JFrame components. (JPanels, JButtons, ActionListeners, etc.) In another Thread, (thread2) I am checking for a variable (v) to equal a certain value. (98) When this variables does change to equal 98, that same thread is supposed to update a JLabel. (Let's call this label label for now.) It does not. Is there something I am doing wrong. I have tried writing a method for the class - that declares all of the JFrame components - that changes the text of label, but when I call this method from thread2 it does not update the label. I have tried calling a super class containing thread1, but that did not seem to make a difference. Also, I have been calling the repaint() method, but that does not seem to help.
Please let me know what I can do to update these JLabels from the two different Threads.
Thanks in advance, ~Rane
Code:
Class1:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel label = new JLabel("V does not equal 98 Yet...");
Thread thread1 = new Thread(){
public void run(){
panel.add(label);
frame.add(panel);
System.out.println("Components added!");
}
});
public void setLabelText(String text){
label.setText(text);
}
Class1=2:
v = 0;
Thread thread2 = new Thread(){
public void run(){
while(v < 98){
System.out.println("V equals -" + v + "-.");
v ++;
}
Class1 otherClass = new Class1();
otherClass.label.setText("V is now equal to: " + v);
otherClass.repaint();
otherClass.setLabelText("V is now equal to: " + v);
otherClass.repaint();
}
});