4

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Chea Indian
  • 677
  • 3
  • 9
  • 16
  • Here is one more [example](http://stackoverflow.com/a/10309277/1057230), do watch `actionPerformed(...)` method for `button (JButton)`, inside that the `JOptionPane` uses a new `JPanel` instead of it's default, or in simple terms watch Line Number 81, 82, 83 and 84 :-) – nIcE cOw Jul 16 '12 at 02:43

2 Answers2

10

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

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
5

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!

Caffeinated
  • 11,982
  • 40
  • 122
  • 216
  • 1
    This is not true. The Object parameter of a JOptionPane will accept any Swing component, including a JPanel that holds a simple or even very complex arrangement of componets. – Hovercraft Full Of Eels Jul 16 '12 at 00:54
  • 1
    Thank you as well. I love this community :) you all are so helpful to one another, and to me! – Chea Indian Jul 16 '12 at 01:01