New Swing Window Not Drawn

H

Hal Vaughan

I've created a small class that's not too different from a JOptionPane. The
purpose is to use it when I need to put up a "Please Wait" type message or
something similar. I'm using a separate thread to show and hide the
window. The other actions that need to happen while this window appears
and later disappears are happening, but the window is not actually
rendered.

I know I need to use a separate thread when I have actions going on while
new Swing components are rendered or changed, and I'm doing that, but that
doesn't seem to make a difference.

Here's the method that is supposed to show the window and check periodically
to see if the window should be closed:

public void activate() {
//flagActive is set to false when the window should disappear
flagActive = true;
new Thread(new Runnable() {
public void run() {
System.out.println("-----Opening Wait Window.");
//jSelf is the JFrame class for the window
jSelf.setVisible(true);
while (true) {
try {Thread.sleep(50);} catch (Exception e) {
System.out.println("Insomnia");
}
if (!flagActive) break;
}
System.out.println("-----Closing Wait Window.");
jSelf.setVisible(false);
}
}).start();
return;
}

I've had cases where I've used different threads like this before, but it
was always the new or internal thread that did the actions and the Swing
events were done by the "regular" thread.

Why is the window not being rendered here, even though it has its own thread
and Swing should be able to have time to render it?

Thanks!

Hal
 
D

Daniel Pitts

Hal said:
I've created a small class that's not too different from a JOptionPane. The
purpose is to use it when I need to put up a "Please Wait" type message or
something similar. I'm using a separate thread to show and hide the
window. The other actions that need to happen while this window appears
and later disappears are happening, but the window is not actually
rendered.

I know I need to use a separate thread when I have actions going on while
new Swing components are rendered or changed, and I'm doing that, but that
doesn't seem to make a difference.

Here's the method that is supposed to show the window and check periodically
to see if the window should be closed:

public void activate() {
//flagActive is set to false when the window should disappear
flagActive = true;
new Thread(new Runnable() {
public void run() {
System.out.println("-----Opening Wait Window.");
//jSelf is the JFrame class for the window
jSelf.setVisible(true);
while (true) {
try {Thread.sleep(50);} catch (Exception e) {
System.out.println("Insomnia");
}
if (!flagActive) break;
}
System.out.println("-----Closing Wait Window.");
jSelf.setVisible(false);
}
}).start();
return;
}

I've had cases where I've used different threads like this before, but it
was always the new or internal thread that did the actions and the Swing
events were done by the "regular" thread.

Why is the window not being rendered here, even though it has its own thread
and Swing should be able to have time to render it?

Thanks!

Hal

All interaction with Swing components should be done on the Event
Dispatch Thread.

Look at the class SwingUtilities
<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/SwingUtilities.html>.
specifically the invokeLater() method

Instead of setting aFlag, you should simple fire an event on the EDT.

Even if you wanted to wait for the flag to change, you need to use
synchronization. You also shouldn't be using a busy wait (even if you
sleep for 50), but instead do something like this:

public void sleepWhileActive() {
synchronize(myLock) {
while (active) {
try {
myLock.wait()
} catch (InterruptedException e);
System.out.println("Insomnia");
}
}
}
}

public void setActive(boolean active) {
synchronize(myLock) {
this.active = active;
myLock.notifyAll();
}
}


But, like I said, you should be using the EDT instead...
public void activate() {
assert !SwingUtilities.isEventDispatchThread() : "This would cause
a deadlock"
SwingUtilities.invokeLater(new Runnable() {
System.out.println("-----Opening Wait Window.");
//jSelf is the JFrame class for the window
jSelf.setVisible(true);
});
doExpensiveOperation();
SwingUtilities.invokeLater(new Runnable() {
System.out.println("-----Closing Wait Window.");
jSelf.setVisible(false);
});
}


Well, hope this helps.
Daniel.
 
T

Tom Hawtin

Hal said:
public void activate() {
//flagActive is set to false when the window should disappear
flagActive = true;
new Thread(new Runnable() {
public void run() {
System.out.println("-----Opening Wait Window.");
//jSelf is the JFrame class for the window
jSelf.setVisible(true);
while (true) {
try {Thread.sleep(50);} catch (Exception e) {
System.out.println("Insomnia");
}
if (!flagActive) break;
}
System.out.println("-----Closing Wait Window.");
jSelf.setVisible(false);
}
}).start();
return;
}

Write out a million time "I shall not use Swing components on the Event
Dispatch Thread (EDT)."

It should be simple enough to update the visible state using a bit of
EventQueue.invokeLater:

