问题
I am creating a dialog for showing a progress of files' copying. What I want to do is to completely remove dialog's icon image.
I created such kind of dialogs before providing an application's instance of FrameView
as an argument for JDialog
's constructor like this:
public class MyAppView extends FrameView {
// ...
@Action
public void showOptionsDialog() {
// Creating modal options' dialog
JDialog optionsDialog = new JDialog(this, true);
// ...
}
}
So, as I can see, I need a parent component to make my dialog with no icon. In my current case (when I have no parent frame view) I tried the following hack method setting a transparent icon but it doesn't work as I expect - I still see an empty area for an icon in dialog's title bar and (what's the worst) I still have a popup window when I click this area.
JFrame dummyFrame = new JFrame();
Image icon = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE);
dummyFrame.setIconImage(icon);
JDialog myDialog = new JDialog(dummyFrame, true);
Anyway, it must be possible to remove an icon. The working example is JOptionPane:
JOptionPane.showMessage(null, "My message");
Looking at JOptionPane
's source code one can find that static Frame JOptionPane.getRootFrame()
is used to get a parent for the calls when parent component is set to null
:
public static Frame getRootFrame() throws HeadlessException {
Frame sharedFrame =
(Frame)SwingUtilities.appContextGet(sharedFrameKey);
if (sharedFrame == null) {
sharedFrame = SwingUtilities.getSharedOwnerFrame();
SwingUtilities.appContextPut(sharedFrameKey, sharedFrame);
}
return sharedFrame;
}
So I've also tried to create my dialog as follows:
JDialog myDialog = new JDialog(JOptionPane.getRootFrame(), true);
but there is still no success. I have a standard Java icon when I try to apply this code snippet.
And my question is: what am I doing wrong? How to completely remove an icon of JDialog
as it is done by JOptionPane
?
P.S. I use JDK6.
回答1:
The solution is to make a dialog non-resizable and then it will be without an icon with any parent window.
JDialog myDialog = new JDialog(new Frame(), true);
myDialog.setResizable(false);
myDialog.setVisible(true);
Unfortunately, if your dialog is to be resizable there is no way to remove an icon except of setting a transparent icon.
回答2:
Maybe the following is more in the direction you want.
JProgressBar pb = new JProgressBar();
JOptionPane op = new JOptionPane(pb, JOptionPane.PLAIN_MESSAGE,
JOptionPane.DEFAULT_OPTION);
JDialog dlg = op.createDialog(MyJFrame.this, "Progress");
dlg.setVisible(true);
回答3:
In netbeans with design options:
initComponents();
Image img = new BufferedImage(1, 1,BufferedImage.TYPE_INT_ARGB_PRE);
this.setIconImage(img);
回答4:
I have no problem with the icon (though JDK7 on WindowsXP). The popup menu on the icon I suppressed in the dialog with setFocusableWindowState(false).
The JFrame like your code:
public MyJFrame() {
//URL url = this.getClass().getResource("transparent.gif");
//if (url != null) {
// ImageIcon icon = new ImageIcon(url);
// if (icon != null)
// setIconImage(icon.getImage());
//}
BufferedImage icon = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE);
//icon.setRGB(0, 0, 0x00000000);
setIconImage(icon);
}
and
JFrame dummyFrame = new MyJFrame();
JDialog myDialog = new JDialog(dummyFrame, true);
myDialog.setIconImage(dummyFrame.getIconImage()); // Not needed.
myDialog.setFocusableWindowState(false);
myDialog.setVisible(true);
来源:https://stackoverflow.com/questions/8504731/how-to-completely-remove-an-icon-from-jdialog