2008/12/2 Paulo Santos <[EMAIL PROTECTED]>:
> As format a JFormattedTextField with 99º99' 99.99' '? I am using NetBeans
> 6.1. My Text must be 99º99' 99.99' ' e my Value 99999999.

Hi Paulo

You need a class derived from JFormattedTextField.AbstractFormatter to
do this for you...  and I just happen to have one here :)

Hope this helps

Michael


/**
 * Formatter to handle DMS data with JFormattedTextField
 *
 * @author Michael Bedward <[EMAIL PROTECTED]>
 */
class DMSFormatter extends JFormattedTextField.AbstractFormatter {

    public static final String DEGREES = "\u00B0";
    public static final String MINUTES = "\'";
    public static final String SECONDS = "\"";
    public static final String DECIMAL = ".";

    private static final String[] delims = {DEGREES, MINUTES, DECIMAL, SECONDS};

    /**
     * Parse a string of degrees, minutes, seconds, decimal seconds
     * to an Long representation
     * @param text input string
     * @return a new Long value
     * @throws java.text.ParseException
     */
    @Override
    public Object stringToValue(String text) throws ParseException {
        long value = 0;

        int pos = 0;
        long x = 1000000;
        for (String delim : delims) {
            int index = text.indexOf(delim, pos);
            if (index > pos) {
                int part = Integer.parseInt(text.substring(pos, index));
                value += x * part;
                pos = index + 1;
            }
            x /= 100;
        }

        return new Long(value);
    }

    /**
     * Format 99999999 as 99d99m99.99s
     * @param value an Integer value
     * @return formatted string
     * @throws java.text.ParseException
     */
    @Override
    public String valueToString(Object value) throws ParseException {
        int ival = (Integer)value;
        int[] parts = new int[4];
        for (int i = 0; i < parts.length; i++) {
            if (ival > 0) {
                parts[i] = ival % 100;
                ival /= 100;
            } else {
                parts[i] = 0;
            }
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < parts.length; i++) {
            sb.append(parts[parts.length - i - 1]);
            sb.append(delims[i]);
        }

        return sb.toString();
    }
}

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to