Java Utililty Methods Swing Text Format

List of utility methods to do Swing Text Format

Description

The list of methods to do Swing Text Format are organized into topic(s).

Method

intfindNext(boolean reverse, int pos)
Find and select the next searchable matching text.
boolean backwards = IS_BACKWARDS_CHECKBOX.isSelected();
backwards = backwards ? !reverse : reverse;
String pattern = (String) FIND_FIELD.getSelectedItem();
if (pattern != null && pattern.length() > 0) {
    try {
        Document doc = textComponent.getDocument();
        doc.getText(0, doc.getLength(), SEGMENT);
    } catch (Exception e) {
...
StringformartCpf(String cpf)
formart Cpf
try {
    return formatNumericString(cpf, "###.###.###-##");
} catch (ParseException e) {
    return null;
Stringformat(String pattern, Object value)
format
MaskFormatter mask;
try {
    mask = new MaskFormatter(pattern);
    mask.setValueContainsLiteralCharacters(false);
    return mask.valueToString(value);
} catch (ParseException e) {
    throw new RuntimeException(e);
Stringformata(String s, String mascara)
formata
MaskFormatter formatter;
try {
    formatter = new MaskFormatter(mascara);
    formatter.setValueContainsLiteralCharacters(false);
    return formatter.valueToString(s);
} catch (ParseException e) {
    return s;
StringformatAreaImperial(BigDecimal areaDec)
Formats a BigDecimal value representing an area in square meters an imperial area (i.e.
String result = null;
if (areaDec != null && BigDecimal.ZERO.compareTo(areaDec) < 0) {
    result = "";
    Double area = areaDec.doubleValue();
    NumberFormatter areaFormatter = new NumberFormatter(DecimalFormat.getNumberInstance());
    try {
        Double acresTmp = new Double(area / METRES_IN_ACRE);
        int acres = acresTmp.intValue();
...
StringformatAreaMetric(BigDecimal area)
Formats a BigDecimal value representing an area in square metres into a string indicating the area as metres squared (where area is less that 10,000) or hectares (where area is >= 10,000
String result = null;
if (area != null && BigDecimal.ZERO.compareTo(area) < 0) {
    NumberFormatter areaFormatter = new NumberFormatter(DecimalFormat.getNumberInstance());
    try {
        if (area.compareTo(new BigDecimal("10000")) >= 0) {
            area = area.divide(new BigDecimal("10000"));
            area = area.setScale(3, RoundingMode.DOWN);
            result = areaFormatter.valueToString(area) + "ha";
...
StringformatCEP(String str)
format CEP
str = str.replaceAll("[\\D]", "");
try {
    str = String.format("%08d", Long.parseLong(str));
    String pattern = "#####-###";
    try {
        MaskFormatter mask = new MaskFormatter(pattern);
        mask.setValueContainsLiteralCharacters(false);
        return mask.valueToString(str);
...
StringformatCpfCnpj(String cpfCnpj)
format Cpf Cnpj
if (cpfCnpj.length() == 9)
    return format("###.###.###-##", cpfCnpj);
else if (cpfCnpj.length() == 14)
    return format("##.###.###/####-##", cpfCnpj);
else
    return "";
StringformatCpfCNPJ(String str)
format Cpf CNPJ
str = str.replaceAll("[\\D]", "");
String pattern;
if (str.length() <= 11) {
    str = String.format("%011d", Long.parseLong(str));
    pattern = "###.###.###-##";
} else {
    str = String.format("%014d", Long.parseLong(str));
    pattern = "##.###.###/####-##";
...
StringformatNumericString(String string, String mask)
format Numeric String
javax.swing.text.MaskFormatter mf = new javax.swing.text.MaskFormatter(mask);
mf.setValueContainsLiteralCharacters(false);
return mf.valueToString(string);