aspose file tools*
The moose likes Beginning Java and the fly likes A Problem related to KEYEVENTS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A Problem related to KEYEVENTS" Watch "A Problem related to KEYEVENTS" New topic
Author

A Problem related to KEYEVENTS

lakmal padmakumara
Ranch Hand

Joined: Aug 08, 2009
Posts: 47
Hi, I'm coding a java calculator at the moment. There are Buttons to add ,subtract ,Multiply....Now what I want to do is when I press + key in the keyboard it should activate the "ADD" button in the J Frame. Similarly when I press '-' key on the keyboard it should activate the 'SUB' button on J Frame.

There is a Text Field in the J Frame. When I press a key like '+' '-' '*' ...... in Key Board it should not print that sign in TextField ,instead of that it should activate the related Button on the J Frame as I have mentioned about .



I tried something like this !! But When I press + key it prints '+' on the text field .I want to avoid that because it hurts my later calculations.It should only activate actionPerformed method of that particular Button. As you know in normal Calculators when you press a operator It won't show that operator in the display.So I think you got my problem !!


[Bsc in Computer Science and Engineering] [SCJP 80%][BCS certificate level]
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2095
    
    7
Read the section from the Swing tutorial on "How to Use Key Bindings".

Your custom Action can just invoke the doClick() method of the appropriate button.
lakmal padmakumara
Ranch Hand

Joined: Aug 08, 2009
Posts: 47
Thanks Rob the doClick() method is very helpful.But Still I could not find an answer for my question. That is
When my code is



it calls the ADD button.But it also prints the '+' sign on my Text Field .How Can I restrict that!? Because when i press equal button it gets the number on the text field and does the calculation .But if you have + sign with the number I cannot do the calculations.

Simply what i want to do is click the ADD button when I press + Key(I can obtain that functionality using doClick() method). And also to avoid the printing of + sign in the Text Field . (how to do this?)
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2095
    
    7
(how to do this?)


I already told you that you need to use Key Bindings.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A Problem related to KEYEVENTS
 
Similar Threads
Load tree on click of button
action performed
Swing incorrect behaviour from JRE5 to JRE6
Robot - can't get symbols: !@#$^&*()_+
keyboard action of my JButton