Search code examples
javaswingiconsjoptionpane

How do I diplay an icon on a JOptionPane.showInputDialog?


I have the following code:

 principleInputString = JOptionPane.showInputDialog(null,"Enter the   principle.","Find Interest Rate", JOptionPane.PLAIN_MESSAGE);

I want to display an icon in the JOptionPane as well as my message. How do I do that? I get an error message when I do this:

final ImageIcon icon = new ImageIcon(new URL("http://iconizer.net/files/Devine_icons/thumb/128/Calculater.png"));
 principleInputString = JOptionPane.showInputDialog(null,"Enter the   principle.","Find Interest Rate", JOptionPane.PLAIN_MESSAGE,icon);

Solution

  • To add an image to an JOptionPane I believe this is what you are after

    import javax.swing.*;
    import java.awt.*;
    
    public class Test
    {
        public static void main(String[] args)
        {
            final ImageIcon icon = new ImageIcon("yourimage.imagetype");
            Image image2 = icon.getImage().getScaledInstance(200,200,0);
            String principleInputString = JOptionPane.showInputDialog(null,"Enter the   principle.","Find Interest Rate", JOptionPane.PLAIN_MESSAGE, new ImageIcon(image2),  null, "").toString();
        }
    }
    

    For more information look here