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 method keyPressed of the KeyListener class just like I have done with the mouseClicked, and ActionPerformed I really don’t want to override keyTyped and keyReleased, just the keyPressed on its own. The interaction takes place in another class called VIEW
Java does not support multiple inheritance, so you can’t extend multiple classes, you can’t have something like:
You can implement multiple interfaces however, but it seems that you want to avoid that.
Now, the solution to this kind of problem is always the same, use composition over inheritance. You could easily have two inner-classes: one that extends
KeyAdapterand the otherMouseAdapter. Then later on, when you need to add listeners, you uses fields of your class instead ofthis.Something like this: