Link to home
Start Free TrialLog in
Avatar of iry
iry

asked on

inner class question

i am using jdk1.2.2, codes like:
public class TMenu extends JFrame{
  public JMenu aboutMenu = new JMenu("About");
    ...
//following is inner class
class ItemHandler implements ActionListener{
 
  public void actionPerformed(ActionEvent e)
  {
   //aboutMenu
//    if (aboutMenu.isSelect()) TDialog.displayAboutDialog();
    JMenu menu = new JMenu();
    menu = aboutMenu;
...
It shows abotMenu is a undefined variable at inner class when compiled.
Avatar of iry
iry

ASKER

Edited text of question.
ASKER CERTIFIED SOLUTION
Avatar of vivexp
vivexp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There is no isSelect() method in JMenu but there is an isSelected(). With this in mind, this compiles just fine...

import javax.swing.*;
import java.awt.event.*;

public class test1 extends JFrame{
  public JMenu aboutMenu = new JMenu("About");

//  ...

  //following is inner class
  class ItemHandler implements ActionListener{
    public void actionPerformed(ActionEvent e)
    {
      //aboutMenu
      if (aboutMenu.isSelected()) System.out.println("Done...");
    }
  }
}


If you want to be doubly safe then use...


import javax.swing.*;
import java.awt.event.*;

public class test1 extends JFrame{
  public JMenu aboutMenu = new JMenu("About");

//  ...

  //following is inner class
  class ItemHandler implements ActionListener{
    public void actionPerformed(ActionEvent e)
    {
      //aboutMenu
      if (test1.this.aboutMenu.isSelected()) System.out.println("Done...");
    }
  }
}


When you use test1.this you get the outer class object so test1.this.aboutMenu always gets you the reference to the aboutMenu object in the outer class.

Do you need to know more about inner classes?