I'm trying to build a simple Java Swing application using the MVC architecture pattern. What I've done is create the user interface components (as private) in my views, and have public methods that return the components. These methods are then called by the controllers, through which I can write methods for event/action listeners. Below is a sample example:
View:
private JButton btnAdd;
public JButton getBtnAdd(){
return btnAdd;
}
Control:
myGuiFrame gui = new myGuiFrame();
//on add button clicked
gui.getBtnAdd().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//calls to model
}
});
Is this implementation correct?
If so, then I'm having a problem with FocusListeners. When I create a FocusListener in my view, the focusLost and focusGained methods are created within the view.
private FocusListener l;
someComponent.addFocusListener(l);
l = new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
}
@Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
}
};
I want all the event handlers to be in my controllers. My question is ... is there a way I can call/declare the focusLost and focusGained methods from my controller? I tried to define the FocusListener as public so that I can define it in my controller:
view:
public FocusListener l;
public someComponentType someComponent;
controller:
gui.l = new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
}
@Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
gui.someComponent.addFocusListener(gui.l);
}
};
This however does not work.
Is it possible to handle FocusEvents from the controller?
EDIT:
Gosh, my bad. Didn't quite understand what Robin was all about. I was too fixated on having the FocusListener explicitly defined somewhere. A simple:
gui.getTextFieldEmployeeCode().addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
}
@Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
System.out.println("YES!!!");
}
});
in the controller would work just fine in the manner I planned to do it, though I quite like how nIcE cOw's gone about it. Just out of curiosity, is there a standard or widely accepted manner of implementing MVC on Swing Apps?
As far as I understood, the way you doing is this. Better still, I prefer the Anonymous Classes, they respect the concept of Encapsulation. Here try your hands on this code, see what you can grasp with this :
Another approach, if you feel irritated by
getters/setters
and sending references around, is to define one inner class as follows (A Simple workaround for the previous example) :