12

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?

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Harry Martland
  • 604
  • 2
  • 14
  • 26

3 Answers3

19

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);
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
  • 6
    `String input = JOptionPane.showInputDialog(null, "Please enter new quantity", "Please enter new quantity", JOptionPane.QUESTION_MESSAGE,null,null,"default text").toString;` – Neifen Sep 28 '11 at 07:54
  • 1
    ^^ I think that should be voted as the best answer to this question as it allows for a title also .But for some reason it gives me an error. – Hele Aug 05 '13 at 09:42
  • 2
    The reason for NullPointer exception is that you are using `toString()` on the resulting object of `JOptionPane`. You first need to check that the object is not null, and then use `toString()` after. @Neifen 's comment will have null pointer exception if the entry in the input dialog is blank. – user3735633 Jun 29 '15 at 17:47
11

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");
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
Tomás Marques
  • 111
  • 1
  • 2
0

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

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

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • I want the user to be able to enter a number when I did this JOptionPane.showInputDialog(null, "Please enter new quantity", "Please enter new quantity", 1) It just displayed a pop up with no input box, am I doing something wrong??? – Harry Martland Jul 20 '11 at 13:53
  • @Ste T You will be able to. Change your method call to `String input = JOptionPane.showInputDialog(null, "Please enter new quantity", "Title", JOptionPane.QUESTION_MESSAGE)`; – Jeffrey Jul 20 '11 at 13:55
  • Please check your links as they appear in the 'preview' in future, and note that it is best to link to the JavaDocs of the current version (adding '6' to the search for `ClassName+javadoc+6` should do it). – Andrew Thompson Jul 20 '11 at 13:56