piratolo wrote:
I should set the text in a JLABEL after clicking on a JButton; where should i put the run() method with the JLabel.setText("bla bla") instruction? in the actionPerformed() method of my JButton?

You don't need a run() method. You can set the text in actionPerformed, since you're already on the EDT. It's when you're outside the EDT that you need to use invokeLater().

Good! This means that I didnt understand anything about edt. :(
Then what is edt useful for? When have I to use it?
For instance, when i use the setText() method calling it from another method not in a actionPerformed method?
thank for helping me

It's useful for handling the events and other things related to the Swing GUI.

When have I to use it?

You need to run (most)code that affects the GUI on the EDT, otherwise you'll experience glitches in the GUI.

For instance, when i use the setText() method calling it from another method not in a actionPerformed method?

Yes. If you're on another thread and wish to update the GUI (let's say you have a thread that polls the database every minute and displays the contents in a JTable), you need to use the SwingUtilities.invokeLater method to make sure that the updating code is run on the EDT, where it will work correctly.

piratolo wrote:
For instance, when i use the setText() method calling it from another method not in a actionPerformed method?

Yes. If you're on another thread and wish to update the GUI (let's say you have a thread that polls the database every minute and displays the contents in a JTable), you need to use the SwingUtilities.invokeLater method to make sure that the updating code is run on the EDT, where it will work correctly.

Actually, the question was calling it from another method, not another thread . The answer is, of course, if the other method is also on the EDT, everything is fine. if it's not, then Kayaman's answer about using SwingUtilities.invokeLater applies.

Thank everybody.
I wrote a little code, to be sure i get the issue. It's an exsample

public static void main(String[] args){
// I shouldn't do this because i'm not in EDT
JLabel.setText("ciao");
// i should do this because i'm not in EDT, so i call the run method below
try{
SwingUtilities.invokeAndWait(runnable);
}
catch(Exception er){}
}
Runnable runnable = new Runnable(){
public void run(){
JLabel.setText("write in the JLabel");
}
};
public class Listener implements ActionListener{
public void actionPerformed(ActionEvent e){
// I can do this because i'm in EDT
JLabel.setText("write in the JLabel");
}
}

The point is that in <tt>main</tt> you usually just start the GUI (and the EDT) and do (almost) nothing else.

When Swing calls one of your listeners the GUI is blocked until you return from the listeners method. As long as you work is short every thing is fine.

But you meight have work to do that requires some time. In this case you should delegeate your work to a new thread (running outside the EDT) so that your listener method can return early. From this other worker thread you need to use <tt>SwingUtilities</tt> to update UI components.

Technically yes. But the example isn't really accurate; changes done to the model are generally inside some piece of event code (run on the EDT), such as a button press. You wouldn't often need to run an invokeLater to execute such code.

Now i'm trying to understand how to handle the situation TPD Opitz-Consulting talked about:
>
TPD Opitz-Consulting com wrote:
But you meight have work to do that requires some time. In this case you should delegeate your work to a new thread (running outside the EDT) so that your listener method can return early. From this other worker thread you need to use SwingUtilities to update UI components.
>

Should i do something like this?

// this is my thread in which i create 100000 JLabel, so it requires some time
Thread t1 = new Thread(){
public void run(){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
for(int i = 0; i <100000;i++){
JPanel.add(new JLabel("i"));
}
}
});
}
};

// after clicking on a button a call the start() method of my thread
public class Listener implements ActionListener{
public void actionPerformed(ActionEvent e){
t1.start();
}
}