I need to make multiple action listeners for 4 radio buttons and a regular button. They all need to call different methods from another class

I currently have

Import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CardGameCH15 extends JFrame
{
		

	public CardGameCH15()
	{
		super("Card Game");
		add(new CardTable());
	
	}

	public static void main(String[] args)
	{
		CardGameCH15 frame = new CardGameCH15();	
		frame.setSize(500,200);
		frame.setTitle("Card Game");
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	
	}

	class CardTable extends JPanel implements ActionListener
	{
	  	String card;
		ImageIcon cardIcon;
		JButton dealButton = new JButton("Deal 5");
		CardDeck deck;
		JLabel card1 = new JLabel("");
		JLabel card2 = new JLabel("");
		JLabel card3 = new JLabel("");
		JLabel card4 = new JLabel("");
		JLabel card5 = new JLabel("");
	   JRadioButton jrb1 = new JRadioButton("1 Deck");	
	   JRadioButton jrb2 = new JRadioButton("2 Decks");
		JRadioButton jrb3 = new JRadioButton("3 Decks");
		JRadioButton jrb4 = new JRadioButton("4 Decks");	
	
				
	

	
		public CardTable()
		{
				setLayout(new FlowLayout());
		
			deck = new CardDeck(1);
			dealHand();
			add(card1);
			add(card2);
			add(card3);
			add(card4);
			add(card5);
			add(dealButton);
			add(jrb1);
			add(jrb2);
			add(jrb3);
			add(jrb4);	
			
}
				dealButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
				dealHand();
				}
				});

THE ABOVE ACTION LISTENER IS NOT WORKING, IS THERE ANOTHER WAY OF WRITING MULTIPLE ACTION LISTENERS OR DID I MAKE A SYNTAX ERROR

private void dealHand()
		{
			String str;

			if((str = deck.getCard()) != null)
				card1.setIcon(new ImageIcon("image/card/" + str));
			else
				return;
			if((str = deck.getCard()) != null)
				card2.setIcon(new ImageIcon("image/card/" + str));
			else
				return;
			if((str = deck.getCard()) != null)
				card3.setIcon(new ImageIcon("image/card/" + str));
			else
				return;
			if((str = deck.getCard()) != null)
				card4.setIcon(new ImageIcon("image/card/" + str));
			else
				return;
			if((str = deck.getCard()) != null)
				card5.setIcon(new ImageIcon("image/card/" + str));
			else
				return;
		}
	}
}

I would appreciate any help...Thanks in advance

Recommended Answers

All 6 Replies

Put a quick print statement at the start of your dealHand method so you can see if it's the listener call that's failing or the dealHand method itself. I haven't checked all the brackets and semicolons, but the way you are defining the listener is the normal way to do it.

its not the deal hand method because when I just had a single action listener done something like

dealbutton.addActionListener(this);.....
.....
public void actionPerformed(ActionEvent e)
		{
			dealHand();
		}

everything worked fine but now that I need multiple action listeners for my radio buttons im havin issues

Well, looking at that brace on line 64, your code to add the listener is not in a method at all. So you've left out some other code in your post or that piece above won't even compile.

Getting closer!! now im getting this error

CardGameCH15.java:27: CardGameCH15.CardTable is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
class CardTable extends JPanel implements ActionListener
^

That's because you moved the code into the anonymous listener. If you aren't going to define that method on your class then remove the "implements ActionListener" from your class declaration.

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.