Hey

I've been writing this hangman code for last week in college. and nearly got it finished but am after running into a problem. i want to run two different button handlers but can not figure out how to?.
one for the letter a to z which i have working but can not figure out how to get the RadioButton to work from the JMenu.Which i'll later use for setting the difficulty.
I'm new to the JMenu and more than one button grouping with an ActionListener so kind of confused.

I've attached my code so far and would appreasiate any help in pointing me in the right direction or tip or anything.

Thanks :

Declare JRadioButtonMenuItem as follow

String[] difLevel = {"Easy", "Normal", Hard"};
JRadioButtonMenuItem[] difficulty;

Initialization somewhere in code with adding ActionListener

difficulty[] = JRadioButtonMenuItem[difLevel.length];
for(int i = 0; i < difficulty.length; i++)
{
	difficulty[i] = new JRadioButtonMenuItem(difLevel[i]);

	//add to file menu that you already declared in your code
	file.add(difficulty[i]);

	difficulty[i].addActionListener(itemHandler);
}

Listener will be something like this

private class ItemHandler implements ActionListener
{
	public void actionPerformed(ActionEvent ae)
	{
		for( int i=0; i < difficulty.length; i++)
		{
			if(event.getSource() == difficulty[i])
			{
				/*
				 * YOUR DIFFICULTY LEVEL SETUP CODE HERE
				 */
			}
		}
	}
}
commented: Great help.! Much Appreciated.Much taken from post +2

Thanks alot for that.

it's a great help spent last hour and a bit at it and i can see that it will work but can't figure out how to addActionListener(ItemHandler) to work.

not sure what way i'm mint to declare it. as i think this is the problem. is there a different Group for radio buttons?

ButtonGroup ItemHandler = new ButtonGroup();

This is the error message

C:\Documents and Settings\Stephen\My Documents\College\NetBeansProjects\hangManFromScrach\src\HangMan\HangMan.java:271: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (javax.swing.ButtonGroup)

Any idea about how to fix this problem

Thanks for your help

Globally declared

String[] difLevel ={"Easy", "Normal", "Hard"};
 JRadioButtonMenuItem[] difficulty;
 ButtonGroup radioGroup = new ButtonGroup();
 ButtonHandler ItemHandler = new ButtonHandler();

Setting up the JMenu

public JMenuBar setMenuBar()
        {
		JMenu file = new JMenu("File");
	
                difficulty = new JRadioButtonMenuItem[difLevel.length];
                for(int i = 0; i < difficulty.length; i++)
                {
                    difficulty[i] = new JRadioButtonMenuItem(difLevel[i]);
                    
                    file.add(difficulty[i]);
                    radioGroup.add(difficulty[i]);
                    // my problem is with the ItemHandler Handler);not sure how to set this up
                    difficulty[i].addActionListener(ItemHandler);
                }

Catching the ActionListener

private class ItemHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent ae)
            {
               
		for( int i=0; i < difficulty.length; i++)
		{
			if(ae.getSource().equals("Easy"))
			{
				System.out.println("this is easy");
			}
                        else
                          if(ae.getSource().equals("Normal"))
			  {
			   	System.out.println("this is medium");
			  }
                        else
                          if(ae.getSource().equals("Hard"))
			  {
				System.out.println("this is hard");
			  }
               }
            }
        }

My problem is i'm getting this code not sure what to do with it. If anyone can point out where i'm going wrong, it would be a great help!!

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JRadioButtonMenuItem cannot be cast to javax.swing.JButton

Sorted it.

It was my own fault when you look at a problem for too long the obvious answer is overlooked on mycase.:$ the problem is i was calling the wrong term for my ActionListener. this was what i was doing

ButtonHandler ItemHandler = new ButtonHandler(); 
ButtonHandler handler = new ButtonHandler(); 

private class ItemHandler implements ActionListener
{
  //this is my radio
}

private class ButtonHandler implements ActionListener
{ 
  //this was Jbutton
}

But should have called the object ButtonHandler which i did do earler only forgot the other object for dealing with the a-z buttons was called

ButtonHandler handler = new ButtonHandler();

Which set up a conflict and tryed to apply JMenuRadio to a JButton which does not work and i could not figure out. until it jumped out at me .... just renamed it.

RadioHandler ItemHandler = new RadioHandler (); 
ButtonHandler ItemHandler = new ButtonHandler (); 

private class RadioHandler implements ActionListener
{
  //what ever
}
private class ButtonHandler implements ActionListener
{ 
  //this was Jbutton
}

THANK ALOT TO peter_budo :)

Your info was invaluable to me... Just took me a while to see my own mistake:$

Sorry couldn't respond faster but I spent most of day writing literature survey on "Alternative ways of controlling computing devices". Not to much joy, but I'm nearly done with it

Keep up good work, you just proved you capable sort things on your own

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.