[RESOLVIDO] Máscara de Telefone com 8 e 9 dígitos + DDD

Fala pessoal, pra quem já manja é uma tarefa fácil, mas pra quem tá começando é um pouco difícil pensar nisso. Então, pra quem estiver precisando de uma máscara de telefone sem usar o jFormattedTextField, pode usar essa aqui. Coloca um evento de FocusLost (toda vez que perder o foco):

    String campo = campotelefone.getText();
    switch (campo.length()) {
        case 11: {
            String ddd, telefone;
            ddd = String.valueOf("(" + campo.charAt(0) + campo.charAt(1) + ")");
            telefone = String.valueOf(" " + campo.charAt(2) + campo.charAt(3) + campo.charAt(4) + campo.charAt(5) + campo.charAt(6) + "-" + campo.charAt(7) + campo.charAt(8) + campo.charAt(9) + campo.charAt(10));
            campotelefone.setText(ddd + telefone);
            break;
        }
        case 10: {
            String ddd, telefone;
            ddd = String.valueOf("(" + campo.charAt(0) + campo.charAt(1) + ")");
            telefone = String.valueOf(" " + campo.charAt(2) + campo.charAt(3) + campo.charAt(4) + campo.charAt(5) + "-" + campo.charAt(6) + campo.charAt(7) + campo.charAt(8) + campo.charAt(9));
            campotelefone.setText(ddd + telefone);
            break;
        }
        default:
            break;
    }
1 curtida