Java JOptionPane default text

Harry Martland picture Harry Martland · Jul 20, 2011 · Viewed 21.8k times · Source

When I ask a user to enter a quantity for a program I have made using the code below, the default text is 3.

String input = JOptionPane.showInputDialog(null, "Please enter new quantity",
                                           JOptionPane.QUESTION_MESSAGE);

How do I change this?

Answer

Harry Joy picture Harry Joy · Jul 20, 2011

The method you have used is:

public static String showInputDialog(Component parentComponent,
                                     Object message,
                                     Object initialSelectionValue)

Here 3rd argument (initialSelectionValue) is default value in text field. You gave JOptionPane.QUESTION_MESSAGE as 3rd argument which is an int constant having value = 3. So you get 3 as a default value entered in text field.

Try this:

String input = JOptionPane.showInputDialog(null,
                "Please enter new quantity", "");

or this

String input = JOptionPane.showInputDialog(null,
                "Please enter new quantity", "Please enter new quantity",
                JOptionPane.QUESTION_MESSAGE);