如何访问Listener的源代码

时间:2015-01-11 13:18:55

标签: java swing listener

我试图使用一个通用的ActionListener来为使用Java和Swing调用它的comboBox做些什么。

有没有办法访问ActionEvent的源并将其作为一个comboBox来访问它的方法,如getSelectedIndex()或setSelectedIndex()? 这也适用于FocusListener吗?

谢谢。

编辑:

示例:

private class CbModalidadFocusListener extends FocusAdapter {
    @Override
    public void focusGained(FocusEvent e) {
        selection = someComboBox.getSelectedIndex();
    }
}

期望的行为类似于:

selection = e.getSource()。getSelectedIndex();

EDIT2:

解答:

正如Yole所说,需要演员阵容:

((JComboBox)e.getSource())。getSelectedIndex()

1 个答案:

答案 0 :(得分:0)

所有AWT事件都有一个getSource()方法,可以返回您需要的内容:http://docs.oracle.com/javase/7/docs/api/java/util/EventObject.html#getSource%28%29

相关问题