Bom Dia pessoal,
Fiz esse código para entrada de dados do tipo decimal com 2 casas em uma caixa de texto
this.textPrice.addListener(SWT.Verify, new Listener() {
public void handleEvent(Event e) {
int cursorPosition = textPrice.getCaretPosition();
int commaPosition = textPrice.getText().indexOf(",");
if ('0' <= e.character && e.character <= '9'){
if(commaPosition >= 0){
if((cursorPosition <= commaPosition) && (textPrice.getText().substring(0, commaPosition).length() > 3)){
e.doit = false;
}
if((cursorPosition > commaPosition) && (textPrice.getText().substring(commaPosition, textPrice.getText().length()).length() > 2)){
e.doit = false;
}
}else{
if(textPrice.getText().length() > 3){
e.doit = false;
}
}
}else if (e.character == ','){
if(textPrice.getText().equals("")){
e.doit = false;
}
if(textPrice.getText().contains(",")){
e.doit = false;
}
}else if (e.character == SWT.DEL || e.character == SWT.BS){
e.doit = true;
}else{
e.doit = false;
}
}
});
Esta funcionando do jeito que eu planejei, porém gostaria que se o usuário digitasse por exemplo 1,1 , o campo fosse formatado para 1,10 na perda do foco, então fiz o seguinte código
this.textPrice.addFocusListener(new FocusListener() {
public void focusLost(FocusEvent e) {
DecimalFormat f = new DecimalFormat("#.00");
Double value = Double.parseDouble(textPrice.getText().replace(',', '.'));
textPrice.setText(f.format(value).replace('.',','));
}
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
}
});
Porém o método setText não esta alterando a propriedade text do componente,
Alguém pode me explicar porque isso acontece?
obrigado