3

I have a dialog, which have four buttons say New, Save, Delete, Cancel. Now each of these need to perform their action. So I have defined a separate class which implements an ActionListener. I have used this class to perform each of the button action.

public class MyClass implements ActionListener {
  public void actionPerformed(ActionEvent e) { 
   if(e.getActionCommand().toString() == "OK") {
        // Code
   } else if( .. ){

   }
  }
}

What I have done is, I defined an inner class which I used to do the same functionality. But what I was not getting is, is it best practice to write a separate class or is it best to use inner class. I was suggested to write in a public class so tomorrow some one can use this class to perform their action. So I have the following questions,

  1. If the functionality is not called by any object (which I can't say) then I can write it in inner class? OR

  2. Is it a good practice always to write inner class which performs the actions of that dialog?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Amarnath
  • 8,736
  • 10
  • 54
  • 81
  • For a reusable action called by a button, I would say: use the `Action` class instead of an `ActionListener`. See also [this answer](http://stackoverflow.com/a/12463553/1076463) – Robin Oct 16 '12 at 14:54

3 Answers3

6

When you talk about defining an inner class for a GUI listener, I think immediately of using anonymous classes to do the job:

newButton.addActionListener(
    new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            // do the work for a button press
        }
    });      

// similarly for save, cancel, delete buttons:
saveButton.addActionListener(
    new ActionListener()
    {
        // ...

You'll often see this, and I often use this in my own code.

Which you use depends on the structure of your code, for instance:

  • how much work each listener needs to do
  • how much code is shared between the handlers

If each handler is short (e.g. just one method call, with all the real work handled within another class method), I'll use anonymous classes this way. But it does look goofy: unfortunately java requires us to define an object to employ a callback method.

It's also a matter of personal preference and coding style.

However, I'd rarely make a new top-level class for a GUI handler as in your example: even if I separate out the class so that I use the same class for each button, I'd define the class within the class file controlling the buttons, either as a non-public top-level class, or as an inner (non-anonymous) class. If the complexity of a GUI callback grows to the point that it deserves a separate class, I'd start looking for places to refactor.

pb2q
  • 58,613
  • 19
  • 146
  • 147
6

There is no general answer to these questions. If the code in actionPerformed() is one line, writing a whole class file is usually overkill.

If the code is more complex, it might be suitable to reuse but as the code grows, it also gets more specific (so you can't reuse it anymore).

Try to follow this approach:

  • Build your application from simple blocks that are independent of each other.
  • Configure the application by writing code that connects the blocks.

So in your case, you could have helper methods which do the work. Wrap them in Swing Actions; this allows you to use them in all kinds of buttons and menus.

Try to move as much work as possible into the helpers so that the actions become really simple. If in doubt, ask yourself: Is this code part of the work? Does everyone need to do this?

If yes, then the code should go into a work/helper class.

If it's things like UI related checks (visible/enabled), conversion (string to number), validation, etc., it should go into the action.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
2
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319