Its generally not wise to assume something is wrong with something widely released in use by tons of people, the correct question is what am I doing wrong.
The answer to that question appears to be you call getText() before any text could have been set by the user. Your input dialog pops up and immediately the code keeps executing and calls get getText() and pops up the message dialog.
A simple fix for this test code is to make the new_dialog modal. In the long run though your code needs to be event driven and respond to user events.
Joined: Nov 29, 2006
Obviously I'm doing something wrong. Question needs rephrasing. Dialog cannot be modal. Doesn't work with Pro/E.
You've added a JLabel to a JPanel, but otherwise, you have nothing but a bunch of disassembled components; and in particular, the JTextField you create is never added to any container, let alone a visible one, so it's impossible for the user to ever enter any text into it -- it never appears on the screen. And I don't know why you've created a JFrame at all.
Here is some code which uses JOptionPane to get a String from the user:
String newName = JOptionPane.showInputDialog(null, "Enter new name");
Thanks for the reply to my question. If only I could use JOptionPane.showInputDialog()! I would make my life so much easier! The trouble is, I am implementing Java using J-Link with Pro/Engineer CAD software. The nice showInputDialog window always appears behind the Pro/Engineer window whether I user toFront() or a number of other methods I've tried.
My java components may all be disassembled; however, the dialog box does appear on the screen and the user can enter text and hit OK. The dialog box then closes. The trouble is, I can't seem to retrieve the string from the text field. I think I need to use an action listener or document listener.
Originally posted by Jerry Goldsmith: If only I could use JOptionPane.showInputDialog()! I would make my life so much easier! The trouble is, I am implementing Java using J-Link with Pro/Engineer CAD software. The nice showInputDialog window always appears behind the Pro/Engineer window whether I user toFront() or a number of other methods I've tried.
That typically means that the JOptionPane has the wrong parent component. I don't know Pro/Engineer at all, but if you can get a reference to it's main window and use that as the parent component, I would expect it to work.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus