Home » Java » java – JOptionPane showInputDialog-Exceptionshub

java – JOptionPane showInputDialog-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment


I´m writing a programm in Java. It has a main class and also a seperate class called “username”. The function of this class is, that there should be a “JoptionPane ” window where you can write your username. Also this window should have a Name, a icon/ picture, a “Yes” and a “No” button and of course a question.

public class Username {
    public Username() {
        Image Icon = new ImageIcon("Pictures/House.jpg");
        String username = JOptionPane.showInputDialog(null,"What´s your username?","Username", JOptionPane.YES_NO_OPTION);

so. Now i would have a window, with a title, a question. But my icon is missing or the yes / no function. I´m very thankful if somebody could help me.

How to&Answers:

You need to use a different version of showOptionDialog():

public static Object showInputDialog(
        Component parentComponent,
        Object message,
        String title,
        int messageType,
        Icon icon,
        Object[] selectionValues,
        Object initialSelectionValue
) throws HeadlessException

This way you can directly pass the Icon. If you want the user to type their intput rather then select it from a predefined list of possible selections use selectionValues = null.