?
Solved

Textfield keylistener problem

Posted on 2006-03-27
10
Medium Priority
?
435 Views
Last Modified: 2012-05-05
I have the below code

textfield.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
            filterStr = filterKeyTyped(evt, textfield.getText().trim());    
            System.out.println(filterStr)
});

private String filterKeyTyped(KeyEvent e, String string) {
        if(e.getKeyChar() == 8 ){ // backspace character was hit
            int length = string.length();
            if(length > 1) {
                string = string.substring(0, length - 1).toUpperCase();
            }
            else {
                string = "";
            }
        }
        else {
            string += e.getKeyChar();
        }

        if (!isNumber(string)) {
            string = "";
        }
       
        return (string);
    }

    public boolean isNumber(String text) {
        try {
            int test = Integer.parseInt(text.trim());
            return true;
        } catch (NumberFormatException nfe) {
            return false;
        }
    }

Works fine except for one problem, if the user selects all of the text in the textfield and hits delete, it only removes one character from the string.
How can i detect if they highlight all the text and delete it so i can set the string back to nothing?
0
Comment
Question by:tbboyett
  • 5
  • 3
  • 2
10 Comments
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16303385
Hi tbboyett,

public String getSelectedText();

check if that is not null or its length is greater then 0

Joe P
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16303391
You'd be better to use a DocumentListener or a custom Document, or you'll only be able to operate 'key-wise'
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16303396
Here are the java docs:
http://java.sun.com/j2se/1.5.0/docs/api/

Browse to: JTextField
getSelectedText() is one of its public methods.
0
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.

 
LVL 86

Accepted Solution

by:
CEHJ earned 1200 total points
ID: 16303425
You need not worry about backspace using a listener. removeUpdate is what you need:

http://javaalmanac.com/egs/javax.swing.text/ChangeEvt.html
0
 
LVL 5

Author Comment

by:tbboyett
ID: 16303432
> getSelectedText()

I've tried the getselectedtext but for some reason returns null when they select all the text and click backspace or delete

> You'd be better to use a DocumentListener or a custom Document, or you'll only be able to operate 'key-wise'

Can explain briefly about the DocumentListener or do you have short example I can view?
0
 
LVL 5

Author Comment

by:tbboyett
ID: 16303454
> http://javaalmanac.com/egs/javax.swing.text/ChangeEvt.html

looks like what I need, i'll give it a shot
0
 
LVL 5

Author Comment

by:tbboyett
ID: 16303602
Works perfect, just one last quick question.  Is there a quick and easy way to limit a textfield to a certain amount of characters or will i just need to check each time to see how many are entered?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16303613
You could use a custom Document to do that.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16303623
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16304046
:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

809 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