Article View: borland.public.jbuilder.java.language
Article #18047Threads and Form Updates/Repaints - simple questions
From: Bob Byron
Date: Tue, 25 Aug 1998 00:00
Date: Tue, 25 Aug 1998 00:00
97 lines
3184 bytes
3184 bytes
I am new to threads and have some simple questions.
I have a very simple test program.
I have a Frame that contains a label and a button.
I also have a thread.
The button on the frame starts the thread.
The thread loops from 1 to 10 simply updating the label on the frame to the
count value then sleeps for 1 second.
Now, when I press the button, I expect to see 1, 2, 3, ... until the counter
gets to 10.
Unfortunately, I only see the final result - 10.
Why?
What techniques should I use to correct this?
Thanks,
Bob Byron
RAD Systems, Inc.
Below is the thread frame, followed by the thread class.
vvvvvvvvvvvvv ThreadFrame.java vvvvvvvvvvvvvv
package TestThreads;
import java.awt.*;
import java.awt.event.*;
import borland.jbcl.control.*;
import borland.jbcl.layout.*;
public class ThreadFrame extends DecoratedFrame {
//Construct the frame
BorderLayout borderLayout1 = new BorderLayout();
XYLayout xYLayout2 = new XYLayout();
BevelPanel bevelPanel1 = new BevelPanel();
TextControl ThreadLabel1 = new TextControl();
ButtonControl buttonControl1 = new ButtonControl();
Thread1 MyThread1 = new Thread1(this);
public ThreadFrame() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setLayout(borderLayout1);
this.setSize(new Dimension(119, 153));
this.setTitle("Frame Title");
ThreadLabel1.setText("ThreadLabel1");
buttonControl1.setLabel("buttonControl1");
buttonControl1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonControl1_actionPerformed(e);
}
});
bevelPanel1.setLayout(xYLayout2);
this.add(bevelPanel1, BorderLayout.CENTER);
bevelPanel1.add(ThreadLabel1, new XYConstraints(12, 14, 82, 35));
bevelPanel1.add(buttonControl1, new XYConstraints(5, 68, -1, -1));
}
void buttonControl1_actionPerformed(ActionEvent e) {
MyThread1.run();
}
}
^^^^^^^^^^^^^ ThreadFrame.java ^^^^^^^^^^^^^^
vvvvvvvvvvvvv Thread1.java vvvvvvvvvvvvvv
package TestThreads;
public class Thread1 extends Thread {
int count;
ThreadFrame threadFrame;
public Thread1(ThreadFrame frame) {
count = 0;
threadFrame = frame;
}
public void run() {
while (count++ < 10) {
threadFrame.ThreadLabel1.setText("" + count);
try {
sleep(1000);
threadFrame.ThreadLabel1.repaint();
} catch (Exception e) {};
}
}
}
^^^^^^^^^^^^^ Thread1.java ^^^^^^^^^^^^^^
Technical Details
Message-ID:
<6rvmum$j3i2@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,b5a30fbf6d926a13,start X-Google-Attributes: gidf74b3,public From: "Bob Byron" <bbyron@radit.com> Subject: Threads and Form Updates/Repaints - simple questions Date: 1998/08/25 Message-ID: <6rvmum$j3i2@forums.borland.com> X-Deja-AN: 384747624 Organization: Another Netscape Collabra Server User X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 Newsgroups: borland.public.jbuilder.java.language