How to parse a number in a JTextField dynamically

32
June 12, 2019, at 08:00 AM

I'm trying to dynamic parse a entry in a JtextField, basically if my user type 100000 the TextField woud show 100 000

I've tried to show a popup Menu but this is not relevent, i've tried to just parse when the user type the key Enter or when the entry loose the focus, and it work but this is not really intuitive.

So there is a bit of code who's runnable, btw i knwo that the method to parse is not really effiecient but it actually work sooo...

import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.NumberFormat;
import java.text.ParseException;
public class Test extends PlainDocument {
    private static final long serialVersionUID = 1L;
    private final JTextComponent _textField;
    public Test(JTextComponent field) {
        _textField = field;
    }
    @Override
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
        super.insertString(offs, str, a);
        String word = null;
        try {
            word = autoComplete(getText(0, getLength()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (word != null) {
            super.insertString(offs + str.length(), word, a);
            _textField.setCaretPosition(offs + str.length());
            _textField.moveCaretPosition(getLength());
        }
    }
    public String autoComplete(String text) throws ParseException {
        NumberFormat nf = NumberFormat.getInstance();
        if (nf.parse(text).intValue() > 0) {
            return numberParser(text);
        }
        return null;
    }
    public static JTextField createAutoCompleteTextField() {
        JTextField field = new JTextField(20);
        Test doc = new Test(field);
        field.setDocument(doc);
        return field;
    }

    public static void main(String args[]) {
        //Le jtextField qui sera autocompleté
        final JTextField field = Test.createAutoCompleteTextField();
        //Action clavier touche entrer
        field.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int key = e.getKeyCode();
                if (key == KeyEvent.VK_ENTER) {
                    Toolkit.getDefaultToolkit().beep();
                    System.out.println("Text : " + field.getText());
                }
            }
        });

        //creation de fenetre principale
        JFrame frame = new JFrame("NumberParser");
        frame.setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JLabel("Entry : "));
        frame.add(field);
        frame.pack();
        frame.setVisible(true);
    }
    public String numberParser(String data) {
        String out = "";
        if (data.length() >= 4 && !data.contains(".")) {
            switch (data.length()) {
                case 4:
                    out = data.substring(0, 1) + " " + data.substring(1, 4);
                    break;
                case 5:
                    out = data.substring(0, 2) + " " + data.substring(2, 5);
                    break;
                case 6:
                    out = data.substring(0, 3) + " " + data.substring(3, 6);
                    break;
                case 7:
                    out = data.substring(0, 1) + " " + data.substring(1, 4) + " " + data.substring(4, 7);
                    break;
                case 8:
                    out = data.substring(0, 2) + " " + data.substring(2, 5) + " " + data.substring(5, 8);
                    break;
                case 9:
                    out = data.substring(0, 3) + " " + data.substring(3, 6) + " " + data.substring(6, 9);
                    break;
                case 10:
                    out = data.substring(0, 1) + " " + data.substring(1, 4) + " " + data.substring(4, 7) + " " + data.substring(7, 10);
                    break;
                case 11:
                    out = data.substring(0, 2) + " " + data.substring(2, 5) + " " + data.substring(5, 8) + " " + data.substring(8, 11);
                    break;
                case 12:
                    out = data.substring(0, 3) + " " + data.substring(3, 6) + " " + data.substring(6, 9) + " " + data.substring(9, 12);
                    break;
                case 13:
                    out = data.substring(0, 1) + " " + data.substring(1, 4) + " " + data.substring(4, 7) + " " + data.substring(7, 10) + " " + data.substring(10, 13);
                    break;
                case 14:
                    out = data.substring(0, 2) + " " + data.substring(2, 5) + " " + data.substring(5, 8) + " " + data.substring(8, 11) + " " + data.substring(11, 14);
                    break;
                case 15:
                    out = data.substring(0, 3) + " " + data.substring(3, 6) + " " + data.substring(6, 9) + " " + data.substring(9, 12) + " " + data.substring(12, 15);
                    break;
            }
        } else if (data.contains(".")) {
            String deci = data.substring(data.indexOf("."));
            data = data.substring(0, data.indexOf("."));
            switch (data.length()) {
                case 4:
                    out = data.substring(0, 1) + " " + data.substring(1, 4) + deci;
                    break;
                case 5:
                    out = data.substring(0, 2) + " " + data.substring(2, 5) + deci;
                    break;
                case 6:
                    out = data.substring(0, 3) + " " + data.substring(3, 6) + deci;
                    break;
                case 7:
                    out = data.substring(0, 1) + " " + data.substring(1, 4) + " " + data.substring(4, 7) + deci;
                    break;
                case 8:
                    out = data.substring(0, 2) + " " + data.substring(2, 5) + " " + data.substring(5, 8) + deci;
                    break;
                case 9:
                    out = data.substring(0, 3) + " " + data.substring(3, 6) + " " + data.substring(6, 9) + deci;
                    break;
                case 10:
                    out = data.substring(0, 1) + " " + data.substring(1, 4) + " " + data.substring(4, 7) + " " + data.substring(7, 10) + deci;
                    break;
                case 11:
                    out = data.substring(0, 2) + " " + data.substring(2, 5) + " " + data.substring(5, 8) + " " + data.substring(8, 11) + deci;
                    break;
                case 12:
                    out = data.substring(0, 3) + " " + data.substring(3, 6) + " " + data.substring(6, 9) + " " + data.substring(9, 12) + deci;
                    break;
                case 13:
                    out = data.substring(0, 1) + " " + data.substring(1, 4) + " " + data.substring(4, 7) + " " + data.substring(7, 10) + " " + data.substring(10, 13) + deci;
                    break;
                case 14:
                    out = data.substring(0, 2) + " " + data.substring(2, 5) + " " + data.substring(5, 8) + " " + data.substring(8, 11) + " " + data.substring(11, 14) + deci;
                    break;
                case 15:
                    out = data.substring(0, 3) + " " + data.substring(3, 6) + " " + data.substring(6, 9) + " " + data.substring(9, 12) + " " + data.substring(12, 15) + deci;
                    break;
            }
        } else {
            out = data;
        }
        return out;
    }
}

So as you would see, if you type 1250 the output in the textfield would be 12501 250 and i would like to only have 1 250

READ ALSO
How file manipulations perform during power outage

How file manipulations perform during power outage

Linux machine, Java standalone application

30
How can I make one flowfile the child of a separate pre-existing flowfile?

How can I make one flowfile the child of a separate pre-existing flowfile?

When a flowfile comes into existence from an external source, but knows the uuid of an prior flowfile that it came from, how can I mark the existing flowfile as a parent of this one

58
Open PDF in new tab, saving file gives wrong file name

Open PDF in new tab, saving file gives wrong file name

There is an Angular 6 app having a feature to download pdf filesIt uses REST API from another Spring Boot application to download files

62
Jpa return more than one result although only one row is in database

Jpa return more than one result although only one row is in database

I am running different JPA queries in the form of

45