开发者

How do I access the source of an ActionEvent when the ActionListener is located in a different class?

I can't get my head round this one. I've tried to adhere to the MVC pattern for the first time and now have difficulties accessing the source of an ActionEvent because the ActionListener is located in a different class. But let the code do the talking...

In the "view":

// ControlForms.java

...

private JPanel createSearchPanel() throws SQLException {

...

comboBoxCode = new JComboBox(); // Field comboBoxCode -> JComboBox()
    SwingUtilities.invokeLater(new Runnable() {  
        public void run() {  
            AutoCompleteSupport<Object> support = AutoCompleteSupport.install(
comboBoxCode, GlazedLists.eventListOf(jnlCodeArray));
        }  
    });  // Auto-Complete comboBox from GlazedLists

...

public void setComboListener(ComboListener comboListener) {
    comboBoxCode.addActionListener(comboListener);
}

...

}

Then, in what I term the controller, I have two different classes:

// Controller.java

    public MyController() throws SQLException {
...
    addListeners();
}

...

    private void addListeners(){
    View view = getView();
    getView().getControlForm().setComboListener(new ComboListener());

}

and

public clas开发者_StackOverflows ComboListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
    System.out.println("ComboBox listened to! e = " + e.toString());
}
}

Now, e obviously doesn't give the name of the variable (which at the moment I wish it would), so I cannot if test for e.getSource().

My question is thus: is there either a) a way to query (via if for example) the source of e, or b) a less complicated way to get to the variable name?

Many, many thanks in advance for your insights and tips!


Why do you need the name of the variable? Why can't you do the event handling like this

public class ComboListener implements ActionListener 
{
    public void actionPerformed(ActionEvent e) 
    {
        JComboBox source = (JComboBox)e.getSource();

        //do processing here
    }
}

I'd think that if you need to do processing according the variable name, obviously you need different listeners for different combo boxes.


Generally, there are only two situations in which you should use a listener like that: a) you're going to handle a certain event the same way for a bunch of objects, or b) you're only going to use the listener for one object. In the latter case, I'd prefer handling the event locally anyway.

That said, the direct answer to your question is: you shouldn't have to check inside your ActionListener implementation to see whether the appropriate object is the source of the event; you should simply only add the ActionListener to that one object.

One final note: without knowing the specifics of your architecture... generally, MVC will treat all event handling as part of the View (it reduces coupling) and the View will pass commands or method calls or your own events (i.e., not Swing's) to the Controller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