Harry Martland
Harry Martland

Reputation: 624

Java JOptionPane default text

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?

Upvotes: 14

Views: 25406

Answers (3)

Tomás Marques
Tomás Marques

Reputation: 121

This way it will work:

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

Upvotes: 12

Jeffrey
Jeffrey

Reputation: 44808

The method you are using is JOptionPane.showInputDialog(Component, Object, Object).

The method you want to use is JOptionPane.showInputDialog(Component, Object, String, int).

Upvotes: 0

Harry Joy
Harry Joy

Reputation: 59660

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);

Upvotes: 21

Related Questions