Search code examples
javajoptionpane

Make method that returns a JOptionPane.showMessageDialog with specified message?


What I am trying to ask here is this:

I've been typing JOptionPane.showMessageDialog, etc etc, all the time. So, I thought that I could make a method to make it shorter and easier, in this case named msgDialog().

public static String msgDialog(String message){
    return JOptionPane.showMessageDialog(null, message);
}

Why wouldn't this work? (error: cannot return a void result.)


Solution

  • JOptionPane#showMessageDialog doesn't return anything. Thus, You can do this instead

    public static void show(String s){
        JOptionPane.showMessageDialog(null, s);
    }