Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove icon from JOptionPane

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

like image 864
Code Hungry Avatar asked Jun 07 '13 10:06

Code Hungry


2 Answers

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

like image 56
Dth Avatar answered Nov 03 '22 11:11

Dth


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);
    }
}
like image 2
Andrew Thompson Avatar answered Nov 03 '22 12:11

Andrew Thompson