Java – many methods that cover many classes in one class

public class EventController extends MouseAdapter implements ActionListener {
public class EventController extends MouseAdapter implements ActionListener {

    private EventModel model;
    private EventView  view;
    String tableClick;
    Events events;    

    /** Constructor */
   public EventController(EventModel myModel,EventView myView){
       model = myModel;
       view = myView;     
   }

   public void setUpListeners() {
       this.view.addEventButton.addActionListener(this); 
       this.view.addEventMenuItem.addActionListener(this);
       this.view.editEventMenuItem.addActionListener(this);
       this.view.tableEvent.addMouseListener(this);                
   }

   @Override
   public void actionPerformed(ActionEvent e){
       Object button = e.getSource();
       if(button==this.view.addEventButton) {
           setEventDetails();          
       }                
   }

   @Override
   public void mouseClicked(java.awt.event.MouseEvent event) {
      int rowSelected = view.tableEvent.getSelectedRow();
       //blahblahblah
      view.changeDisplay(events);
    }

How do I override the keypressed method of the keylistener class, just like I use mouseclicked, and actionperformed? I really don't want to override keytype D and keyreleased, just my own keypressed The interaction occurs in another class called view

Solution

Java does not support multiple inheritance, so you can't extend multiple classes. You can't have such things:

class EventController extends MouseAdapter,KeyAdapter

However, you can implement multiple interfaces, but it seems that you want to avoid this

Now, the solution to this problem is always the same, using composition rather than inheritance You can easily have two inner classes: one extends keyadapter and the other extends keyadapter Then, when you need to add a listener, you will use the field of the class instead of this field

Something like this:

import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseAdapter;

public class EventController {
    private EventModel model;
    private EventView  view;
    String tableClick;
    Events events;

    private MouseAdapter mouseListener = new MouseAdapter() {

        @Override
        public void mouseClicked(java.awt.event.MouseEvent event) {
          int rowSelected = view.tableEvent.getSelectedRow();
           //blahblahblah
          view.changeDisplay(events);
        }
    };

    private KeyAdapter keyAdapter = new KeyAdapter() {
        public void keyTyped(java.awt.event.KeyEvent e) {
            // Perform here whatever is needed
            // You also have access to your enclosing instance EventController.this and its methods
        }
    };

    private ActionListener actionListener = new ActionListener() {@Override
               public void actionPerformed(ActionEvent e){
           Object button = e.getSource();
           if(button==this.view.addEventButton) {
               setEventDetails();          
           }

       }



    /** Constructor */
   public EventController(EventModel myModel,EventView myView){
       model = myModel;
       view = myView;     
    }

   public void setUpListeners() {
       this.view.addEventButton.addActionListener(actionListener); 
       this.view.addEventMenuItem.addActionListener(actionListener);
       this.view.editEventMenuItem.addActionListener(actionListener);
       this.view.tableEvent.addMouseListener(mouseListener);
       // Here you can also add the keyadapter to your views
   }


 }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>