Java
19
Вклад в тег
class MyView extends JFrame {
...
JButton button = new JButton();
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Controller.getInstance().fireEvent(Events.OPEN_WINDOW);
}
});
...
}
void fireEvent(Event event) {
for (Listener listener: listeners) {
listener.onEvent(event);
}
}
Controller implements Listener {
...
void onEvent(Event event) {
if ( event.equals(Events.OPEN_WINDOW) ) {
// put here code for initializing and opening the window
// NOTE: new window also can be listener of specific events
}
}
...
}
class MyView extends JFrame {
...
JButton button = new JButton();
button.addOnTextChangeListener(new TextChangeListener() {
public void textChanged(TextChange e) {
Controller.getInstance().fireEvent(new Event(Events.TEXT_CHANGE, e.getNewText()));
}
});
...
}
Controller implements Listener {
...
void onEvent(Event event) {
if ( event.equals(Events.TEXT_CHANGE) ) {
// find and update the module
getTextContainer().setText(event.getData());
}
}
...
}