private volatile active;
public void setActive(boolean active) {
//flagActive is set to false when the window should disappear
this.active = active;
java.awt.Event.invokeLater(new Runnable() { public void run() {
//jSelf is the JFrame class for the window
jSelf.setVisible(MyOuterClass.this.active);
}});
}

For experts: Use javax.swing.Timer to prevent the frame flashing on and
off for very short bouts of activity. Technically javax.swing.Timer
should be called on the EDT (within the invokeLater).

Tom Hawtin
 
H

Hal Vaughan

Tom said:
Write out a million time "I shall not use Swing components on the Event
Dispatch Thread (EDT)."

Okay. Did the research and see what you mean. In conjunction with the
other post, that's a big help in figuring it out. I tried using
invokeLater() before, but had troubles with it.

Thanks!

Hal
 
H

Hal Vaughan

Daniel said:
All interaction with Swing components should be done on the Event
Dispatch Thread.

Look at the class SwingUtilities
<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/SwingUtilities.html>.
specifically the invokeLater() method

Instead of setting aFlag, you should simple fire an event on the EDT.

Even if you wanted to wait for the flag to change, you need to use
synchronization. You also shouldn't be using a busy wait (even if you
sleep for 50), but instead do something like this:

public void sleepWhileActive() {
synchronize(myLock) {
while (active) {
try {
myLock.wait()
} catch (InterruptedException e);
System.out.println("Insomnia");
}
}
}
}

public void setActive(boolean active) {
synchronize(myLock) {
this.active = active;
myLock.notifyAll();
}
}


But, like I said, you should be using the EDT instead...
public void activate() {
assert !SwingUtilities.isEventDispatchThread() : "This would cause
a deadlock"
SwingUtilities.invokeLater(new Runnable() {
System.out.println("-----Opening Wait Window.");
//jSelf is the JFrame class for the window
jSelf.setVisible(true);
});
doExpensiveOperation();
SwingUtilities.invokeLater(new Runnable() {
System.out.println("-----Closing Wait Window.");
jSelf.setVisible(false);
});
}

Thanks -- good examples and code and points to ponder.

It would seem to me I might be able to make it a bit simpler. This class is
supposed to be a simple "Please Wait..." window that I can customize and
flash up during longer operations. I might do it with a JOptionPane, but I
also need to understand this situation anyway. I've tried using
invokeLater before and had some problems getting it to work.

Instead of having any "aFlag" or "active" or any other variable, can't I do
something like this:

public void activate() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("-----Opening Wait Window.");
jSelf.setVisible(true);
}
});
return;
}

public void deactivate() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("-----Closing Wait Window.");
jSelf.setVisible(false);
}
});
return;
}

That would provide simple methods to both open and close the window.

The CPU cycle using activities are in another class. I was trying to make
this class a simple one that I could set up in a couple lines and call
without having to create separate threads or anything like that -- in other
words, the class itself would create any needed Runnable or Threads.

The calling code would look like this (assuming this class name is
WaitDialog):

WaitDialog wd = new WaitDialog();
//Follow with settings for wd here
wd.activate();
bigClass.doExtensiveThings();
wd.deactivate();

It seems to me that would still do everything the same way as what you're
talking about in the same way, just that the extra typing of creating a
Runnable() is in the WaitDialog class so all I have to do is call it with
two calls instead of creating a Runnable each time I use it.

Is that reasoning correct? If it is, I'm still having trouble getting it to
work, but I'd like to know if I understand this correctly first.

Hal
 
H

Hal Vaughan

Tom said:
Write out a million time "I shall not use Swing components on the Event
Dispatch Thread (EDT)."

It should be simple enough to update the visible state using a bit of
EventQueue.invokeLater:

     private volatile active;
     public void setActive(boolean active) {
         //flagActive is set to false when the window should disappear
         this.active = active;

Do you mean "java.awt.EventQueue.invokeLater()" on the next line?
         java.awt.Event.invokeLater(new Runnable() { public void run() {
                         //jSelf is the JFrame class for the window
                         jSelf.setVisible(MyOuterClass.this.active);
         }});
     }

