0

Here I am using search TextField and search button in my program

searchTxt = new TextField();
searchTxt.setWidth("400px");

search = new Button("Search");
search.setImmediate(true);
search.addListener((Button.ClickListener) this);

public void buttonClick(ClickEvent event) {
final Button button = event.getButton();
    if (button == search) {
        String searchText = (String) searchTxt.getValue();
        searchText = searchText.trim();

        if (!searchText.equals(GlobalConstants.EMPTY_STRING) && searchText != null)
        {
           // logic
        }               
    }

}

which logic should I use here by performance point of view?

pxm
  • 1,611
  • 2
  • 18
  • 34

2 Answers2

1

First and foremost, you should not so much care about performance in GUI event handling code. If you'll encounter performance problems they will most probably not emerge in user input validation code.

So, when writing an application your focus should be on maintainability and readability. You'll best achieve that when you use built-in framework functionality. So, assuming that with 'check that entered text only contains alphanumeric characters' you mean 'validate that...' you can use Vaadin's validators, and more specifically the RegexpValidator. Your code would then look like

searchTxt = new TextField();
 // match at least one or more alphanumeric characters
searchTxt.addValidator(new RegexpValidator("\\w+", true, "Error msg: not valid..."));
public void buttonClick(ClickEvent event) {
final Button button = event.getButton();
    if (button == search) {
        if (searchTxt.isValid()) {
            // logic
        }    
    }
}

With that, the logic will only be executed if the user has entered at least one alphanumeric character.

Roland Krüger
  • 904
  • 5
  • 15
0

You can do it as follows:
1. Check that the length of the String is 1.
2. If so, get the character at position zero by using charAt() method of String.
3. Then use Character.isAlphabetic() or Character.isDigit() to do your validation.

Another option would be to use RegEx which would greatly reduce your lines of code but increase your learning curve.

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • could you please give me any example of regular expression and whether this logic is good by performance point of view or not? (It means searching Speed) – Amol Shewale May 13 '14 at 12:45