javaswingjmenujmenuitem

JCheckBoxMenuItem cannot be added to Menu


The src code of JCheckBoxMenuItem has the following class head:

public class CheckboxMenuItem extends MenuItem implements ItemSelectable, Accessible

So when a class extends another one it means that it should also inherit its type. Or not?

My problem is that I can't add the JCheckBoxMenuItem to a JMenu (it needs a MenuItem to be added).

The following code does not work for me:

private void initMenu()
{
    menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    mnFile = new JMenu("File");
    menuBar.add(mnFile);

    mnAudio = new JMenu("Audio");
    menuBar.add(mnAudio);

    mnitmQuit = new JMenuItem("Quit");
    mnFile.add(mnitmQuit);


    rmnitmNoice = new CheckboxMenuItem("Noice");
    // Eclipse gives error Message below *
    mnAudio.add(rmnitmNoice);

    rmnitmNuke = new JRadioButtonMenuItem("Nuke");
    // Same here
    mnAudio.add(rmnitmNuke);
}

The method add(JMenuItem) in the type JMenu is not applicable for the arguments (CheckboxMenuItem)

I m quite sure that I used this one before and had no problems with it. But since I started to use Marven I get strange behaviour sometimes (other example: @Override does not work for Methods, that implement interfaces anymore)

Anyone with the same issue or the solution?


Solution

  • As VGR explained in his comment: The mistake was trying to add a CheckboxMenuItem instead of a JCheckBoxMenuItem.