This leads to something I've had trouble with before.  If I use
invokeLater() (in this case I'm using it in SwingUtilities), then the GUI
update happens AFTER the work I'm doing.

What do I do if I need to update the GUI before performing other actions?
For example, if I want to warn the user the next operation could take a few
minutes?

I've tried working with this before, and could never get invokeLater() or
invokeAndWait() (which is apparently not as good a method to use) to update
the Swing components before other non-GUI related work is done.

I'd like to be able to do something like this:

        User presses button
        Display is updated with "Working, please wait..." type of message
        Run functions that could take from 1 second to a minute or more
        Update display to get rid of "Please wait..."

No matter what I try, though, the 2nd step does not seem possible and I
cannot find a way to update the GUI before the 3 step (the actual work) is
done.

How do I work with this?

Hal
 
D

Daniel Pitts

Hal said:
Daniel said:
All interaction with Swing components should be done on the Event
Dispatch Thread.

Look at the class SwingUtilities
<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/SwingUtilities.html>.
specifically the invokeLater() method

Instead of setting aFlag, you should simple fire an event on the EDT.

Even if you wanted to wait for the flag to change, you need to use
synchronization. You also shouldn't be using a busy wait (even if you
sleep for 50), but instead do something like this:
[snip my code]
Thanks -- good examples and code and points to ponder.

It would seem to me I might be able to make it a bit simpler. This class is
supposed to be a simple "Please Wait..." window that I can customize and
flash up during longer operations. I might do it with a JOptionPane, but I
also need to understand this situation anyway. I've tried using
invokeLater before and had some problems getting it to work.

Instead of having any "aFlag" or "active" or any other variable, can't I do
something like this:
[snip code]
That would provide simple methods to both open and close the window.

The CPU cycle using activities are in another class. I was trying to make
this class a simple one that I could set up in a couple lines and call
without having to create separate threads or anything like that -- in other
words, the class itself would create any needed Runnable or Threads.

The calling code would look like this (assuming this class name is
WaitDialog):

WaitDialog wd = new WaitDialog();
//Follow with settings for wd here
wd.activate();
bigClass.doExtensiveThings();
wd.deactivate();

It seems to me that would still do everything the same way as what you're
talking about in the same way, just that the extra typing of creating a
Runnable() is in the WaitDialog class so all I have to do is call it with
two calls instead of creating a Runnable each time I use it.

Is that reasoning correct? If it is, I'm still having trouble getting it to
work, but I'd like to know if I understand this correctly first.

Hal
That seems like a good approach to me, the only thing I would watch out
for is to make sure your calls to "activate" and "deactivate" *don't*
occure on the EDT, otherwise your doExtensiveThings is going to block
the EDT, and your window won't show up.

Generally the way to do this is to
wd.activate();
new Thread(new Runnable() {
bigObject.expensive();
wd.deactivate();
}).start();

Ofcourse, it might be useful to create a class "ExpensiveOperation"
abstract class ExpensiveOperation extends Thread {
private final WaitDialog wait;
public ExpensiveOperation(WaitDialog dialog) {
wait = dialog;
}

public void run() {
wait.activate();
try {
performExpensiveOperation();
} finally {
wait.deactivate();
}
}
protected abstract void performExpesniveOperation();
}

Also, if you can use Java 1.6, look into the new SwingWorker class. It
allows you a lot more interaction between the EDT and the worker thread.
 
H

Hal Vaughan

Daniel said:
Hal said:
Daniel said:
All interaction with Swing components should be done on the Event
Dispatch Thread.

Look at the class SwingUtilities
specifically the invokeLater() method

Instead of setting aFlag, you should simple fire an event on the EDT.

Even if you wanted to wait for the flag to change, you need to use
synchronization. You also shouldn't be using a busy wait (even if you
sleep for 50), but instead do something like this:
[snip my code]
Thanks -- good examples and code and points to ponder.

It would seem to me I might be able to make it a bit simpler. This class
is supposed to be a simple "Please Wait..." window that I can customize
and
flash up during longer operations. I might do it with a JOptionPane, but
I
also need to understand this situation anyway. I've tried using
invokeLater before and had some problems getting it to work.

Instead of having any "aFlag" or "active" or any other variable, can't I
do something like this:
[snip code]
That would provide simple methods to both open and close the window.

The CPU cycle using activities are in another class. I was trying to
make this class a simple one that I could set up in a couple lines and
call without having to create separate threads or anything like that --
in other words, the class itself would create any needed Runnable or
Threads.

The calling code would look like this (assuming this class name is
WaitDialog):

WaitDialog wd = new WaitDialog();
//Follow with settings for wd here
wd.activate();
bigClass.doExtensiveThings();
wd.deactivate();

It seems to me that would still do everything the same way as what you're
talking about in the same way, just that the extra typing of creating a
Runnable() is in the WaitDialog class so all I have to do is call it with
two calls instead of creating a Runnable each time I use it.

Is that reasoning correct? If it is, I'm still having trouble getting it
to work, but I'd like to know if I understand this correctly first.

