Search code examples
javaswingdialogwindowjoptionpane

How do I make dialog windows in java?


I found a webpage on the java site on how to make dialog windows, but it isn't working when I try it. The site said to type:

JOptionPane.showMessageDialog(frame, "Window text.");

I'm just trying to make a window with a bit of text and an ok button, but when I type this in, my Eclipse IDE wants me to import something for the JOptionPane, and after I do that, it says the the "frame" part is incorrect, that it "cannot be resolved to a variable." What am I doing wrong here?


Solution

  • Start by making sure you have included the import javax.swing.JOptionPane; statement within your import portion of your code.

    Next, try using

    JOptionPane.showMessageDialog(null, "Window text.");
    

    instead.

    For example...

    import javax.swing.JOptionPane;
    
    public class TestDialog {
    
        public static void main(String[] args) {
            JOptionPane.showMessageDialog(null, "Window text.");
        }
    
    }
    

    Take a closer look at How to Make Dialogs for more details.

    You should also consult the JavaDocs when in doubt...