Search code examples
javajtextfieldjoptionpanejdialog

How do I set the text in it at the beginning when the dialog opens?


I have the following code that displays a JDialog, it shows a text field, I assume it's a JTextField.

How do I set the text in it at the beginning when the dialog opens?

JOptionPane pane = new JOptionPane("", JOptionPane.QUESTION_MESSAGE);
pane.setWantsInput(true);
JDialog dialog = pane.createDialog(null, "Test");
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setVisible(true);

Solution

  • Use:

    pane.setInitialSelectionValue("foo");
    

    to set the input value that is initially displayed as selected to the user.