How to avoid nested ActionListeners?

In my program, I want the user:

  • select / open a database (e.g. Access) yourself
  • select table from database
  • select column (s) from table

In my code, I have a class that does something like this:

mntmOpenDatabase.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //open the database //display tables as buttons tableButton.addActionListener(new ActionListener() { // select a table public void actionPerformed(ActionEvent e) { //display the columns of the table selected as buttons colButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {// add to the list of columns to be exported } 

And this leads to a very large block of code. Is there a simpler and easier way to do this?

+5
source share
2 answers

The solution is to refactor:

  • Create a separate and separately verifiable class for the code to open the database.
  • And a separate and separately checked class for displaying this data.
  • In ActionListener, either instantiate these classes, or interact with them if they already exist.
  • Learn the basic principles of the MVC (Model-View-Control) design pattern and use them. You do not need to be slavish to them, and the lord knows that there are many options, but their general guidelines should be followed at a minimum.
  • Strive to make your GUI or browse as mute as possible . He knows how to display his data, he has capabilities that allow the control to update its display, and he knows how to notify the control when the user interacts with it, and what about it.
  • Side 1 recommendation: ensure that all database interactions are performed in the background thread.
  • Lateral recommendation 2: be convinced that almost all interactions with Swing are carried out on Swing EDT (stream of transmission of events).

Please look at this similar but more complete question and answer: What is the best way to avoid writing bloated GUI code? . The answers are as good as mine, and I want me to be able to vote for them a million times.

For example, your code above may be as simple as:

 mntmOpenDatabase.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { control.openDatabase(); } } 
+10
source

In your example, you create an instance and add a new listener to each ActionEvent. Indeed, you must configure it once. Something like that:

 public class OpenDataBaseListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e){ //your event handling here } } public class TableButtonListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e){ //your logic } } 

etc...

And when you create your listeners, you have to register them once:

 mntmOpenDatabase.addActionListener(new OpenDataBaseListener()); tableButton.addActionListener(new TableButtonListener()); colButton.addActionListener(new ColButtonListener()); 
+1
source

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


All Articles