Applet -more than one event handler

Reply

Join Date: Feb 2008
Posts: 17
Reputation: Natique is an unknown quantity at this point 
Solved Threads: 0
Natique Natique is offline Offline
Newbie Poster

Applet -more than one event handler

 
0
  #1
Aug 27th, 2008
Hi
I was messing around with java applets just now, and I can't seem to figure out how to put more than one event handler in an applet. I'm just working on a really simple calculator. Three buttons, one to add, one to subtract and one to clear everything. I know it can easily be done with one event handler, but it's not about the program, just about learning how to use more than one. Here's the code, any idea what's wrong? Thanks!

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class calculator extends JApplet
  6. {
  7. static JLabel resultLabel;
  8. static JLabel entryLabel;
  9. static JLabel outputLabel;
  10. static JTextField enterNum;
  11. static JButton adding;
  12. static JButton subtracting;
  13. static JButton clearbutton;
  14. static double result;
  15.  
  16. //listener for clear button
  17. static class clearHandler implements ActionListener
  18. {
  19. public void actionPerformed(ActionEvent event)
  20. {
  21. result=0.0;
  22. outputLabel.setText("0.0");
  23. enterNum.setText("");
  24.  
  25. }
  26. }
  27. //listener for adding and subtracting buttons
  28. static class operatorHandler implements ActionListener
  29. {
  30. public void actionPerformed(ActionEvent event)
  31. {
  32. double secondoperand=0.0;
  33. secondoperand=Double.parseDouble(enterNum.getText());
  34. String whichbutton=event.getActionCommand();
  35. if (whichbutton.equals("+"))
  36. result=result+secondoperand;
  37. else if (whichbutton.equals("-"))
  38. result =result-secondoperand;
  39. outputLabel.setText(result+" ");
  40. enterNum.setText("");
  41.  
  42. }
  43. }
  44.  
  45.  
  46. operatorHandler operation;
  47. clearHandler clearing;
  48.  
  49. public void init()
  50. {
  51. result=0.0;
  52. resultLabel=new JLabel("result: ");
  53. entryLabel=new JLabel("enter #: ");
  54. outputLabel=new JLabel("0.0");
  55. adding=new JButton("+");
  56. subtracting=new JButton("-");
  57. clearbutton =new JButton("clear");
  58. enterNum=new JTextField("value here ",10);
  59.  
  60. adding.addActionListener(operation);
  61. subtracting.addActionListener(operation);
  62. clearbutton.addActionListener(clearing);
  63.  
  64. add(resultLabel);
  65. add(outputLabel);
  66. add(entryLabel);
  67. add(enterNum);
  68. add(adding);
  69. add(subtracting);
  70. add(clearbutton);
  71. setLayout(new GridLayout(4,1));
  72.  
  73. }
  74. }
Last edited by Natique; Aug 27th, 2008 at 8:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 410
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Applet -more than one event handler

 
1
  #2
Aug 27th, 2008
1. what errors do you actually get?
2. ALL class names should begin with a capital
3. you never actually initialize your handlers operatorHandler operation = new operatorHandler();
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,439
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Applet -more than one event handler

 
1
  #3
Aug 28th, 2008
Also, don't make everything in your class static for no reason.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 410
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Applet -more than one event handler

 
0
  #4
Aug 28th, 2008
oh, i didn't notice nice catch
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,439
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Applet -more than one event handler

 
0
  #5
Aug 28th, 2008
Well, you caught #3, which is the root of the poster's problem. The static thing was just an aside
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: Natique is an unknown quantity at this point 
Solved Threads: 0
Natique Natique is offline Offline
Newbie Poster

Re: Applet -more than one event handler

 
0
  #6
Aug 28th, 2008
Thank you! the error was that the buttons weren't doing anything, which is now fixed. But I didn't get why I should capitalize my class names? is that just common practice or is there a reason for it?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,439
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Applet -more than one event handler

 
0
  #7
Aug 28th, 2008
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: Natique is an unknown quantity at this point 
Solved Threads: 0
Natique Natique is offline Offline
Newbie Poster

Re: Applet -more than one event handler

 
0
  #8
Aug 28th, 2008
Cool, never knew that existed. thanks!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: Natique is an unknown quantity at this point 
Solved Threads: 0
Natique Natique is offline Offline
Newbie Poster

Re: Applet -more than one event handler

 
0
  #9
Aug 28th, 2008
Sorry if this is the wrong forum now. But how can I view my applet in a web browser? I used the code below, and I also uploaded operatorHandler.class and clearHandler.class to the directory. Should I have made a .jar file? If so, how?

  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <applet code="calculator.class" codebase="I put it- don't know it off hand" width=200 height=200></applet>
  6. </body>
  7. </html>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC