Non-blocking client-Server GUI

Hi,

I am sorry if this is not the right thread to post my question. Admin/Mod is free to move it anywhere else more appropriate.

I have created a Swing GUI which reads tables (using a Server I have created) from the database (MySQL) when I press a specific button.

This button calls for a NetManager object which uses an ObjectWriter object (initialized just after the program starts).

How can I make NetManager object doesn't block my GUI and run in its own thread? At the moment the GUI is heavily blocked by the NetManager object...

I have implemented Runnable in my NetManager object but I use SwingUtils.InvokeLater(<NetManager Object>) on the beginning of my application only to connect to the server and create the ObjectInput/OutputStrems. I now need to call a method that uses those Streams to communicate with the Server.

I am sorry if I can't make myself clear enough. I can copy-paste any code you ask me.

[971 byte] By [xpantaa] at [2007-11-27 1:54:14]
# 1

The best way, I think, is to use a specific thread to

query the database and create graphic objects. Then,

to show Swing object already created, you should run

your code in the EDT (Event Dispatcher Thread). As

you'll see, the most heavy code will be elaborated in

background, the EDT load will be lower and your

interface will become more reactive.

Try to use SwingWorker. It's a small library included

in JRE/JDK 1.6, but there is a backporting for JRE 1.5.

Hope this helps

CanapaGa at 2007-7-12 1:25:37 > top of Java-index,Core,Core APIs...
# 2

Thanks I will check on the SwingWorker. Didn't know it existed! :)

In the meantime what is your opinion on this code I found somewhere in the web?

class ThreadedAction extends AbstractAction implements Runnable{

//InnerClass

ActionEvent event;

Object source;

public void actionPerformed(ActionEvent e){

event = e;

source = e.getSource();

if (worker != null)

return;

worker = new Thread(this);

worker.start();

}

public void run(){

if (source == ThisButton)

{

callAComplicatedMethod();

}

worker = null;

}

which is implemented in this way

ThreadedAction tfe = new ThreadedAction(); //my Threaded Action

ThisButton.addActionListener(tfe);

xpantaa at 2007-7-12 1:25:37 > top of Java-index,Core,Core APIs...
# 3
It's pretty good but SwingWorker is better. ^_^In fact it use a thread-pool to reuse threads alreadycreated and, the most important thing, there is a simple method to interact safely with Swing objects,in the EDT.Regards
CanapaGa at 2007-7-12 1:25:37 > top of Java-index,Core,Core APIs...