Multiple Action Listeners

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 12
Reputation: geek_till_itMHZ is an unknown quantity at this point 
Solved Threads: 0
geek_till_itMHZ geek_till_itMHZ is offline Offline
Newbie Poster

Multiple Action Listeners

 
0
  #1
Nov 10th, 2009
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

  1. Import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class CardGameCH15 extends JFrame
  6. {
  7.  
  8.  
  9. public CardGameCH15()
  10. {
  11. super("Card Game");
  12. add(new CardTable());
  13.  
  14. }
  15.  
  16. public static void main(String[] args)
  17. {
  18. CardGameCH15 frame = new CardGameCH15();
  19. frame.setSize(500,200);
  20. frame.setTitle("Card Game");
  21. frame.setLocationRelativeTo(null);
  22. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23. frame.setVisible(true);
  24.  
  25. }
  26.  
  27. class CardTable extends JPanel implements ActionListener
  28. {
  29. String card;
  30. ImageIcon cardIcon;
  31. JButton dealButton = new JButton("Deal 5");
  32. CardDeck deck;
  33. JLabel card1 = new JLabel("");
  34. JLabel card2 = new JLabel("");
  35. JLabel card3 = new JLabel("");
  36. JLabel card4 = new JLabel("");
  37. JLabel card5 = new JLabel("");
  38. JRadioButton jrb1 = new JRadioButton("1 Deck");
  39. JRadioButton jrb2 = new JRadioButton("2 Decks");
  40. JRadioButton jrb3 = new JRadioButton("3 Decks");
  41. JRadioButton jrb4 = new JRadioButton("4 Decks");
  42.  
  43.  
  44.  
  45.  
  46.  
  47. public CardTable()
  48. {
  49. setLayout(new FlowLayout());
  50.  
  51. deck = new CardDeck(1);
  52. dealHand();
  53. add(card1);
  54. add(card2);
  55. add(card3);
  56. add(card4);
  57. add(card5);
  58. add(dealButton);
  59. add(jrb1);
  60. add(jrb2);
  61. add(jrb3);
  62. add(jrb4);
  63.  
  64. }
  65. dealButton.addActionListener(new ActionListener(){
  66. public void actionPerformed(ActionEvent e) {
  67. dealHand();
  68. }
  69. });
THE ABOVE ACTION LISTENER IS NOT WORKING, IS THERE ANOTHER WAY OF WRITING MULTIPLE ACTION LISTENERS OR DID I MAKE A SYNTAX ERROR

  1. private void dealHand()
  2. {
  3. String str;
  4.  
  5. if((str = deck.getCard()) != null)
  6. card1.setIcon(new ImageIcon("image/card/" + str));
  7. else
  8. return;
  9. if((str = deck.getCard()) != null)
  10. card2.setIcon(new ImageIcon("image/card/" + str));
  11. else
  12. return;
  13. if((str = deck.getCard()) != null)
  14. card3.setIcon(new ImageIcon("image/card/" + str));
  15. else
  16. return;
  17. if((str = deck.getCard()) != null)
  18. card4.setIcon(new ImageIcon("image/card/" + str));
  19. else
  20. return;
  21. if((str = deck.getCard()) != null)
  22. card5.setIcon(new ImageIcon("image/card/" + str));
  23. else
  24. return;
  25. }
  26. }
  27. }

I would appreciate any help...Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,030
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 153
JamesCherrill JamesCherrill is offline Offline
Veteran Poster
 
0
  #2
Nov 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 12
Reputation: geek_till_itMHZ is an unknown quantity at this point 
Solved Threads: 0
geek_till_itMHZ geek_till_itMHZ is offline Offline
Newbie Poster
 
0
  #3
Nov 10th, 2009
its not the deal hand method because when I just had a single action listener done something like

  1. dealbutton.addActionListener(this);.....
  2. .....
  3. public void actionPerformed(ActionEvent e)
  4. {
  5. dealHand();
  6. }

everything worked fine but now that I need multiple action listeners for my radio buttons im havin issues
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #4
Nov 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 12
Reputation: geek_till_itMHZ is an unknown quantity at this point 
Solved Threads: 0
geek_till_itMHZ geek_till_itMHZ is offline Offline
Newbie Poster
 
0
  #5
Nov 10th, 2009
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
^
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #6
Nov 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 12
Reputation: geek_till_itMHZ is an unknown quantity at this point 
Solved Threads: 0
geek_till_itMHZ geek_till_itMHZ is offline Offline
Newbie Poster
 
0
  #7
Nov 10th, 2009
Jackpot!! Thank you very much
Reply With Quote Quick reply to this message  
Reply

Tags
java

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 693 | Replies: 6
Thread Tools Search this Thread



Tag cloud for java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC