views:

60

answers:

1

I'm trying to make a dialogue box which displays an answer and will print to the screen but am having no success. The code looks like this:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dialogbox;
import javax.swing.JOptionPane;
/**
 *
 * @author Tyranax87
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String name = "";
        //Setting up a variable name of type string and initialising it to the empty string.
        name = JOptionPane.showInputDialog(null, "Please input your name");
        System.exit(0);
}
}

It will run just fine, but that will only bring up the dialog box asking the question. Is there a way to have the dialog box come up with pre-written text from the code?

+4  A: 

Try it like that:

name = (String)JOptionPane.showInputDialog(
                    null,
                    "Please input your name",
                    "Title of the window",
                    JOptionPane.PLAIN_MESSAGE,
                    icon,
                    null,
                    "default text");
Tarnschaf