How do I get a link to the currently displayed Java Swing JDialog window?
I am using a Java Swing framework that displays toast messages using Java Swing JDialog boxes. I need to get a link to them so that I can close them programmatically. The framework creates dialog boxes, but does not retain links to them. Can I get a link to the currently displayed field?
Thank.
Try the following:
import java.awt.Window;
import javax.swing.JDialog;
public class Test {
public static void main(String[] args) {
JDialog d = new JDialog((Window)null,"Demo Dialog");
for (Window w : JDialog.getWindows()) {
if ( w instanceof JDialog) {
System.out.println(((JDialog)w).getTitle());
}
}
}
}
And if you're puzzled:
(Window)null
just try compiling without it :)
(BTW passing an empty window to the constructor creates an unfinished dialog)
EDIT: Please note that this will give you links to ALL dialogs, regardless of whether they are visible, as the code demonstrates. To fix this, just ask the dialog if this is visible with isVisible ()
source to share
You can get a reference to all child windows using the getWindows () method in the Window class.
public static Window[] getWindows()
Returns an array of all Windows, both owned and ownerless, created by this application. If called from an applet, the array includes only the Windows accessible by that applet.
Be careful as I suspect that one of the returned windows will be your top-level frame / window. You can check window attributes such as title to determine which dialog you want to close.
There are also more specific versions: getOwnedWindows () and getOwnerlessWindows () depending on your needs.
source to share
if you want to get a link to the frontmost JDialog you need to listen for AWT events and update this link "live" and save it somewhere.
This is what I am doing in my own Swing framework:
public class ActiveWindowHolder
{
public DefaultActiveWindowHolder()
{
_current = null;
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener()
{
public void eventDispatched(AWTEvent e)
{
DefaultActiveWindowHolder.this.eventDispatched(e);
}
}, AWTEvent.WINDOW_EVENT_MASK);
}
public Window getActiveWindow()
{
return _current;
}
private void eventDispatched(AWTEvent e)
{
switch (e.getID())
{
case WindowEvent.WINDOW_ACTIVATED:
_current = ((WindowEvent) e).getWindow();
break;
case WindowEvent.WINDOW_DEACTIVATED:
_current = null;
break;
default:
// Do nothing (we are only interested in activation events)
break;
}
}
private Window _current;
}
Just make sure you create ActiveWindowHandler
in your main thread before showing any component; from this point, each call ActiveWindowHandler.getActiveWindow()
will return the window of the window itself (or JDialog
from JFrame
).
source to share
I don't see a way to find out what is currently open as a child of the frame - I only see ways to get parent frames.
Can you change the current classes that call dialog boxes? If so, you can add a container to keep track of everything as they open and close and add some methods to access that container.
source to share
If you create JDialog
explicitly and not using JOptionPane.showXXX
, you will already have a link to the dialog. This will definitely be my preferred approach; eg.
JDialog dlg = new JDialog(null, "My Dialog", false); // Create non-modal dialog.
dlg.setLayout(new BorderLayout());
dlg.add(myPanel, BorderLayout.CENTER);
dlg.pack();
dlg.setLocationRelativeTo(null);
dlg.setVisible(true);
source to share
either set the dialog to be modal, which makes your life very simple ...
OR when you initialize your JDialog do the following:
JDialog d = new JDialog();
d.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){
dialogClosedAlertFunction(); //goes wherever you want it to go
}
});
source to share