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 :

Attachments
package HangMan; 

/**
 *
 * @author Stephen
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;
import javax.swing.JOptionPane;
 
 
public class HangMan extends JFrame 
{
	Container c;
	JButton buttonSet[];
	JTextField textField;
	String display,pickWord,pickHint,show,choice;
	JLabel label,wordlabel, hintlabel;
	JPanel buttonPanel,labelPanel,picpanel,     displaymenu;
        JRadioButtonMenuItem easy,medium,hard;
        char[] blank;
        char[] displayWord;
        int life = 7;
        ButtonGroup group = new ButtonGroup();
        
        Icon life1 = new ImageIcon("stock.gif");
	Icon life2 = new ImageIcon("head.gif");
        Icon life3 = new ImageIcon("body.gif");
	Icon life4 = new ImageIcon("1stArm.gif");
	Icon life5 = new ImageIcon("2ndArm.gif");
	Icon life6 = new ImageIcon("1stLeg.gif");
	Icon life7 = new ImageIcon("2ndLeg.gif");
	Icon life8 = new ImageIcon("Finish.gif");
	//Icon life8 = new ImageIcon("fireworks.jpg");
        
 ButtonHandler handler = new ButtonHandler();
	public HangMan()
	{

            
                setMenuBar();
		c = getContentPane();
		c.setBackground(Color.white);
		setSize(200,200);
		setLocation(250,100);
 
                
                label = new JLabel();
                label.setIcon(life1);
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setLayout(new BorderLayout());
                label.setBackground(Color.magenta);
		label.setPreferredSize(new Dimension(300,300));
                picpanel = new JPanel();
                picpanel.add(label);
                picpanel.setVisible(true);
               
		pickWord();
                blankWord();
		wordlabel = new JLabel(show);
		wordlabel.setHorizontalAlignment(SwingConstants.CENTER);
		wordlabel.setLayout(new BorderLayout());
		wordlabel.setBackground(Color.CYAN);
		wordlabel.setPreferredSize(new Dimension(300,40));
		labelPanel = new JPanel();
                labelPanel.add(wordlabel);
                GridLayout lblayout = new GridLayout(0,1);
                labelPanel.setLayout(lblayout);
	
		textField = new JTextField();
		textField.setHorizontalAlignment(JTextField.CENTER); 
		textField.setFont(new Font("Helvetica", Font.PLAIN, 20));
		textField.setBackground(Color.blue);
		textField.setPreferredSize(new Dimension(300,40));
		labelPanel.add(textField);
		
		hintlabel = new JLabel(pickHint);
		hintlabel.setHorizontalAlignment(SwingConstants.CENTER);
		hintlabel.setLayout(new BorderLayout());
		hintlabel.setBackground(Color.yellow);
		hintlabel.setPreferredSize(new Dimension(300,40));
		labelPanel.add(hintlabel);
		
 
		
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout( 4,3));
		buttonSet = new JButton[26];
 
		for (int i=65; i <= 90;i++)
		{
			buttonSet[i-65] = new JButton("" + ((char)i));
			buttonPanel.add(buttonSet[i-65]);
			buttonSet[i-65].addActionListener(handler);
			buttonSet[i-65].setFont(new Font ("Helvetica",Font.BOLD, 18));
			buttonSet[i-65].setForeground(Color.gray);
		} 
 
                c.add(picpanel,BorderLayout.NORTH);
		c.add(labelPanel,BorderLayout.CENTER);
		c.add(buttonPanel,BorderLayout.SOUTH);
  
		
		display = new String();	
 
	}
        
            private class ButtonGroup implements ActionListener
            {
             public void itemStateChanged(ItemEvent e)
                    {
                        JRadioButton radio = (JRadioButton) (e.getSource());
                        
                        if (radio.equals(medium))
                        {
                            reset();
                        }
                    }
            }
        
        
        
        
		private class ButtonHandler implements ActionListener
		{ 
                    public void actionPerformed(ActionEvent e)
			{
				JButton button = (JButton) e.getSource();
				 choice = button.getText();
                                 
                                 
                                 
                                 
                            if(life > 0)
                            {
				if(e.getSource() == button)
				{
                                        display += button.getText();
					textField.setText(display);
                                        boolean isThere = false;
                                        
                                        //check guess and compares to word. Replaces any correctly guessed letter
                                        for (int start = 0; start < pickWord.length(); start++)
                                        {
                                            char[] check = choice.toCharArray();
                                            
                                            if (check[0] == displayWord[start])
                                            {
                                                blank[start]  = displayWord[start];
                                                isThere = true;
                                            }
                                         }
                                        
                                          show = new String(blank);
                                          wordlabel.setText(show);
                                         
                                          
                                          if (isThere != true)
                                          {
                                            life--;
                                          }
                                          setLives();
                                          if(show.equals(pickWord))
                                          {
                                                JOptionPane.showMessageDialog(null, "WELL DONE!! YOU'VE DONE IT","",JOptionPane.INFORMATION_MESSAGE);
                                                reset();
                                          }
                                          if (life < 1)
                                          {
                                               JOptionPane.showMessageDialog(null, "Sorry NO lives left","",JOptionPane.ERROR_MESSAGE);
                                               reset();
                                          }
                                }
                            }   
                       
                            else
                            {
                                JOptionPane.showMessageDialog(null, "Sorry NO lives left","",JOptionPane.ERROR_MESSAGE);
                                reset();
                            }     
                        }
                }
 
        public void pickWord()
        { 
            
            
    //        switch (JRadioButtonMenuItem)
      //      {   
          //          case "easy":
            ///            break;
               //     case "medium":
                 //       break;
                   // case "hard":
                     //   break;
        //    }           
            int look;
            Random generator = new Random();
            look = generator.nextInt(8) + 1;
            pickWord = wordlibrary.WORD_LIST[look];
            pickHint = wordlibrary.HINT_WORD_LIST[look];
        }
        
        public void blankWord()
        {
            displayWord =  pickWord.toCharArray();
            
            show = pickWord.replaceAll("[a-zA-Z0-9]","_");
            blank = show.toCharArray();
        }
        
        
        public void setLives()
        {
            if(life == 7)
                label.setIcon(life1);
            if(life == 6)
                label.setIcon(life2);
            if(life == 5)
                label.setIcon(life3);
            if(life == 4)
                label.setIcon(life4);
            if(life == 3)
                label.setIcon(life5);
            if(life == 2)
                label.setIcon(life6);
            if(life == 1)
                label.setIcon(life7);
            if(life == 0)
                label.setIcon(life8);
        }
        
        public void reset()
        {
            life = 7;
            pickWord();
            blankWord();
            wordlabel.setText(show);
            hintlabel.setText(pickHint);
            display = "";
            textField.setText("");
        }
        
        public JMenuBar setMenuBar()
        {
		
		JMenu file = new JMenu("File");
		//file.setMnemonic('F');
		
                

                easy = new JRadioButtonMenuItem("Easy",true);
                easy.isSelected();
                easy.addActionListener(group);
                //group.add(easy);
                file.add(easy);
                 
                

                medium = new JRadioButtonMenuItem("Normal",false);
                medium.addActionListener(group);
                //group.add(medium);
                file.add(medium);
                
                hard = new JRadioButtonMenuItem("Hard",false);
                hard.addActionListener(group);
               // group.add(hard);
                file.add(hard);
                
                file.addSeparator();
                JMenuItem exitItem = new JMenuItem("Exit");
		file.add(exitItem);
                
                		
		JMenuBar bar = new JMenuBar();
		setJMenuBar(bar);
		bar.add(file);
                
                return bar;
            
        }
        
        
        
        
        
	public static void main(String args[])
	{
		JFrame frame = new HangMan();
		frame.setVisible(true);
		frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
 
}
package HangMan;

/**
 *
 * @author Stephen
 */
