File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JDialog input - How? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JDialog input - How?" Watch "JDialog input - How?" New topic

JDialog input - How?

Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

Hello again.
I am starting with Swing now. Since I have little experience (for now) with it, I got stuck with the problem.
Here is the deal:
I have a simple main frame for application, and user can choose menu item (let's say it's Login) which shows simple dialog (for providing username and password). I implemented dialog as extension to JDialog class.
So, the thing is - How can I retrieve user input from that dialog in main frame?

I don't know is this against the rules, but I will try to provide some simple C# code of how to do this, which I couldn't make in Java:

Is there any way it can be done this simple in Java?
In advance - Yes, I am opened to new opinions and I am interesting to learn new things. I was just wondering can it be done like this in Java.


The quieter you are, the more you are able to hear.
pete stein

Joined: Feb 23, 2007
Posts: 1561
1) Show the JDialog as a modal dialog. Give it public getter (but not setter) methods to get the user name String and the password char array.
2) When you call setVisible(true) on the JDialog all code in the calling portion of the program is halted while waiting for the dialog to be dealt with. Once the dialog has been finished, code will resume on the line immediately after the setVisible(true).
3) It is here that you call getName() and getPassword() on your dialog object from the calling portion of the program.

Simplified version:
1) Make your dialog a JPanel without OK/Cancel buttons.
2) Display a JOptionPane.showConfirmDialog passing the above JPanel as the second parameter (the JFrame as the first).
3) If the return int is JOptionPane.OK_OPTION, then extract the user name and password same as above.
Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

I think second option is better.
I agree. Here's the link:
subject: JDialog input - How?
It's not a secret anymore!