943,626 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1925
  • Java RSS
Aug 27th, 2008
0

Applet -more than one event handler

Expand Post »
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!

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
Natique is offline Offline
28 posts
since Feb 2008
Aug 27th, 2008
1

Re: Applet -more than one event handler

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();
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Aug 28th, 2008
1

Re: Applet -more than one event handler

Also, don't make everything in your class static for no reason.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Aug 28th, 2008
0

Re: Applet -more than one event handler

oh, i didn't notice nice catch
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Aug 28th, 2008
0

Re: Applet -more than one event handler

Well, you caught #3, which is the root of the poster's problem. The static thing was just an aside
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Aug 28th, 2008
0

Re: Applet -more than one event handler

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?
Reputation Points: 11
Solved Threads: 0
Light Poster
Natique is offline Offline
28 posts
since Feb 2008
Aug 28th, 2008
0

Re: Applet -more than one event handler

Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Aug 28th, 2008
0

Re: Applet -more than one event handler

Cool, never knew that existed. thanks!
Reputation Points: 11
Solved Threads: 0
Light Poster
Natique is offline Offline
28 posts
since Feb 2008
Aug 28th, 2008
0

Re: Applet -more than one event handler

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?

Java Syntax (Toggle Plain Text)
  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>
Reputation Points: 11
Solved Threads: 0
Light Poster
Natique is offline Offline
28 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: do cookies contain passwords??
Next Thread in Java Forum Timeline: Demo projects





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC