Reputation: 849
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
Upvotes: 3
Views: 2429
Reputation: 109
Search for Composite and Facade pattern, to reuse code.
class SilverBullet implements Interface1, Interface2 {
Concrete1 c1;
Concrete2 c2;
void someMethod() {
c1.someMethod();
}
}
Upvotes: 0
Reputation: 26185
Instead of providing the listeners directly in the EventController, do it in inner classes. For example, one inner class can extend MouseAdapter, and call EventController methods to do the actual work. A different inner class can extend KeyAdapter, and call other EventController methods.
Attach instances of the inner classes as listeners, rather than the EventController itself.
Upvotes: 3
Reputation: 47617
Java does not support multiple inheritance, so you can't extend multiple classes, you can't have something like:
class EventController extends MouseAdapter, KeyAdapter
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 KeyAdapter
and the other MouseAdapter
. Then later on, when you need to add listeners, you uses fields of your class instead of this
.
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
}
}
Upvotes: 7
Reputation: 24895
You can't do this way.
A class can only extend from another class (multiple inheritance is forbidden in Java).
A class can implement multiple interfaces, but since the interface provides no implementation of the method you must provide it (or declare the class abstract
, or even an interface
itself).
Since you already extend MouseAdapter
, you have to implement the ActionListener
me
Upvotes: 3
Reputation: 45283
Swing provides Adapter classes for helping override methods without needing to do all of them in a Listener class.
If you need to implements only some of the methods of KeyListener, you should use the KeyAdapter class.
Upvotes: 3
Reputation: 692073
You can't, because you're limited to single inheritance in Java. So you'll have to implement KeyListener, and provide an implementation for these two methods (which does nothing).
A better design would be to separate the responsibilities, and have a class for the mouse events, another one for the action events, and a third one for the key events. The classes could be implemented as anonymous inner classes.
Upvotes: 9