4

I am curious, I am wondering if there exist a way to make the top of the JOptionPane into a different color, say red or orange. Also i was wondering how to change the image on the left of the JOptionPane. I am guessing it is not possible because it is already a method being used from java. But i am no expert.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Renuz
  • 1,587
  • 7
  • 21
  • 34
  • `make the top of the JOptionPane into a different color` are you meaning toolbar with close button – mKorbel May 07 '12 at 21:59
  • 1
    [part of Look and Feels can do that](http://stackoverflow.com/a/3954646/714968) or for WindowsLookAndFeel is possible some dirty hacks too, not good way for newbee use proper Look and Feel instead – mKorbel May 07 '12 at 22:14

2 Answers2

7

There are three options here:

  1. Use one of the predefined icons using the respective message type:
    JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.", "Inane error", JOptionPane.ERROR_MESSAGE);

  2. Use a custom icon:
    JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.", "Inane custom dialog", JOptionPane.INFORMATION_MESSAGE, icon);

  3. Use a look & feel to have consistent icons all over your application: How to Set the Look and Feel

Have a look at this page of the Java Tutorial for more information on dialogs.

rolve
  • 10,083
  • 4
  • 55
  • 75
6

You can add your own ImageIcon to a JOptionPane -- check out the API, and try calling the methods with an Icon field, passing in your own ImageIcon to see how this works. You can also create a complex JPanel, a full fledged GUI-containing JPanel, and make it the basis for your JOptionPane, simply by passing it in as the Object parameter (usually the second paramter) of the JOptionPane.showXXX(...) method.

Another option is to create and use your own modal JDialog.

A working code :

import java.awt.Color;
import javax.swing.*;

public class JOptionPaneExample
{
    private void createAndDisplayGUI()
    {
        JOptionPane.showMessageDialog(null, getOptionPanel(), "Modified JOptionPane : ", JOptionPane.PLAIN_MESSAGE);
    }

    public static void main(String... args)
    {   
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new JOptionPaneExample().createAndDisplayGUI();
            }
        });
    }

    private JPanel getOptionPanel()
    {
        JPanel panel = new JPanel();
        panel.setOpaque(true);
        panel.setBackground(Color.RED);
        try
        {
            java.net.URL url = new java.net.URL("http://gagandeepbali.uk.to/gaganisonline/images/swing/geek.gif");
            ImageIcon image = new ImageIcon(url);
            JLabel label = new JLabel("I am one MODIFIED JOPTIONPANE's LABEL.", image, JLabel.RIGHT);
            panel.add(label);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return panel;
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I like @rolve's answer better. 1+ to him. – Hovercraft Full Of Eels May 07 '12 at 22:00
  • 1
    @HovercraftFullOfEels : Sorry for the edit, though, this thing as I learned from YOU once, was amazing, so just added a small code part. I wish All can learn this thing, and see how easy and wonderful it can work :-) Revert it back, if I did anything wrong in any way :( – nIcE cOw May 08 '12 at 07:40