Best way to organize action handler?

C

C-man

Say, I have a simple Jframe set up, in which I have a main panel, then
embedded in this main panel I have a bunch more panels, and so on and so
fourth. and each panel will be it's own class and so it's own file. What is
the best way to add an action listener? Should a guy have an action listener
for each panel to keep it more object oriented? How does say the main panel
access say a text field from a panel that is nested deep in other panels?
Should there just be a main action handler and all global components? I
think I read somewhere that there is a way to have an action listener just
pass the message to its parent panel. Is this correct?

Thanks
Allot
 
J

Jim Cobban

C-man said:
Say, I have a simple Jframe set up, in which I have a main panel, then
embedded in this main panel I have a bunch more panels, and so on and so
fourth. and each panel will be it's own class and so it's own file. What is
the best way to add an action listener? Should a guy have an action listener
for each panel to keep it more object oriented? How does say the main panel
access say a text field from a panel that is nested deep in other panels?
Should there just be a main action handler and all global components? I
think I read somewhere that there is a way to have an action listener just
pass the message to its parent panel. Is this correct?

The way that I have found easiest to organize is to define each dialog as a
class that implements ActionListener. For each component in the dialog that
can generate an ActionEvent I make the dialog instance the listener and put
a client property with a numeric value. Then I can use a switch statement
to distinguish between the components quickly while still permitting each
bit of action handling code to easily access all of the resources of the
dialog.

The main class should only access the information within a sub-panel through
get and set methods. The main class should not need to know anything about
the implementation of the lower level dialogs.

I suspect from your description that you should be considering implementing
a JDesktopPane for your application and then displaying dialogs on it using
either JOptionPane.showInternalXxxxxDialog() methods or creating your own
JInternalFrames.

For example:

private final static int ADD = 4;
....
class MyDialog
implements ActionListener {
....
JButton addButton = new JButton("Add");
addButton.putClientProperty("action", new Integer(ADD));
addButton.addActionListener(this);
....
public void actionPerformed(ActionEvent ev) {
JComponent source = (JComponent)ev.getSource();
Integer action = (Integer)source.getClientProperty("action");
switch(action.intValue())
{
....
case ADD:
{ // add a field definition
performAddAction();
break;
} // add a field definition
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top