Solved

Need keys to perform action, and not edit text in a JTextField

Posted on 2001-07-12
4
263 Views
Last Modified: 2012-06-27
I have a JTextField with several components. When the JTextField has focus I would like to use the <->, <+>, <Ins>, <Del>, keys as hot keys on buttons. I have the funcionality correct for the hotkeys, however when I type the <->, <+>, <Ins>, <Del> it is adds or deletes the JTextField text first. I would like to disable the <->, <+>, <Ins>, <Del> keys from changing the text in the JTextField and only use them for the keyListner to perform the methods of the buttons.

I feel like there should be an included method to do this.
I could change the JTextField to different type text object if I can find something that works better.
I would appreciate any help.
0
Comment
Question by:JDay
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:kotan
Comment Utility
Add KeyListener to your JTextField. Then from the KeyEvent parameter, you can get the key pressed through getKeyChar() or getKeyCode() to compare it.

YourJTextField.addKeyListener(new java.awt.event.KeyAdapter()
{
     public void keyPressed(java.awt.event.KeyEvent evt)
     {
          // See Blow
             if (evt.getKeyChar() == '-')
               // do your thing.
          else if (evt.getKeyChar() == '+')
               // do your thing.
          else if (evt.getKeyCode() == KeyEvent.VK_INSERT)
               // do your thing.
          else if (evt.getKeyCode() == KeyEvent.VK_DELETE)
               // do your thing.
        }
});


0
 

Author Comment

by:JDay
Comment Utility
I have already tried this solution.

The textfield should not be edited when the keys are pressed. For example, the minus should not be added to the textfield before calling the associated method. The textfield is still being edited.

JDay
0
 
LVL 6

Accepted Solution

by:
kotan earned 75 total points
Comment Utility
Now you change the event to keyTyped and add a line evt.consume() to prevend the certain key to be output to edit box.

YourJTextField.addKeyListener(new java.awt.event.KeyAdapter() {
     public void keyTyped(java.awt.event.KeyEvent evt) {
        // See Blow
        if (evt.getKeyChar() == '-') {
        evt.consume();
           // do your thing.
        }
        else if (evt.getKeyChar() == '+') {
            evt.consume();
           // do your thing.
        }
});
0
 

Author Comment

by:JDay
Comment Utility
Thank you the evt.consume() did the trick!
Thanks,
Jamie
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now