[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

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.
0
iry
Asked:
iry
1 Solution
 
iryAuthor Commented:
Edited text of question.
0
 
vivexpCommented:
Hi,

Visit the site for detailed info
http://www.javaworld.com/javaworld/javatips/jw-javatip75.html
Try out...
0
 
JodCommented:
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?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now