Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT TextArea listener

Tags:

java

gwt

I try to use gwt to create a textarea and a counter under it with the length of the characters, but it doesn't counts the backspace and with 1 character it has length 0. Here's my code. What can be the problem?

public class Test implements EntryPoint {

 TextArea textArea;
 Label  counter;

 public void onModuleLoad() {
  textArea = new TextArea();
  counter = new Label("Number of characters: 0");
  textArea.addKeyPressHandler(new KeyPressHandler() {
        public void onKeyPress(KeyPressEvent event) {
       counter.setText("Number of characters: " + textArea.getText().length());
   }
  });
  RootPanel.get("myContent").add(textArea);
  RootPanel.get("myContent").add(counter);
}
like image 489
Infinite Possibilities Avatar asked Feb 26 '23 14:02

Infinite Possibilities


2 Answers

Perhaps you want to track KeyUp event instead:

textArea.addKeyUpHandler(new KeyUpHandler() {
    public void onKeyUp(KeyUpEvent event) {
        counter.setText("Number of characters: " + textArea.getText().length());
    }
});
like image 123
barti_ddu Avatar answered Mar 07 '23 17:03

barti_ddu


I think checked this code should work

public class TextAreaEx implements EntryPoint {
 final TextArea textArea = new TextArea();
 final Label counter = new Label("Number of characters: 0");

public void onModuleLoad() {
    RootPanel.get().add(textArea);
    RootPanel.get().add(counter);
    addlistener();
}

private void addlistener() {
       textArea.addKeyUpHandler(new KeyUpHandler() {
            public void onKeyUp(KeyUpEvent keyUpEvent) {
              counter.setText(" Number of characters:"+textArea.getText().length());
            }
        });
        textArea.addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent changeEvent) {
                counter.setText(" Number of characters:"+textArea.getText().length());
            }
        });
  }

}

like image 27
Jama A. Avatar answered Mar 07 '23 18:03

Jama A.