Chea Indian
Chea Indian

Reputation: 697

Is there a simple way to implement a dialog box with two input lines? (java)

I'm making an xml editor as one of our projects in class, and for adding an attribute, I am currently doing this:

String name = JOptionPane.showInputDialog(this, "Enter the attribute name: ", "Name", JOptionPane.INFORMATION_MESSAGE);
String value = JOptionPane.showInputDialog(this, "Enter the attribute value: ", "Value", JOptionPane.INFORMATION_MESSAGE);

Is there a better way to have just a single dialog box with both those things on it? I looked at some examples but I am having trouble implementing/understanding them. While I am able to correctly add attributes with the current method, it is kind of silly to have two input boxes.

Please let me know if there is some simple solution to this. Thanks

Upvotes: 4

Views: 1522

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285440

Yes, you can create a JPanel that holds two JTextFields and pop that into a JOtionPane.showConfirmDialog(....), and then when it returns, if the user presses the OK button, extract the text from the JTextFields.

For instance, please check out my code in this answer

Upvotes: 10

Caffeinated
Caffeinated

Reputation: 12484

So you can but you'll need to use the version that takes an Object( thanks hovercraft) , look at the Java 6 JOptionPage , there are variants that take more than one!

Upvotes: 5

Related Questions