aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help with passing events between components. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help with passing events between components." Watch "Help with passing events between components." New topic
Author

Help with passing events between components.

Chris Waguespack
Ranch Hand

Joined: Jul 26, 2007
Posts: 36

I have a JFrame, and when a button is clicked, it needs to pop up a custom JDialog. In this JDialog, a user inputs data and clicks ok to pass the data back to the JFrame, so it can be saved off. I can do this using Flex/Actionscript 3.0, but can't figure out how to get it working in Java. Below are code fragments from AS3.0.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
perhaps the simplest way is to set the dialog's modality to true, then when you
create the dialog you also call the method that will return the data. the data
will not be returned until the dialog disposes (or visible false).

simple demo
(click 'get name', enter something into the textfield, click 'ok'

Chris Waguespack
Ranch Hand

Joined: Jul 26, 2007
Posts: 36

Thank you, but I don't believe this will work (because there will be a cancel button that calls dispose(); ). Is there any good way to do this using events and listeners?

Thank you,
Chris
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
another way

Chris Waguespack
Ranch Hand

Joined: Jul 26, 2007
Posts: 36

Hmm, I think this would work, but I would like to have the dialog window not care what called it. I've been looking into using addActionListener() and dispatchEvent() and have tried created my own custom event/listener but can't seem to figure out exactly what to do. Any suggestions?

Thank you,
Chris
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> but I would like to have the dialog window not care what called it.

that's the first example I posted
Chris Waguespack
Ranch Hand

Joined: Jul 26, 2007
Posts: 36

> that's the first example I posted

The text would be set to the value of the input every time, and I need it to be able to not do anything if the user clicks a 'Cancel' button. I would also prefer this to be done using listeners, as I need it as reusable as possible.

Thank you,
Chris

P.S. Thanks for the help so far.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
here's the first example modified for cancel/ok (with empty textfield)/ok



I don't fully understand the requirements - perhaps all you need is a
JOptionPane.showInputDialog(...)
Chris Waguespack
Ranch Hand

Joined: Jul 26, 2007
Posts: 36

Thanks for the help. I finally decided to go with an MVC approach and that seems to be working like I need it to.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with passing events between components.