
Signature
Knute Johnson
email s/nospam/linux/
------->>>>>>http://www.NewsDem
> You are blocking the Event Dispatch Thread. Nothing GUI related will
> work while its thread is asleep.
>
> If you need to do two things at different times to your GUI, run another
> thread that calls the actions on the GUI in the EDT using
> EventQueue.invokeLater().
I created a thread that is passed the components that will be modified
in the GUI and its run() it modified it.
That thread is called by EventQueue.invokeLater() method from the
synchronized method. It still didnt work , it did the same behaviour
exactly. I suspect i am doing it incorrectly. if you would point out
whats done incorrectly i would greatly appreciate it.
Eric Sosman - 17 Mar 2008 21:24 GMT
>> You are blocking the Event Dispatch Thread. Nothing GUI related will
>> work while its thread is asleep.
[quoted text clipped - 10 lines]
> exactly. I suspect i am doing it incorrectly. if you would point out
> whats done incorrectly i would greatly appreciate it.
As Knute said, you're blocking the EDT. invokeLater()
runs your code *on* the EDT, meaning that the EDT cannot
do anything else -- like keeping the GUI alive -- until
your code finishes.
Run your code on its own thread, not on the EDT. When
your code needs to update the GUI, use invokeLater() to
get the updating -- and only the updating -- to run on
the EDT.
See the Java Tutorial for examples of how to handle
long-running tasks in a GUI framework.
http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html

Signature
Eric.Sosman@sun.com
Knute Johnson - 17 Mar 2008 22:11 GMT
>> You are blocking the Event Dispatch Thread. Nothing GUI related will
>> work while its thread is asleep.
[quoted text clipped - 10 lines]
> exactly. I suspect i am doing it incorrectly. if you would point out
> whats done incorrectly i would greatly appreciate it.
Here is a simple example you can try.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class test7 {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
final JLabel l = new JLabel("Button Not Yet Pressed");
f.add(l,c);
final JButton b = new JButton("Start");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
b.setEnabled(false);
l.setText("Running");
Runnable r = new Runnable() {
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
b.setEnabled(true);
l.setText("Done");
}
});
}
};
new Thread(r).start();
}
});
f.add(b,c);
f.pack();
f.setVisible(true);
}
});
}
}

Signature
Knute Johnson
email s/nospam/linux/
ash - 17 Mar 2008 22:51 GMT
Thank you , great help :D
> You are blocking the Event Dispatch Thread. Nothing GUI related will
> work while its thread is asleep.
> If you need to do two things at different times to your GUI, run another
> thread that calls the actions on the GUI in the EDT using
> EventQueue.invokeLater().
I created a thread that is passed the components that will be modified
in the GUI and in its run() it modified these passed components.
That thread is called by EventQueue.invokeLater() method from the
synchronized method. It still didnt work , it did the same behaviour
exactly. I suspect i am doing it incorrectly. if you would point out
whats done incorrectly i would greatly appreciate it.