I have a swt text box with which I have a key listener assigned.
I want that the first character in the text box should not be the space
for that I am using the following code –
textCSVFileName.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(textCSVFileName.getCharCount() == 0){
if(e.keyCode == 32){
}
}
}
now I know when the first character is entered by user is space.
but if the above condition is true then how can I restrict it from entering in the textbox?
Any help?
Although the question is answered with a dirty workaround*, I would suggest having a look at the
SWT.Verifyevent, listening to that and preventing spaces at the first position:This will prevent a space at the beginning in any case.
*The “workaround” does not even work. Just think about what happens when you enter a space at the first position, when the text already contains some text. That will not be prevented by that approach.