Hal
That seems like a good approach to me, the only thing I would watch out
for is to make sure your calls to "activate" and "deactivate" *don't*
occure on the EDT, otherwise your doExtensiveThings is going to block
the EDT, and your window won't show up.

Thanks! I'm beginning to get a feel for this.

If activate() and deactivate() have separate threads within them, won't that
take care of that? In other words, the EDT thread calls activate(), and
within activate() I use SwingUtilities.invokeLater() and put the thread in
there to update the GUI? That would still be using a different thread, but
just within the method called instead of outside. It may not be much of a
difference, but it would make the coding when I use that class easier.

Also, as I've experimented, I've found that if I put any GUI updates within
a Runnable() that is called with SwingUtilities.invokeLater(), those
functions are not done until after the doExtensiveThings() routine is
called. What do I do if I want to update the Swing GUI, THEN
doExtensiveThings(), then update the GUI again? It seems invokeLater()
does such a good job at invoking later all GUI work that it calls is held
until the other stuff is done. That makes it hard if I want to start with
a message to the user saying, "This could take a while, please wait..."

Is there a way to repaint/refresh the GUI first, then do the work?

Again, thanks!

Hal
 
K

Knute Johnson

Hal said:
Daniel said:
Hal said:
Daniel Pitts wrote:
All interaction with Swing components should be done on the Event
Dispatch Thread.

Look at the class SwingUtilities
specifically the invokeLater() method

Instead of setting aFlag, you should simple fire an event on the EDT.

Even if you wanted to wait for the flag to change, you need to use
synchronization. You also shouldn't be using a busy wait (even if you
sleep for 50), but instead do something like this:
[snip my code]
Thanks -- good examples and code and points to ponder.

It would seem to me I might be able to make it a bit simpler. This class
is supposed to be a simple "Please Wait..." window that I can customize
and
flash up during longer operations. I might do it with a JOptionPane, but
I
also need to understand this situation anyway. I've tried using
invokeLater before and had some problems getting it to work.

Instead of having any "aFlag" or "active" or any other variable, can't I
do something like this:
[snip code]
That would provide simple methods to both open and close the window.

The CPU cycle using activities are in another class. I was trying to
make this class a simple one that I could set up in a couple lines and
call without having to create separate threads or anything like that --
in other words, the class itself would create any needed Runnable or
Threads.

The calling code would look like this (assuming this class name is
WaitDialog):

WaitDialog wd = new WaitDialog();
//Follow with settings for wd here
wd.activate();
bigClass.doExtensiveThings();
wd.deactivate();

It seems to me that would still do everything the same way as what you're
talking about in the same way, just that the extra typing of creating a
Runnable() is in the WaitDialog class so all I have to do is call it with
two calls instead of creating a Runnable each time I use it.

Is that reasoning correct? If it is, I'm still having trouble getting it
to work, but I'd like to know if I understand this correctly first.

Hal
That seems like a good approach to me, the only thing I would watch out
for is to make sure your calls to "activate" and "deactivate" *don't*
occure on the EDT, otherwise your doExtensiveThings is going to block
the EDT, and your window won't show up.

Thanks! I'm beginning to get a feel for this.

If activate() and deactivate() have separate threads within them, won't that
take care of that? In other words, the EDT thread calls activate(), and
within activate() I use SwingUtilities.invokeLater() and put the thread in
there to update the GUI? That would still be using a different thread, but
just within the method called instead of outside. It may not be much of a
difference, but it would make the coding when I use that class easier.

Also, as I've experimented, I've found that if I put any GUI updates within
a Runnable() that is called with SwingUtilities.invokeLater(), those
functions are not done until after the doExtensiveThings() routine is
called. What do I do if I want to update the Swing GUI, THEN
doExtensiveThings(), then update the GUI again? It seems invokeLater()
does such a good job at invoking later all GUI work that it calls is held
until the other stuff is done. That makes it hard if I want to start with
a message to the user saying, "This could take a while, please wait..."

Is there a way to repaint/refresh the GUI first, then do the work?

Again, thanks!

Hal

This is how you do it.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JFrame implements Runnable {
JLabel label;
int n;

public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

label = new JLabel(" ");
add(label,BorderLayout.CENTER);

pack();
setVisible(true);

new Thread(this).start();
}

public void run() {
while (true) {
try {
Thread.sleep(1000);
Runnable r = new Runnable() {
public void run() {
label.setText(Integer.toString(n++));
}
};
EventQueue.invokeLater(r);
} catch (InterruptedException ie) { }
}
}

public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
new test();
}
};
EventQueue.invokeLater(r);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top