N
N
Nikita Kolosov2012-04-23 14:57:47
Java
Nikita Kolosov, 2012-04-23 14:57:47

An example JAVA application using MVC?

In general, I don’t quite understand how to correctly split the project into Model, View and Controller. If someone would throw a simple example, I would be very grateful. (View is written using Swing). The problem itself arises from the fact that there are several windows and it is not entirely clear how to interact with them. And it is not clear what to shove in main.
In general, such an example:
When the application starts, a window opens. There are 2 buttons in the window. By pressing one, some text is displayed, by pressing the other, a new window opens (the old one is killed).
The second window also has 2 buttons. One displays the text, the other displays the old window, and closes the new one.
It’s clear how to write, I don’t understand exactly the architecture.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
serso, 2012-04-23
@Anexroid

For example:

class MyView extends JFrame {
    ...
    JButton button = new JButton();
    button.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            Controller.getInstance().fireEvent(Events.OPEN_WINDOW);
        }
    });
    ...
}

In controller:
void fireEvent(Event event) {
    for (Listener listener: listeners) {
        listener.onEvent(event);
    }
}

The controller itself can be an event listener:
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
        }
    }
...
}

And working with the model:
 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());
        }
    }
...
}

Naturally, TextChangeListener was invented to simplify understanding, I don’t know if there is such a thing in Swing

S
serso, 2012-04-23
@serso

It's simple - the controller coordinates the actions. In your case, I would make a class that can register listeners and manage events.
Windows with buttons are views. They register for some events in the controller, and call the controller themselves to send the event.
The models in your example are not. You can add a model, for example, an object that will be stored in the controller and contain the text from the first window. This object, accordingly, must be updated by the controller on the event of the window with the text field, and the text in the window with the text field must be set from this object by the controller.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question