Textfield keylistener problem

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?
LVL 5
tbboyettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BogoJokerCommented:
Hi tbboyett,

public String getSelectedText();

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

Joe P
0
CEHJCommented:
You'd be better to use a DocumentListener or a custom Document, or you'll only be able to operate 'key-wise'
0
BogoJokerCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

CEHJCommented:
You need not worry about backspace using a listener. removeUpdate is what you need:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tbboyettAuthor Commented:
> 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
tbboyettAuthor Commented:
> http://javaalmanac.com/egs/javax.swing.text/ChangeEvt.html

looks like what I need, i'll give it a shot
0
tbboyettAuthor Commented:
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
CEHJCommented:
You could use a custom Document to do that.
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.