GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Dialog Input Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Dialog Input Question" Watch "Dialog Input Question" New topic
Author

Dialog Input Question

Mohsen Gamshad
Greenhorn

Joined: Aug 27, 2002
Posts: 12
I'm tyring to implement a dialog box that has a few text areas for user to type data, and I have found a couple of ways to do it: 1) using the JDialog way or 2) laying out the text fields and button, i.e. crude JDialog imitator.
1) Firstly, is there an alternative method?
2) I noticed with method #2, the user has to press the Enter key or else the action event object is not fired.
3) So, how do all those dialog boxes work, where you can just enter a value in a text field and without either pressing the Enter key or changing focus via the Tab key, when pressing the apps main "go" button the values are read?
4) Related to question #1, what's the preferred way of getting user data?
Thanks,
Mohsen
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8915
    
    8

Originally posted by Mohsen:

1) Firstly, is there an alternative method?

JOptionPane gives a quick way to display simple dialogs, but if you have more than one text field, I think you have the best options.

2) I noticed with method #2, the user has to press the Enter key or else the action event object is not fired.
3) So, how do all those dialog boxes work, where you can just enter a value in a text field and without either pressing the Enter key or changing focus via the Tab key, when pressing the apps main "go" button the values are read?

I can't quite picture what you are talking about here. Do you mean, in question #2, when you construct your own dialog you aren't getting an ActionEvent fired from the button on the dialog? The question there would be: is any listener registered with that button?
In #3, you have seen a program where you get a dialog to fill in values then you press a button on the main program to exit the dialog and process the dialog's fields? That's just weird.

4) Related to question #1, what's the preferred way of getting user data?

What Works Best For You, Your Program And Your Users. That's why Java gives you several options. One size does not fit all.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

"Mohsen" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

Sorry, but I don't think I understand all of your question... are you asking about the difference between using JOptionPane and hand-coding a JDialog in the first part?

The later part sounds like you might want to set some kind of default button activated by a keypress, but I'm not sure if that is what you meant by "the apps main "go" button".

Thanks! and welcome to the JavaRanch!


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Mohsen Gamshad
Greenhorn

Joined: Aug 27, 2002
Posts: 12
Originally posted by Nathan Pruett:
"Mohsen" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.


Done.


Sorry, but I don't think I understand all of your question... are you asking about the difference between using JOptionPane and hand-coding a JDialog in the first part?

The later part sounds like you might want to set some kind of default button activated by a keypress, but I'm not sure if that is what you meant by "the apps main "go" button".


I guess I wasn't clear enough. What I wanted to emulate was something I have seen in other applications. Which is, the dialog box comes up with some text fields and an OK/Cancel buttons. User inputs data in the text fields without necessarily pressing the Enter key. User then presses the app's OK button and the data in the text fields are communicated to the app.
Now, when I tried to do the above with my own text fields I could only get the data iff the Enter key was pressed. So, how can I do what I described in the example app?
Thanks,
Mohsen

p.s. to the first reply: yes I do have all the listeners registered.
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
I am new to the Swing components. But correct me if I am wrong, but can't you do something along the lines of attaching an event to your "OK" button to pull the data from the TextField component entered by the user.
For example:

Craig
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Thanks for the name change

Here's an example of using a JOptionPane and a JDialog to output to a JTextArea in the main app.... hopefully it is helpful.

chris czinder
Greenhorn

Joined: Oct 02, 2002
Posts: 28
Well if you don't want to have to rely on the user hitting enter, to fire an ActionEvent, you could register a MouseListener and fire a MouseEvent when the user clicks on the button.
Mohsen Gamshad
Greenhorn

Joined: Aug 27, 2002
Posts: 12
Thanks Nate. I'll give it a try.
To Chris' point: How would a mouse listener help in this case?
Thanks,
Mohsen
Mohsen Gamshad
Greenhorn

Joined: Aug 27, 2002
Posts: 12
Originally posted by cj jack:
I am new to the Swing components. But correct me if I am wrong, but can't you do something along the lines of attaching an event to your "OK" button to pull the data from the TextField component entered by the user.
For example:

Craig

So, yes and I do this. But for text fields if the user hadn't actually pressed the Enter key, the catching of the OK button would still not get me any of the text field data. And that's what I was after.
Thanks,
Mohsen
Mohsen Gamshad
Greenhorn

Joined: Aug 27, 2002
Posts: 12
Nate,
The JDialog version worked exactly like I was discussing. Thanks, now I have to take a closer look at the code. Thanks very much.
Mohsen
 
GeeCON Prague 2014
 
subject: Dialog Input Question