How to get a JOptionPane with three text fields

I want to know how I can make a messageBox from three input dialogs.

Like this:

JOptionPane.showInputMessageDialog("Enter your FirstName"); JOptionPane.showInputMessageDialog("Enter your MiddleName"); JOptionPane.showInputMessageDialog("Enter your LastName"); 

But I want to have three input fields in one message.

+6
java swing joptionpane
source share
5 answers

Create a JPanel (add it named inputPanel) with three JtextFields input and do the following:

 if (JOptionPane.YES_OPTION == JOptionPane.showconfirmDialog( parentComponent, inputPanel, "Enter your data", JOptionPane.YES_NO_OPTION) { // retrieve data from the JTextFields and do things } else { // User close the dialog, do things... or not } 
+3
source share

You cannot do this with JOptionPane . Create a JDialog and add three JTextField instead. A JDialog blocks the caller when setVisible(true) , so it’s easy to create a dialog that waits for user input before it returns.

+1
source share

showInputMessageDialog and his brothers are easy ways to crack a simple "standard" dialog. For more complex dialogs, I'm sure you will have to subclass JDialog or such.

+1
source share

Since Telcontar suggested adding Swing components (like JPanel) to the options panel. Thus, it is easy to use automatic button creation, rather than doing it from scratch, creating your own JDialog.

However, there is one small problem. The focus will be on the first button, and not on the first component of the panel. To work around this problem, you can try the solution presented in the dialog box .

+1
source share

Here you can find a standard Java tutorial example:

Click here to open a sample java file.

There is only one text field in the example, but the example is clear enough for you to extend it.

-one
source share

All Articles