Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static import of ActionCommands.

 
Timmy Ryan
Ranch Hand
Posts: 63
IntelliJ IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
IntelliJ IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A common approach is to give each control gets its own (anonymous) listener.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guru Charan Anand,
Your post was moved to a new topic.
 
Timmy Ryan
Ranch Hand
Posts: 63
IntelliJ IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need private classes. I have written about this before, here, with links to other useful thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic