Hi Sudhakar,
Sudhakar S <[EMAIL PROTECTED]> wrote on 08/24/2006 06:18:38 AM:
> > So I am a little confused about this. First your example shows this
> > being done in 'Main' which is not the Swing (AWT Event) thread. There
> > would not be a problem with the above code using invokeAndWait.
>
> I tried to give one example program. but by mistake i called update
method
> in main. It is suppossed to be called from ?valueChanged? and
?stateChanged?
> methods.
Ok.
> Yes. It may be ?valueChanged? or ?stateChanged?, once it is completed
the
> update method then only it should execute the next statement.
> If I use third thread with invokeAndWait, it is running without getting
> struck. But it is not waiting to finish update()/render() to finish
> executing. So it is giving some problems in updating the objects in SVG
> Canvas.
So you have two basic options:
1) Move the code that is currently in valueChanged/stateChanged
into the Runnable that runs in the UpdateManager Thread. This
can be a little tricky since you generally need to pull any
data out of the Swing components up front (also you may find
that you get updates 'piled up' in the UpdateManager).
2) Modify your valueChanged/stateChanged methods so that they
do nothing or 'delay' the action until the UpdateManager
runnable
completes.
This is probably the 'better' of the two. So I've written code
that sets a Runnable that should be run in the AWT EventQueue
when the current update completes. This is a basic outline
of the approach.
bool runnableRunning; // Set to true when you send a Runnable
to UpdateManager
NextRunnable nextRunnable; // The runnable to run
_in_the_swing_thread_ when current
// Update Manager runnable
completes.
// Returns true while this component has a runnable queued in
UpdateManager
public synchronized bool isUpdateRunning() {
return runnableRunning;
}
// Used to send a runnable to the canvas's update manager
// ensures that runnableRunning is set properly, and
// that updateFinished is called when it completes.
public synchronized void startUpdate(final Runnable r) {
runnableRunning = true;
canvas.getUpdateManager().invokeLater(
new Runnable() {
public void run() { r.run(); updateFinished();
}});
}
// Called when my runnable finishes in the UpdateManager
// thread. Sends 'queued' Swing work to the AWT Event queue.
public synchronized void updateFinished() {
runnableRunning = false;
Runnable run = nextRunnable;
nextRunnable = null;
SwingUtilities.invokeLater(run);
}
public class NextRunnable implements Runnable() {
NextRunnable nr;
NextRunnable(NextRunnable nr) {
this.nr = nr;
}
public void run() {
// Do all the queued actions in order...
if (nr != null) nr.doAction();
doAction();
// push changes to Canvas at the end...
render();
}
public void doAction() { }
}
// If we have an update running then queue this
// stateChanged event for processing after the
// current update completes.
public stateChanged(final ChangeEvent ce) {
synchronized (this) {
if (isUpdateRunning()) {
nextRunnable = new NextRunnable(nextRunnable) {
public void doAction() { doStateChanged(ce) }
}
return; // we have queued our work for later.
}
}
doStateChange(ce);
render();
}
public void doStateChange(ChangeEvent ce) {
// Do normal work from stateChange but not 'render'
}
I hope this helps....
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]