We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Textfield keylistener problem

tbboyett
tbboyett asked
on
Medium Priority
454 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?
Comment
Watch Question

Hi tbboyett,

public String getSelectedText();

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

Joe P
CERTIFIED EXPERT
Top Expert 2016

Commented:
You'd be better to use a DocumentListener or a custom Document, or you'll only be able to operate 'key-wise'
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.
CERTIFIED EXPERT
Top Expert 2016
Commented:
You need not worry about backspace using a listener. removeUpdate is what you need:

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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?

Author

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

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

Author

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?
CERTIFIED EXPERT
Top Expert 2016

Commented:
You could use a custom Document to do that.
CERTIFIED EXPERT
Top Expert 2016

Commented:
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.