Jason Rogers
Jason Rogers

Reputation: 19344

JOptionPane : change the Icon

I have this layout in the JOptionPane:

JCheckBox checkbox = new JCheckBox("Do not show this message again.");
String message = "Level loaded successfully";  
Object[] params = {message, checkbox};  
int n = JOptionPane.showConfirmDialog(p, params, "Draft saving",
     JOptionPane.PLAIN_MESSAGE);  

everything is fine except for the display of the Icon..,

Ideally I would like not to have an Icon (hence the JOptionPane.PLAIN_MESSAGE), but I get a default Icon.

How can I change this ?

Jason

Upvotes: 6

Views: 47604

Answers (6)

The_dempa
The_dempa

Reputation: 115

Hope this would work:

Image icon = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
int option = JOptionPane.showConfirmDialog(
  null, "Do you really want to Exit Alarm Clock - By jLab", "Alarm Clock - By jLab",
  JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,icon
);

if (option == JOptionPane.YES_OPTION) {
  System.exit(0);
}

Upvotes: 1

obiero felix
obiero felix

Reputation: 11

To Have no Icon, try this.

String[] options = {"Ok", "Cancel"};

int choices = JOptionPane.showOptionDialog(
  Ann.this, "Do you Want To Exit Calculator ", "Exit Calculator",
  JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, 
  options, options[0]
);

Although I am using an OptionDialog, you can change it to a ConfirmDialog.

Upvotes: 1

Lukas Knuth
Lukas Knuth

Reputation: 25755

The method you're using doesn't specify an icon.

You should use showConfirmDialog(Component, Object, String, int , int , Icon), which enables you to pass an Icon as well.

Upvotes: 2

squidwardprops02
squidwardprops02

Reputation: 135

this is the format @Jesse Barnum was talking about

public static void showMessageDialog(Component parentComponent,
                                     Object message,
                                     String title,
                                     int messageType,
                                     Icon icon);

you supply int messageType with the messageType constants that also determines the icon

example: this one outputs a Question icon, using JOptionPane.QUESTION_MESSAGE

JOptionPane.showMessageDialog(null,"Are you OK?","HI",JOptionPane.QUESTION_MESSAGE,null);

this one outputs a Information icon

JOptionPane.showMessageDialog(null,"You are reading something","FYI",JOptionPane.INFORMATION_MESSAGE,null);

Upvotes: 3

Jesse Barnum
Jesse Barnum

Reputation: 6856

Use the version of the method that takes an icon parameter, and pass in null for the icon.

Upvotes: 8

mKorbel
mKorbel

Reputation: 109823

for example, works as I expected

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class OptionPaneOptions {

    private JPanel options;
    private Object[] items;

    public OptionPaneOptions() {
        options = new JPanel(new GridLayout(0, 3));
        options.add(createOptionPane("Plain Message", JOptionPane.PLAIN_MESSAGE));
        options.add(createOptionPane("Error Message", JOptionPane.ERROR_MESSAGE));
        options.add(createOptionPane("Information Message", JOptionPane.INFORMATION_MESSAGE));
        options.add(createOptionPane("Warning Message", JOptionPane.WARNING_MESSAGE));
        options.add(createOptionPane("Want to do something?", JOptionPane.QUESTION_MESSAGE));
        items = new Object[]{"First", "Second", "Third"};
        JComboBox choiceCombo = new JComboBox(items);
        options.add(titled(new JOptionPane(choiceCombo,
                JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION), "Question Message"));
        JFrame frame = new JFrame("JOptionPane'Options");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(options, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    static JOptionPane createOptionPane(String message, int type) {
        JOptionPane pane = new JOptionPane(message, type);
        String title = message;
        if (type == JOptionPane.QUESTION_MESSAGE) {
            title = "Question Message";
            pane.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION);
        }
        return titled(pane, title);
    }

     static <T extends JComponent> T titled(T c, String title) {
        c.setBorder(BorderFactory.createTitledBorder(title));
        return c;
    }

    public static void main(String[] args) {
        OptionPaneOptions test = new OptionPaneOptions();
    }
}

Upvotes: 6

Related Questions