簡體   English   中英

如何強制JFormattedTextField值動態更改

[英]How to force JFormattedTextField value to change dynamically

我想在JFormattedTextField輸入數字時看到格式化的數字。 例如,當用戶鍵入124451000時,它必須在屏幕上動態顯示為124.451.000,而不是在用戶按Tab或Enter鍵時顯示。

我想在輸入第四個數字時在第三個數字之后輸入一個點(例如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);
}


 }

好吧,我嘗試了這段代碼,無法運行,另一個類中的My JFormattedTextField,我無法在另一個類中獲得成功,我無法訪問另一個類中的對象。

在這種情況下添加DocumentListener會有所幫助嗎?

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

創建對JFormattedTextField的引用的偵聽器,並在偵聽器中進行格式化。

這段代碼有什么問題。我無法更改JformattedTextfield的格式。 我使用不同的課程

我的主班

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
public class MainClass extends JFrame {
private JPanel contentPane;
private JFormattedTextField MyTextField;
public static void main(String[] args) {
    new MainClass();
}
public MainClass() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
    MyTextField=new JFormattedTextField();
    MyTextField.setColumns(20);
    MyTextField.getDocument().addDocumentListener(new MyDocumentListener());
    contentPane.add(MyTextField);
    this.setVisible(true);
  }
 }

MyDocumentListener類別

import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.NumberFormatter;
public class MyDocumentListener implements DocumentListener{
    NumberFormat fmt;
    public void insertUpdate(DocumentEvent e) {
    fmt = NumberFormat.getCurrencyInstance(Locale.GERMAN);
    MainClass.MyTextField.setFormat(fmt);//I get error in this this line.
                                               //MyTextField is invisible here
}
public void removeUpdate(DocumentEvent e) {
    //
}
public void changedUpdate(DocumentEvent e) {
    //
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM