0

I want to see a formatted number when I enter a number into a JFormattedTextField. For example, when a user typed in 124451000, it must be seen as 124.451.000 onscreen dynamically and not when a user presses Tab or Enter key.

I want to put a dot when the fourth number has been typed in, right after the third number (for example, like 2.566).


public class MyDocListener implements DocumentListener{
     NumberFormat nf = NumberFormat.getIntegerInstance(Locale.GERMAN); 
     NumberFormatter nff = new NumberFormatter(nf);
     DefaultFormatterFactory factory = new DefaultFormatterFactory(nff);
     final String newline = "\n";

    public void insertUpdate(DocumentEvent e) {
        updateLog(e, "inserted into");
        NumberFormat numberFormatter = new DecimalFormat("#,###,###"); 
        // I put this code to change the format.
    }
    public void removeUpdate(DocumentEvent e) {
        updateLog(e, "removed from");
    }
    public void changedUpdate(DocumentEvent e) {
        //Plain text components don't fire these events.
    }

    public void updateLog(DocumentEvent e, String action) {
    Document doc = (Document)e.getDocument();
    int changeLength = e.getLength();
    displayArea.append(
        changeLength + " character" +
        ((changeLength == 1) ? " " : "s ") +
        action + doc.getProperty("name") + "." + newline +
        "  Text length = " + doc.getLength() + newline);
}


 }

Well I tried this code, I could not run, My JFormattedTextField in another class, I could not succced in another class.I could not access to object which is in another class.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Alify
  • 59
  • 1
  • 7

1 Answers1

0

Would adding a DocumentListener help in this situation?

http://docs.oracle.com/javase/7/docs/api/javax/swing/event/DocumentListener.html

Create the Listener with a reference to the JFormattedTextField, and do your formatting within the Listener.

EyeOfTheHawks
  • 576
  • 1
  • 5
  • 16
  • Well I did it but I could not change the text in JFormattedTextField on writing time – Alify Sep 26 '13 at 21:56
  • NumberFormat numberFormatter; public class MyDocumentListener implements DocumentListener{ public void insertUpdate(DocumentEvent e) { numberFormatter= new DecimalFormat("#,###,###"); } } myJFormattedTextFrield.getDocument().addDocumentListener(new MyDocumentListener()); – Alify Sep 26 '13 at 22:56