user4307551
user4307551

Reputation:

Adding a JTextField into a JOptionPane: ShowOptionDialog

I was wondering if it is possible to add a JTextField to a ShowOptionDialog box.

     int optionChosen = JOptionPane.showOptionDialog(finishPayInput,     
     dialogPanel, "The Title", JOptionPane.NO_OPTION,      
     JOptionPane.QUESTION_MESSAGE, null, options , options[0]); 

When I run the program, the dialog box shows, but the JTextField doesn't display.

Upvotes: 0

Views: 3931

Answers (2)

Branislav Lazic
Branislav Lazic

Reputation: 14816

Yes of course. Easiest solution:

JTextField txt = new JTextField();
JOptionPane.showOptionDialog(null, finishPayInput, "The Title", JOptionPane.NO_OPTION,
                JOptionPane.QUESTION_MESSAGE, null, null, null);

However, if you only want to display JTextField (to obtain users input), best idea would be to use JOptionPane.showInputDialog:

JOptionPane.showInputDialog(null, "Insert value: ", "The title", JOptionPane.QUESTION_MESSAGE);

Upvotes: 1

Jens
Jens

Reputation: 69460

You cannot add a textfield to the JOptionPane.showOptionDialog first Parameter is the parent component not a child component.

See the documentation:

public static int showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue) throws HeadlessException

Brings up a dialog with a specified icon, where the initial choice is determined by the initialValue parameter and the number of choices is determined by the optionType parameter. If optionType is YES_NO_OPTION, or YES_NO_CANCEL_OPTION and the options parameter is null, then the options are supplied by the look and feel.

The messageType parameter is primarily used to supply a default icon from the look and feel. Parameters:parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is usedmessage - the Object to displaytitle - the title string for the dialogoptionType - an integer designating the options available on the dialog: DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION, or OK_CANCEL_OPTIONmessageType - an integer designating the kind of message this is, primarily used to determine the icon from the pluggable Look and Feel: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGEicon - the icon to display in the dialogoptions - an array of objects indicating the possible choices the user can make; if the objects are components, they are rendered properly; non-String objects are rendered using their toString methods; if this parameter is null, the options are determined by the Look and FeelinitialValue - the object that represents the default selection for the dialog; only meaningful if options is used; can be nullReturns:an integer indicating the option chosen by the user, or CLOSED_OPTION if the user closed the dialogThrows:HeadlessException - if GraphicsEnvironment.isHeadless returns trueSee Also:GraphicsEnvironment.isHeadless()

Upvotes: 0

Related Questions