3

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Cong Hui
  • 633
  • 1
  • 13
  • 25
  • `class foo() {` ??? ... If you want us to take your question seriously, please post real code, not bad code like the above. But yes, bar is visible from within your mouseClicked method. That's not a problem with this code. – Hovercraft Full Of Eels Apr 01 '12 at 15:00
  • possible duplicate of [keyword for the outer class from an anonymous inner class?](http://stackoverflow.com/questions/56974/keyword-for-the-outer-class-from-an-anonymous-inner-class) – Mat Apr 01 '12 at 15:02
  • I fixed the code to make to more clear, Thanks for the headsup – Cong Hui Apr 01 '12 at 15:06

3 Answers3

3

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thank you for the info, just a quick question, I should add the MenuListner to the JMenu or JMenuItem when the one I am trying to listen to is the mouse click on JMenuItem. – Cong Hui Apr 01 '12 at 15:28
  • for JButton's JComponents there is [ButtonModel](http://stackoverflow.com/questions/5751311/creating-a-custom-button-in-java-with-jbutton/5755124#5755124), returns interesting methods for testing, notifying and listening – mKorbel Apr 01 '12 at 15:33
  • Thanks, I am always wondering what those **Model** are, could you give me a pointer of a good read on this? Thank you – Cong Hui Apr 01 '12 at 15:38
  • @Clint Hui every JComponents have got Models, only for JTextComponents is there Document – mKorbel Apr 01 '12 at 15:39
3

To answer your question, you need to understand the basics, as to how the JVM use to work. When the classes are compiled which contain inner classes, the byte code which gets generated does not actually implement inner classes as a class within a class.

WHY THE ERROR : The local variable was accessed from an inner class, needs to declare it final

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenu;
import javax.swing.JPanel;

public class foo extends JPanel
{  
  public foo()
  {
    final JMenu edit = new JMenu();
    edit.getItem(0).addMouseListener(new MouseAdapter(){ 
    @Override
        public void mouseClicked(MouseEvent e) 
        {
            if (e.getClickCount() == 1) {
                edit.getItem(0).setEnabled(true);
            }
        } 
    });
  }
}

When you compile your this program, two files will be created, Foo.class and Foo$1.class. So now your problem comes, since the Second class i.e. foo$1.class doesn't know that Variable edit is present inside the First class i.e. foo.class.

So how to solve this problem ? What JVM does, is that, It makes a requirement for the developer to make the variable of an outer class to be declared as final.

Now this being done, now JVM quietly places a hidden variable with the name val$edit inside the 2nd compiled class file, here is the output as got from javap

Ouput for foo.class

C:\Mine\JAVA\J2SE\folder>javap foo.class
Compiled from "foo.java"
public class foo extends javax.swing.JPanel {
  public foo();
}

Now since, edit is local to the constructor, hence the output as above.

C:\Mine\JAVA\J2SE\folder>javap foo$1.class
Compiled from "foo.java"
class foo$1 extends java.awt.event.MouseAdapter {
  final javax.swing.JMenu val$edit;
  final foo this$0;
  foo$1(foo, javax.swing.JMenu);
  public void mouseClicked(java.awt.event.MouseEvent);
}

The Variable val$edit is assigned the same value which has been assigned to edit since now the compiler knows that the value cannot be changed as it has been declared final and hence it works this time.

Now what if I change the edit Variable from being Local to being Instance. Now the object of the class knows everything about this variable edit, if it gets changed. So changing the above program likewise we get :

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenu;
import javax.swing.JPanel;

public class foo extends JPanel
{  
    JMenu edit = new JMenu();

    public foo()
    {   
        edit.getItem(0).addMouseListener(new MouseAdapter(){ 
        @Override
            public void mouseClicked(MouseEvent e) 
            {
            if (e.getClickCount() == 1) {
                    edit.getItem(0).setEnabled(true);
                }
            } 
        });
    }
}

Here in this case, we are not suppose to declare and define it as being final, because in this case since the Variable being Local to the whole class, the Variable is send to the Inner Class along with the Object Reference i.e. this

C:\Mine\JAVA\J2SE\folder>javap foo.class
Compiled from "foo.java"
public class foo extends javax.swing.JPanel {
  javax.swing.JMenu edit;
  public foo();
}

Here is how the Variable is send in this case i.e. this$0 :

C:\Mine\JAVA\J2SE\folder>javap foo$1.class
Compiled from "foo.java"
class foo$1 extends java.awt.event.MouseAdapter {
  final foo this$0;
  foo$1(foo);
  public void mouseClicked(java.awt.event.MouseEvent);
}

Seems like that the interpretation, how this situation works, according to me. Just now I found this wonderful explanation on the internet regarding Mystery of Accessibility in Local Inner Classes, might be this will help you understand the situation in a much better way :-)

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
1

Your anonymous inner class does exist in the scope of it's parent object, as you expect. Scope is not the problem.

As the error message suggests, the anonymous inner class can only access it's parent's "edit" member if that member is declared final.

So, change

  private JMenu edit = new JMenu();

to

  private final JMenu edit = new JMenu();

and it should work.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67