I am trying to build a Swing user interface based on MVC and have a couple of questions on the topic. The best way of asking my questions would be with the help of a simple example.
Let's say that we have a JDialog with one JButton and 3 JTextFields. When that button is pressed I want this JDialog to close and another one to open that requires the data from the 3 JTextFields.
The easy way of doing this (going for just snippets of code, no need to annoy you too much) is:
myButton.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent evt){
firstFrame.setVisible(false);
secondFrame.setData(jTextField1.getText(),jTextField2.getText(),jTextField3.getText());//just sending the data to the new window, would be this or something similar
secondFrame.setVisible(true);
}});
Or something like that.
First Question: If I don't use an anonymous inner class but decide to use a seperate class for my Listener how would I pass the data from the 3 JTextFields to my mouseListener class? Are there any alternatives other than keeping a reference of the view in the Listener class?
Carrying on, given the MVC pattern:
Second Question: I guess that it makes sense the listener (controller according to MVC) to call another window without the model getting involved, that it's job I guess. But the data that need to pass from one window to the next one (the data from the 3JTextFields) shouldn't those go through the model ? Like having the first window save that data in the model, and then when the second one needs them request them from the model.
Third Question: I was considering of using the Observer/Observable pattern. How would that be used in this example to open the last window? I mean since (I think) the Controller is the class that opens the second window, would that mean that the Listener would have to be Observed and the second window would have to be an Observer so that the Listener would say
notifyObservers("openSecondWindow");
and then the second window would see that and open itself?
Finally: I heard that a PropertyChangeListener could work as well, and that it is sometimes preffered over the Observer/Observable Pattern. What are your thoughts on this, especially regarding my example.
I am confused..
Thank you for your time.