Recognizing when a button is pressed.

Reply

Join Date: Jul 2005
Posts: 5
Reputation: MFal is an unknown quantity at this point 
Solved Threads: 0
MFal MFal is offline Offline
Newbie Poster

Recognizing when a button is pressed.

 
0
  #1
Aug 1st, 2005
I really appreciate all the help i've been getting on my project lately from you guys, and hopefully this will be the last problem.

I've written an applet, and it uses ActionListener to detect when the button is pressed. The program is supposed to go through my methods when the button is pressed. However when i launch the applet and put values in the boxes, nothing happens when i click. Using an embedded class is too hard for me (i've been doing java for 5 weeks, and the only prior experience i had was html). Also i can't seem to make two seperate files, because only one can implement ActionListener.

here's what i have so far.

making the button -
  1. enter = new JButton("Click here to calculate numeric palindrome.");
  2. enter.addActionListener(this);

running my methods when i click the button -
  1. public void actionPerformed(ActionEvent e)
  2. {
  3. Numtemp = displayN.getText();
  4.  
  5. Base = displayB.getText();
  6.  
  7. convert();
  8. FindPalindrome();
  9. reconvert();
  10. print();
  11. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: Recognizing when a button is pressed.

 
0
  #2
Aug 1st, 2005
Hi everyone,

I have no idea the number of JButtons in your application but where's the source??

Try this

  1. public void actionPerformed(ActionEvent e)
  2. {
  3.  
  4. JButton B = (JButton)e.getSource();
  5.  
  6. if(B == enter)
  7. Numtemp = displayN.getText();
  8.  
  9. Base = displayB.getText();
  10.  
  11. convert();
  12. FindPalindrome();
  13. reconvert();
  14. print();
  15. }
  16.  
  17. }

I hope this helps you

Yours Sincerely

Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond

Tell me what type of software do you like and what would you pay for it

http://www.daniweb.com/techtalkforums/thread19660.html
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 5
Reputation: MFal is an unknown quantity at this point 
Solved Threads: 0
MFal MFal is offline Offline
Newbie Poster

Re: Recognizing when a button is pressed.

 
0
  #3
Aug 1st, 2005
Thanks for responding. I took that code and adapted it to my code, and it compiles, but i still have the same problem. Here is all of the relevant code. Oh and there is only one button.
  1. public void init()
  2. {
  3. displayN = new JTextField(35);
  4. displayN.setFont(new Font("Monospaced", Font.BOLD, 14));
  5. String message1 = "Enter Number Here";
  6. displayN.setText(message1);
  7.  
  8. displayB = new JTextField(15);
  9. displayB.setFont(new Font("Monospaced", Font.BOLD, 14));
  10. String message2 = "Enter Base Here";
  11. displayB.setText(message2);
  12.  
  13. enter = new JButton("Click here to calculate numeric palindrome.");
  14. enter.addActionListener(this);
  15.  
  16. displayS = new JTextField(60);
  17. displayS.setFont(new Font("Monospaced", Font.BOLD, 14));
  18.  
  19. Container c = getContentPane();
  20. c.setLayout(new FlowLayout());
  21. c.setBackground(Color.darkGray);
  22. c.add(displayN);
  23. c.add(displayB);
  24. c.add(enter);
  25. c.add(displayS);
  26.  
  27. }
  28.  
  29. //This tells the program to run when the button is clicked. Help for this code came from
  30. //freesoft_2000 at the www.daniweb.com/techtalkforums
  31. public void actionPerformed(ActionEvent e)
  32. {
  33. JButton B = (JButton)e.getSource();
  34.  
  35. if(B == enter)
  36. {
  37. Numtemp = displayN.getText();
  38.  
  39. Base = displayB.getText();
  40.  
  41. convert();
  42. FindPalindrome();
  43. reconvert();
  44. print();
  45. }
  46. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Recognizing when a button is pressed.

 
0
  #4
Aug 1st, 2005
Debugging 101: Put some system.out.println()'s throughout the actionPerformed() method, and all methods it calls. check to make sure all variables have the proper values, and that it even enters certain methods.

By the way, what are you doing with these lines:

Numtemp = displayN.getText();

Base = displayB.getText();


If those are used in the coorisponding methods called, I would suggest passing them as parameters instead.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC