ActionListners on the EDT?
Hey Guys
Im building a swing app and i was wondering if this is the principle that i should follow for swing/events:
please tell me if this is the right approach, I should be using. Ive read around and need some conformation from you pros:
1 -> Create a button
private JButton btn_submit =new JButton("Login");
2 -> Create a class that does some work as a thread
publicclass MyWorkerimplements Runnable{}
3 -> Create a class that implements actionlistener and that calls the MyWorker thread to do the job
class MyListnerimplements ActionListener{
...
publicvoid actionPerformed(ActionEvent e)
{
MyWorker();//thread called
}
}
4 -> assign an Actionlistner to my button
btn_submit.addActionListner(new MyListner());
any help would be great
Cheers
[1426 byte] By [
FatClienta] at [2007-11-26 19:10:41]

Hey thanks for quick post
Sorry, that was just a bit of pseudo wrongness from me, i just wanted to give an example of how it would work.
The only reason I included a thread/runnable was because there was so much emphasis on having lite weight event handler code that may just check the state and then spawn new threads that actually do the work, instead of using the EDT for the code.
Is my understanding right?
thanks for being so elaborate with your explanations
this following code will be run on EDT, because it dirrectly effects the SWING GUI:
JButton btn = new Jbutton("Reset");
JTextField txt = new JTextField (29);
btn.addActionListener (
new ActionListener() {
public void actionPerformed(ActionEvent e) {
txt_user.setText("");
}
} );
where as if i was doing some connection stuff, it would be spawned off to another thread, so as to not tie up the EDT thread?
Im a noob and just wanting to learn!
Your generally right there, although these lines:
JButton btn = new Jbutton("Reset");
JTextField txt = new JTextField (29);
will be executed by whatever thread is creating that class (if these are fields)
or running the method in question (if these are local variables).
Sun used to write it was okay to execute Swing code in other threads
until the components were realized or some such thing, but now it
seems that they are recommending you create your GUI totally in the EDT --
it certainly couldn't hurt to do it this way.
Thanks DrLaszloJamf, I see that your high level of duke stars hasn't effected helping noobs (unlike others), by answering painful ignorant questions (I love idiosyncrasies)
> but now it
> seems that they are recommending you create your GUI
> totally in the EDT --
I had a look and according to the tutorials, if you have any code that will directly affect the swing GUI that is not in the original thread where you created your JFrame, Jbuttons, etc.
Use: javax.swing.SwingUtilities.invokeLater();
which will place your code in the ED queue ready for the EDT to do ts stuff.
hope it helps