944,116 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 9689
  • Java RSS
Aug 1st, 2005
0

Recognizing when a button is pressed.

Expand Post »
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 -
Java Syntax (Toggle Plain Text)
  1. enter = new JButton("Click here to calculate numeric palindrome.");
  2. enter.addActionListener(this);

running my methods when i click the button -
Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MFal is offline Offline
5 posts
since Jul 2005
Aug 1st, 2005
0

Re: Recognizing when a button is pressed.

Hi everyone,

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

Try this

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Aug 1st, 2005
0

Re: Recognizing when a button is pressed.

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.
Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MFal is offline Offline
5 posts
since Jul 2005
Aug 1st, 2005
0

Re: Recognizing when a button is pressed.

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Feb 28th, 2011
0
Re: Recognizing when a button is pressed.
for the button thing use this

Java Syntax (Toggle Plain Text)
  1. enter = new JButton("Click here to calculate numeric palindrome.");
  2. enter.setActionCommand("enter");
  3. enter.addActionListener(this);


and for the action thing this

Java Syntax (Toggle Plain Text)
  1. public void actionPerformed(ActionEvent e){
  2. if("enter".equals(e.getActionCommand())){
  3. //your code here
  4. }
  5. }
Reputation Points: 11
Solved Threads: 10
Junior Poster
Progr4mmer is offline Offline
111 posts
since Nov 2009
Feb 28th, 2011
0
Re: Recognizing when a button is pressed.
never surrender !!!

just replace

Java Syntax (Toggle Plain Text)
  1. JButton B = (JButton)e.getSource();

with

Java Syntax (Toggle Plain Text)
  1. Object obj = e.getSource();

your Container probably freeze, put your long action(s) to the backGround Task, and if is method "print()"
real outPut to PrinterJob, hmmmm ...
Last edited by mKorbel; Feb 28th, 2011 at 5:35 pm. Reason: EDT issue
Reputation Points: 425
Solved Threads: 212
Veteran Poster
mKorbel is offline Offline
1,092 posts
since Feb 2011
Feb 28th, 2011
0
Re: Recognizing when a button is pressed.
This thread is from 2005. I assume the OP has moved on. Closing.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

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.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: setting tool tip text not working
Next Thread in Java Forum Timeline: Please help, Calendar problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC