?
Solved

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

Posted on 2001-07-12
4
Medium Priority
?
300 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 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

749 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