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(ActionEven t 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.
public class TMenu extends JFrame{
public JMenu aboutMenu = new JMenu("About");
...
//following is inner class
class ItemHandler implements ActionListener{
public void actionPerformed(ActionEven
{
//aboutMenu
// if (aboutMenu.isSelect()) TDialog.displayAboutDialog
JMenu menu = new JMenu();
menu = aboutMenu;
...
It shows abotMenu is a undefined variable at inner class when compiled.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(ActionEven t 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(ActionEven t e)
{
//aboutMenu
if (test1.this.aboutMenu.isSe lected()) 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?
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(ActionEven
{
//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(ActionEven
{
//aboutMenu
if (test1.this.aboutMenu.isSe
}
}
}
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?
ASKER