/**
 * Write a description of class WordLibrary here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public final class wordlibrary 
{

    protected static final String[] WORD_LIST = {
        "MANCHESTER UNITED",
        "GONE IN 60 SECONDS",
        "CRASH",
        "CHESS",
        "NEED FOR SPEED",
        "DAVID BECKHAM",
        "DUBLIN",
        "SYDNEY",
        "TOKYO"
                
            
    };

    protected static final String[] HINT_WORD_LIST = {
        "SPORT",
        "FILM",
        "FILM",
        "SPORT",
        "GAME",
        "SPORTS PERSON",
        "PLACE",
        "PLACE",
        "PLACE"
    };

   

}

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
				 */
			}
		}
	}
}
Comments
Great help.! Much Appreciated.Much taken from post

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

Attachments
package HangMan; 

/**
 *
 * @author Stephen
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;
import javax.swing.JOptionPane;
 
 
public class HangMan extends JFrame 
{
	Container c;
	JButton buttonSet[];
	JTextField textField;
	String display,pickWord,pickHint,show,choice;
	JLabel label,wordlabel, hintlabel;
	JPanel buttonPanel,labelPanel,picpanel,     displaymenu;
        JRadioButtonMenuItem easy,medium,hard;
        char[] blank;
        char[] displayWord;
        int life = 7;
        ButtonGroup ItemHandler = new ButtonGroup();
        
        Icon life1 = new ImageIcon("stock.gif");
	Icon life2 = new ImageIcon("head.gif");
        Icon life3 = new ImageIcon("body.gif");
	Icon life4 = new ImageIcon("1stArm.gif");
	Icon life5 = new ImageIcon("2ndArm.gif");
	Icon life6 = new ImageIcon("1stLeg.gif");
	Icon life7 = new ImageIcon("2ndLeg.gif");
	Icon life8 = new ImageIcon("Finish.gif");
	Icon life9 = new ImageIcon("fireworks.jpg");
        
        
        //test area
        String[] difLevel ={"Easy", "Normal", "Hard"};
        JRadioButtonMenuItem[] difficulty;
        
 ButtonHandler handler = new ButtonHandler();
	public HangMan()
	{

            
                setMenuBar();
		c = getContentPane();
		c.setBackground(Color.white);
		setSize(200,200);
		setLocation(250,100);
 
                
                label = new JLabel();
                label.setIcon(life1);
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setLayout(new BorderLayout());
                label.setBackground(Color.magenta);
		label.setPreferredSize(new Dimension(300,300));
                picpanel = new JPanel();
                picpanel.add(label);
                picpanel.setVisible(true);
               
		pickWord();
                blankWord();
		wordlabel = new JLabel(show);
		wordlabel.setHorizontalAlignment(SwingConstants.CENTER);
		wordlabel.setLayout(new BorderLayout());
		wordlabel.setBackground(Color.CYAN);
		wordlabel.setPreferredSize(new Dimension(300,40));
		labelPanel = new JPanel();
                labelPanel.add(wordlabel);
                GridLayout lblayout = new GridLayout(0,1);
                labelPanel.setLayout(lblayout);
	
		textField = new JTextField();
		textField.setHorizontalAlignment(JTextField.CENTER); 
		textField.setFont(new Font("Helvetica", Font.PLAIN, 20));
		textField.setBackground(Color.blue);
		textField.setPreferredSize(new Dimension(300,40));
		labelPanel.add(textField);
		
		hintlabel = new JLabel(pickHint);
		hintlabel.setHorizontalAlignment(SwingConstants.CENTER);
		hintlabel.setLayout(new BorderLayout());
		hintlabel.setBackground(Color.yellow);
		hintlabel.setPreferredSize(new Dimension(300,40));
		labelPanel.add(hintlabel);
		
 
		displayButtons();
		
 
                c.add(picpanel,BorderLayout.NORTH);
		c.add(labelPanel,BorderLayout.CENTER);
		c.add(buttonPanel,BorderLayout.SOUTH);
  
		
		display = new String();	
 
	}
        
        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");
			  }
               }
            }
        }
        
        
		private class ButtonHandler implements ActionListener
		{ 
                    public void actionPerformed(ActionEvent e)
			{
				JButton button = (JButton) e.getSource();
				 choice = button.getText();
                                 
                            if(life > 0)
                            {
				if(e.getSource() == button)
				{
                                        display += button.getText();
					textField.setText(display);
                                        boolean isThere = false;
                                        
                                        //check guess and compares to word. Replaces any correctly guessed letter
                                        for (int start = 0; start < pickWord.length(); start++)
                                        {
                                            char[] check = choice.toCharArray();
                                            
                                            if (check[0] == displayWord[start])
                                            {
                                                blank[start]  = displayWord[start];
                                                isThere = true;
                                            }
                                         }
                                        
                                          show = new String(blank);
                                          wordlabel.setText(show);
                                         
                                          
                                          if (isThere != true)
                                          {
                                            life--;
                                          }
                                          setLives();
                                          if(show.equals(pickWord))
                                          {
                                                JOptionPane.showMessageDialog(null, "WELL DONE!! YOU'VE DONE IT","",JOptionPane.INFORMATION_MESSAGE);
                                                reset();
                                          }
                                          if (life < 1)
                                          {
                                               JOptionPane.showMessageDialog(null, "Sorry NO lives left","",JOptionPane.ERROR_MESSAGE);
                                               reset();
                                          }
                                }
                            }   
                       
                            else
                            {
                                JOptionPane.showMessageDialog(null, "Sorry NO lives left","",JOptionPane.ERROR_MESSAGE);
                                reset();
                            }     
                        }
                }
 
        public void pickWord()
        { 
            int look;
            Random generator = new Random();
            look = generator.nextInt(8) + 1;
            pickWord = wordlibrary.WORD_LIST[look];
            pickHint = wordlibrary.HINT_WORD_LIST[look];
        }
        
        public void blankWord()
        {
            displayWord =  pickWord.toCharArray();
            
            show = pickWord.replaceAll("[a-zA-Z0-9]","_");
            blank = show.toCharArray();
        }
        
        
        public void setLives()
        {
            if(life == 7)
                label.setIcon(life1);
            if(life == 6)
                label.setIcon(life2);
            if(life == 5)
                label.setIcon(life3);
            if(life == 4)
                label.setIcon(life4);
            if(life == 3)
                label.setIcon(life5);
            if(life == 2)
                label.setIcon(life6);
            if(life == 1)
                label.setIcon(life7);
            if(life == 0)
                label.setIcon(life8);
        }
        
        public void reset()
        {
            life = 7;
            pickWord();
            blankWord();
            wordlabel.setText(show);
            hintlabel.setText(pickHint);
            display = "";
            textField.setText("");
        }
        
        public void displayButtons()
        {
                buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout( 4,3));
		buttonSet = new JButton[26];
 
		for (int i=65; i <= 90;i++)
		{
			buttonSet[i-65] = new JButton("" + ((char)i));
			buttonPanel.add(buttonSet[i-65]);
			buttonSet[i-65].addActionListener(handler);
			buttonSet[i-65].setFont(new Font ("Helvetica",Font.BOLD, 18));
			buttonSet[i-65].setForeground(Color.gray);
		}
        }
        
        
        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]);
                    
                    //also this adds the radio buttons to the file but does not group the radio
                    //buttons so that only on can be chosen. i.e.
                    //RadioButton = new ButtonGroup---PLACED OUTSIDE LOOP
                    //RadioButton.add(difficulty[i]);
                    file.add(difficulty[i]);
                    
                    // my problem is with the ItemHandler Handler);not sure how to set this up
                    difficulty[i].addActionListener(ItemHandler);
                }
                
 
		JMenuBar bar = new JMenuBar();
		setJMenuBar(bar);
		bar.add(file);
                return bar;
            
        }
        
	public static void main(String args[])
	{
		JFrame frame = new HangMan();
		frame.setVisible(true);
		frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
 
}
package HangMan;

/**
 *
 * @author Stephen
 */
/**
 * Write a description of class WordLibrary here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public final class wordlibrary 
{

    protected static final String[] WORD_LIST = {
        "MANCHESTER UNITED",
        "GONE IN 60 SECONDS",
        "CRASH",
        "CHESS",
        "NEED FOR SPEED",
        "DAVID BECKHAM",
        "DUBLIN",
        "SYDNEY",
        "TOKYO"
                
            
    };

    protected static final String[] HINT_WORD_LIST = {
        "SPORT",
        "FILM",
        "FILM",
        "SPORT",
        "GAME",
        "SPORTS PERSON",
        "PLACE",
        "PLACE",
        "PLACE"
    };

   

}

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

This question has already been answered. Start a new discussion instead.