30

I'm trying to display a JDialog in Windows. How do I show a JDialog (like JFrame) on my Windows taskbar?

Nathan
  • 6,095
  • 6
  • 42
  • 63
or123456
  • 1,563
  • 7
  • 22
  • 44

4 Answers4

37

A dialog itself cannot have a task bar entry, but you can construct a frame that does not have any visible effect and use it as a parent for the dialog. Then it will look like the dialog has a task bar entry. The following code shows you how to do it:

class MyDialog extends JDialog {

    private static final List<Image> ICONS = Arrays.asList(
            new ImageIcon("icon_16.png").getImage(), 
            new ImageIcon("icon_32.png").getImage(),
            new ImageIcon("icon_64.png").getImage());

    MyDialog() {
        super(new DummyFrame("Name on task bar", ICONS));
    }

    public void setVisible(boolean visible) {
        super.setVisible(visible);
        if (!visible) {
            ((DummyFrame)getParent()).dispose();
        }
    }
}

class DummyFrame extends JFrame {
    DummyFrame(String title, List<? extends Image> iconImages) {
        super(title);
        setUndecorated(true);
        setVisible(true);
        setLocationRelativeTo(null);
        setIconImages(iconImages);
    }
}
Ingo Kegel
  • 42,759
  • 9
  • 65
  • 97
  • But there is no use in using modal dialog if we are using like this right? – Vivek MVK Dec 19 '15 at 22:37
  • 1
    Actually, there is: if there is a common login dialog that is used even at run-time (to re-prompt for credentials due to expired authentication), then if the developer desires a JDialog for its ability to be modal for the run-time case, then this approach helps re-use that JDialog code during the first-time login case too when there is nothing to be modal to. Some tweaks required. – Greg Smethells Feb 02 '16 at 22:51
32

I found the answer to your question because I had the opposite problem. I had a JDialog that was showing in the taskbar and it took me forever to figure out how to prevent it from showing. Turns out if you pass a null parent to the JDialog constructor, your dialog will show in the taskbar.

JDialog dialog = new JDialog((Dialog)null);

The cast to java.awt.Dialog is to avoid the ambiguous constructor.

Lunchbox
  • 1,376
  • 8
  • 13
  • 2
    Alternatively, cast to `Window`. Both work. This seems to be the intended design. Do this rather than defining your own class. – Justin Sep 05 '15 at 05:42
  • 2
    Why isn't this the top answer? It's so much better and simpler – Hello234 Apr 05 '20 at 21:53
14
class MyDialog extends JDialog {
    MyDialog() {
        super(null, java.awt.Dialog.ModalityType.TOOLKIT_MODAL);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
}
Elliot Evers
  • 141
  • 1
  • 4
7

Dialogs are shown in the taskbar, when they have no owner. The possibility to have unowned Dialogs was added to AWT in Java 6. Unfortunately, at this time, the Swing class JDialog had already constructors with a predefined behavior regarding absent or null owners, working around the limitations of the previous Java versions. This behavior can’t be changed due to compatibility concerns.

Thus, when you use the old constructors JDialog() and those accepting a Frame argument, they exhibit the behavior compatible with the older versions, creating an invisible dummy Frame as owner if none is specified. So the Dialogs created this way are always owned by a Frame.

This is also incorporated into the documentation:

NOTE: This constructor does not allow you to create an unowned JDialog. To create an unowned JDialog you must use either the JDialog(Window) or JDialog(Dialog) constructor with an argument of null.

The named constructors are new to Java 6, as the possibility to have a Dialog owned by another Dialog or a Window was added in that version as well. Since these new constructors do not have to be compatible to a previous version, they can support unowned Dialogs. This is the reason why the solution in this answer works.

You may also use the constructor taking a ModalityType like in this answer as this constructor is also new two Java 6 and supports unowned dialogs. But you don’t need to create a subclass of JDialog to use this constructor.

Community
  • 1
  • 1
Holger
  • 243,335
  • 30
  • 362
  • 661
  • Also explained [here](http://stackoverflow.com/a/19117575/2711488) but I took the opportunity to go a bit into the details of the reasons… – Holger Sep 07 '15 at 15:11