2
I have a stylized component with phone formatting as follows
(XX)XXXXX-XXXX
I’d like to know, how can I withdraw the DD part (XX), so that the field stays just like this: XXXXX-XXXX
I tried to modify the cases, but not quite right.
My component:
import javafx.application.Platform;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.control.TextFormatter;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import java.util.function.UnaryOperator;
public class CampoTelefone extends TextField {
UnaryOperator filter;
public CampoTelefone() {
/*Quando o campo é denominado apenas para numeros inteiros*/
filter = new UnaryOperator<TextFormatter.Change>() {
@Override
public TextFormatter.Change apply(TextFormatter.Change t) {
String text = t.getText();
for (int i = 0; i < text.length(); i++) {
if (!Character.isDigit(text.charAt(i)) && text.charAt(i) != '-' && text.charAt(i) != '.'
&& text.charAt(i) != '/' && text.charAt(i) != '(' && text.charAt(i) != ')' && text.charAt(i) != ':') {
return null;
}
}
return t;
}
};
setTextFormatter(new TextFormatter<String>(filter));
focusedProperty().addListener(e -> mascara(getText(), getText()));
setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(final KeyEvent keyEvent) {
if (keyEvent.getCode().equals(KeyCode.UNDEFINED)
|| keyEvent.getCode().equals(KeyCode.MINUS)
|| keyEvent.getCode().equals(KeyCode.SUBTRACT)
|| keyEvent.getCode().equals(KeyCode.DIVIDE)
|| keyEvent.getCode().equals(KeyCode.PERIOD)) {
setText(getText().replace("-", "").replace("/", "").replace(".", "").replace("(", "").replace(")", "").replace(":", "").replace(" ", ""));
}
if (keyEvent.getCode().equals(KeyCode.CONTROL)) {
Clipboard clpbrd = Clipboard.getSystemClipboard();
final ClipboardContent content = new ClipboardContent();
String rep = clpbrd.getString().replaceAll("[^\\d.]", "");
content.putString(rep);
clpbrd.setContent(content);
}
}
});
textProperty().addListener((ObservableValue<? extends String> ov, String antigo, String novo) -> {
if (!(novo).isEmpty() && novo.length() > antigo.length()) {
novo = novo.replace(".", "").replace("-", "").replace("/", "").replace("(", "").replace(")", "").replace(",", "").replace(":", "").replace(" ", "");
//antigo = antigo.replace(".", "").replace("-", "").replace("/", "").replace(",", "").replace("(", "").replace(")", "");
mascara(novo, antigo);
}
}
);
}
public void mascara(String novo, String antigo) {
switch (novo.length()) {
case 1:
setText("(" + novo);
Platform.runLater(() -> {
end();
});
break;
case 2:
setText("(" + novo + ")");
Platform.runLater(() -> {
end();
});
break;
case 3:
setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 3));
Platform.runLater(() -> {
end();
});
break;
case 4:
setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 4));
Platform.runLater(() -> {
end();
});
break;
case 5:
setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 5));
Platform.runLater(() -> {
end();
});
break;
case 6:
setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 6) + "-");
Platform.runLater(() -> {
end();
});
break;
case 7:
setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 6) + "-" + novo.substring(6, 7));
Platform.runLater(() -> {
end();
});
break;
case 8:
setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 6) + "-" + novo.substring(6, 8));
Platform.runLater(() -> {
end();
});
break;
case 9:
setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 6) + "-" + novo.substring(6, 9));
Platform.runLater(() -> {
end();
});
break;
case 10:
setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 6) + "-" + novo.substring(6, 10));
Platform.runLater(() -> {
end();
});
break;
case 11:
setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 7) + "-" + novo.substring(7, 11));
Platform.runLater(() -> {
end();
});
break;
case 12:
setText(antigo);
break;
default:
setText(antigo);
}
}
public void setValor(Object valor) {
setText(String.valueOf(valor)); //To change body of generated methods, choose Tools | Templates.
}
public Object getValor() {
return ((!getText().isEmpty())
? (getText()).replace("-", "").replace(".", "").replace("/", "").replace("(", "").replace(")", "") : "");
}
@Override
public String getText(int start, int end) {
if (start > end) {
}
if (start < 0 || end > getLength()) {
//throw new IndexOutOfBoundsException();
}
return getContent().get(0, 0);
}
}
For this you will have to overload the phone text class and make your customization.
– Dev
Adjustment has to be made in this class or you can propose another solution?
– Renan Gomes
@Renan can propose another, any suggestion is welcome - welcome !
– G1Win