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- Multiple inputs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JDialog- Multiple inputs" Watch "JDialog- Multiple inputs" New topic

JDialog- Multiple inputs

prav ford

Joined: Apr 28, 2004
Posts: 16
I want to have a dialog box which has 5 textbox's and 2 combo. I need to get these values into my main frame. Can anybody help me out

Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
You need to create a class with one instance variable for each value you want to return (7 in your case). Create the form and add "Accept" and "Cancel" buttons. When the user chooses "Accept", then transfer the values from the form (JTextField, JComboBox) to the instance variables and shut down the dialog form. When the user chooses "Cancel", then set the instance variables to null and shut down the dialog form. Make sure you have a "getter" method for each of the instance variables.

The calling class will instantiate and show the dialog [make sure it is modal]. Then simply call the seven "getter" methods to get the values from the dialog.

Here's a very simple class that returns a single value.

The constructor forces the dialog to be modal, meaning that the calling frame will be blocked until the user chooses ACCEPT or CANCEL, which is what you want. I like to leave out the "setVisible(true)" method call so the calling object can decide when to show the dialog.

The calling method would include these lines:

If the return value is "null", then you know the user selected "Cancel"; otherwise you'll get back the value they entered.

There are other ways of doing this, but this should help get you started.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
prav ford

Joined: Apr 28, 2004
Posts: 16
Thanks a lot guys. You solved my problem.
John Deal

Joined: Sep 07, 2006
Posts: 20
Thanks for posting this code. I had the same problem and this helped a lot.

SCJP 1.4, SCJD 1.5
I agree. Here's the link:
subject: JDialog- Multiple inputs
It's not a secret anymore!