?
Solved

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

Posted on 2001-07-12
4
Medium Priority
?
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:
Suggested Courses

649 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