Overriding many methods of many classes in one class
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
KeyListener class method the same way I did with mouseClicked
and ActionPerformed
, I really don't want to override keyTyped
and keyReleased
, just my keyPressed
own. Interaction takes place in another class named VIEW
source to share
Java does not support multiple inheritance, so you cannot extend multiple classes, you cannot have something like:
class EventController extends MouseAdapter, KeyAdapter
However, you can implement multiple interfaces, but it looks like you want to avoid this.
Now the solution to this problem is always the same, use composition over inheritance. You can easily have two inner classes, one that extends KeyAdapter
and the other MouseAdapter
. Then, when you need to add listeners, you use the fields of your class instead this
.
Something like that:
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
}
}
source to share
You cannot, because you are limited to single inheritance in Java. So you will have to implement the KeyListener and provide an implementation for these two methods (which does nothing).
The best design would be a separation of duties and a class for mouse events, another for action events, and a third for key events. Classes can be implemented as anonymous inner classes.
source to share
Swing provides adapter classes to support overriding methods without having to execute all of them in the Listener class.
If you only need to implement some of the KeyListener methods, you must use the KeyAdapter class .
source to share
You cannot do this.
A class can only extend from another class (multiple inheritance is prohibited in Java).
A class can implement multiple interfaces, but since the interface does not provide an implementation of the method you must provide it (or declare the class abstract
or even yourself interface
).
Since you are already extending MouseAdapter
you need to implement ActionListener
me
source to share
Instead of providing listeners directly to the EventController, do it in inner classes. For example, one inner class can extend MouseAdapter and call the EventController methods to do the actual work. Another inner class can extend KeyAdapter and call other methods of EventController.
Attach instances of the inner classes as listeners, not the EventController itself.
source to share