I am wonder what the scope of the MouseAdapter is in this case.
class foo extends JPanel()
{
private JMenu edit = new JMenu();
public foo()
{
this.edit.getItem(0).addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
edit.getItem(0).setEnabled(true);
}
}
});
}
}
I thought the MouseAdapter has access to the variable edit because the newly declared MouseAdapter is an inner class of the class foo. However, it can't find the variable edit. If I explicitly declare an inner class and implements, say, the MouseAdapter interface or whatever, it can detect the variable edit from within it.So my question is what the scope the new MouseAdpater() has? Besides, does anyone know a good read on this? Thank you very much. By the way, the error I got was the local variable was accessed from an inner class, needs to declare it final
1) edit.getItem(0)
returns fist JMenuItem
if exist, otherwise returns IllegalArgumentException
2) this.edit.getItem(0)
, not class that returns members
3) edit.getItem(0).addMouseListener(new MouseAdapter(){
is contraproductive, becuase JMenu
, JMenuItem
has implemented MouseEvents
correctly, for better workaround you have to look at ButtonModel
4) there no reason for scope of the mouse adapter
5) for listening of events from JMenu
(not JMenuItem
) look at MenuListener