views:

46

answers:

2

I'm developing the Java Swing application. I'm quite new to Java, so got some questions. I have a modal window with some set of controls (text fields, buttons etc).

I want to handle click on the button in the parent window. I think the most efficient and accurate way is first to handle it in modal window, then raise some another event from the model form and handle it in the parent form.

Is this approach right and what are the best practices on doing that?

Thanks for your help!

A: 

In general, the dialog that contains the button should handle the button click.

However, maybe you can use a JOptionPane. It is designed to return which button was clicked and then you can do custom processing based on the clicked button. Check out the section from the Swing tutorial on How to Make Dialogs for some examples. Also not that you can add a panel to an option pane. In this case you may find the Dialog Focus tip usefull.

camickr
A: 

I suppose what you want is for the action (or an action listener) of a button in the parent window to process a mouse click on a button (or anything) in the modal dialog.

There are infinite ways to do that. You can pass the action to the modal dialog, pass the button and call doClick(), pass an implementation of an interface that can redirect mouse click (or anything), etc.

Or if instead you want to click the actual button in the parent window when the modal dialog is up, look up the definition of modal.

Geoffrey Zheng