Solved

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

Posted on 2001-07-12
4
274 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
ID: 6279250
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
ID: 6280713
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
ID: 6284423
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
ID: 6285969
Thank you the evt.consume() did the trick!
Thanks,
Jamie
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 46
iterator/ListIterator approach 17 54
Android development question 2 81
hashmap order 17 42
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

726 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