Determine the actions of buttons in the inner class Vs; determine the actions of buttons in the open class in a swing

I have a dialog in which there are four buttons: "New", "Save", "Delete", "Cancel". Now each of them must perform its action. Therefore, I defined a separate class that implements ActionListener . I used this class to perform each button action.

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

What I did, I defined an inner class that I used to perform the same function. But what I did not get is best to write a separate class, or it is better to use an inner class. I was asked to write in a public class, so tomorrow someone can use this class to perform their actions. Therefore, I have the following questions:

  • If the functionality is not called by any object (which I cannot say), can I write it in the inner class? OR

  • Is it always good to write an inner class that performs the actions of this dialog?

+3
source share
3 answers

There is no general answer to these questions. If the code in actionPerformed() is one line, the record of the entire class file is usually overflowed.

If the code is more complex, it can be used for reuse, but as the code grows, it also becomes more specific (so you can no longer reuse it).

Try following this approach:

  • Create an application from simple blocks that are independent of each other.
  • Customize your application by writing code that connects the blocks.

So, in your case, you may have helper methods that do the job. Wrap them in Swing Action s ; this allows you to use them in all types of buttons and menus.

Try to work with assistants as much as possible so that the actions become very simple. If in doubt, ask yourself: is this code part of the job? Is there any need to do this?

If so, then the code should go to the work / helper class.

If it looks like checks related to the UI (visible / activated), conversion (string to number), check, etc., it should go into action.

+6
source

When you talk about defining an inner class for a GUI listener, I think immediately 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 often see this, and I often use this in my own code.

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

  • how much work should each listener do
  • how many codes are shared between handlers

If each handler is short (for example, only one method call, while all the real work is done in another class method), I will use anonymous classes. But it looks silly: unfortunately, java requires us to define an object to use the callback method.

It is also a matter of personal preference and coding style.

However, I rarely made a new top-level class for the GUI handler, as in your example: even if I separate the class to use one class for each button, I would define the class inside the class file that controls the buttons, either as a non-public top-level class, or as an internal (not anonymous) class. If the complexity of the GUI callback grows to such an extent that it deserves a separate class, I would start looking for places to refactor.

+6
source
+2
source

Source: https://habr.com/ru/post/1480560/


All Articles