I'm trying to display a JDialog
in Windows. How do I show a JDialog
(like JFrame
) on my Windows taskbar?
问题:
回答1:
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);
}
}
回答2:
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.
回答3:
class MyDialog extends JDialog {
MyDialog() {
super(null, java.awt.Dialog.ModalityType.TOOLKIT_MODAL);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
回答4:
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 unownedJDialog
you must use either theJDialog(Window)
orJDialog(Dialog)
constructor with an argument ofnull
.
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.