• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dialog Input Question

 
Mohsen Gamshad
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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!
 
Mohsen Gamshad
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic