12

How to remove icon from JOptionPane?

ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
int result = JOptionPane.showConfirmDialog((Component) null, label, "ScreenPreview", JOptionPane.OK_CANCEL_OPTION);

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Code Hungry
  • 3,930
  • 22
  • 67
  • 95

3 Answers3

25

You can do it by directly specifying the Look and Feel of your message.

Your code will take the default one, while this one will use the "PLAIN_MESSAGE" style, which lacks the icon. The behaviour of the component remains unchanged.

JOptionPane.showConfirmDialog(null, label, "ScreenPreview", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

More info: http://docs.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html

Dth
  • 1,916
  • 3
  • 23
  • 34
2

This is fairly easy by using a transparent icon as below (as opposed to the black 'splash image'). Though note that while option pane offers some 'wiggle space' in terms of how it is displayed, go to change a couple of things and it quickly becomes easier to use a JDialog instead.

Icon Free Option Pane

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

class IconFree {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // A transparent image is invisible by default.
                Image image = new BufferedImage(
                        1, 1, BufferedImage.TYPE_INT_ARGB);
                JPanel gui = new JPanel(new BorderLayout());
                // ..while an RGB image is black by default.
                JLabel clouds = new JLabel(new ImageIcon(new BufferedImage(
                        250, 100, BufferedImage.TYPE_INT_RGB)));
                gui.add(clouds);

                JOptionPane.showConfirmDialog(null, gui, "Title",
                        JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        new ImageIcon(image));
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

Write -1 in place of JOptionPane.QUESTION_MESSAGE.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Aldrik
  • 21
  • 1
  • Why `-1`? What does that mean? Why not use one of the `JOptionPane` constants? – Robert Aug 25 '18 at 15:55
  • It is meant to look this way: JOptionPane.showMessageDialog(parent, message, status, -1 ); so -1 will hide the icon used with showMessageDialog. – tonimaroni Jun 09 '21 at 11:44