Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDialog input - How?

 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Cheers!
Kemal
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think second option is better.
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic