I have a program that creates a JFrame and makes it visible. Is there anyway to know when the JFrame is fully drawn and visible? Right now a hacky "wait and pray" method is being used.
Thanks for the help!
I have a program that creates a JFrame and makes it visible. Is there anyway to know when the JFrame is fully drawn and visible? Right now a hacky "wait and pray" method is being used.
Thanks for the help!
You could add a WindowListener
to the JFrame
; then when windowOpened
is fired, the window should be visible.
You can also try overriding Component.addNotify()
which I believe is called when the component is drawn. Just make sure to call super.addNotify()
.
public void addNotify() {
super.addNotify();
// after displayed handling
}
to answer the "How can yield and specifically wait for the Swing thread to execute" question:
you can use a SwingWorker.
edit reading your comment once more i see a potentially problematic statement:
I'm creating a JFrame in a thread, then I want that thread to wait for the JFrame to appear.
are you calling your frame.setVisible(true)
in this thread as well? If so, you should probably be reminded that Swing painting should all be handled on the AWT EventQueue. This is where the SwingWorker
comes in.
However, what you may need for the 'yield and wait' is a wait/notify operation. In your calling thread you can wait on a shared Object:
synchronized (frameShowingLock) {
frameShowingLock.wait()'
}
then in your SwingWorker, or wherever you call frame.setVisible(true) you can finish the process by using the notifyAll()
method to wake up your waiting thread:
synchronized (frameShowingLock) {
frameShowingLock.notifyAll()'
}