my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes static import of ActionCommands. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "static import of ActionCommands." Watch "static import of ActionCommands." New topic
Author

static import of ActionCommands.

Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

I have a class that extends JFrame and I want to have another top level class that extends ActionListener to handle the events for this.
Is importing the ActionCommands via a static import the best way to do this?

In JFrame I have

In the ActionListener class I have
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38020
    
  22
Timmy Ryan wrote: . . . Is importing the ActionCommands via a static import the best way to do this? . . .
No. Not at all. It is a dreadful way to do it. You want different classes for different actions.
Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

Thanks for the response, what way should I split it up?
Currently I have one class that extends JFrame and another that extends ActionListener, to handle the events.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

A common approach is to give each control gets its own (anonymous) listener.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Guru Charan Anand,
Your post was moved to a new topic.
Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

From reading up a bit more it seems that the Action events are handled in the same class with inner classes. So point taken there.

One final question. Say I have a screen with with a JFrame which contains 3 JPanels.
Would I create this 3 JPanels in the same class as the JFrame(Every example I have looked at has it done this way) or in there own separate classes.
Could you give a reason as to why they are all in the same class?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38020
    
  22
You don't need private classes. I have written about this before, here, with links to other useful thread.
 
wood burning stoves
 
subject: static import of ActionCommands.
 
Similar Threads
Jwindow
Menus and replacing the prompts fr JFrame
Help with Combo box and action listener
Probelm with JFrame Minimization.
Swing - Parent window goes back on closing the model dialog