I have a question concerning MVC Swing java application.
Lets say, Entity e
is simple class without any logic – only attributes and getters, setters, equals, hashCode, toString (or maybe compareTo). It will represent Model in MVC.
Than we have MainWindow
(as the View in MVC).
Is it okay to use e.getSomething();
, e.setSomething(someValue);
or even sort/iterate some collection of Element
s in MainWindow
? And therefore do some GUI rendering and actions in component listeners anonymous classes (I guess listener implementation cannot be in Controller, because it's "view dependent" - HTML doesn't have listeners)?
I did something like this in MainWindow
:
...
final Element el = Controller.getInstance().getSomeElement();
JButton save = new JButton();
JTextField field = new JTextField(el.getSomething());
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
el.setSomething(field.getText());
Controller.getInstance().persist(); //let controller know some Element has changed and needs to be saved
}
});
...
How to change this piece of code for it to comply with MVC? Thanks.