Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top