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.
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.
What is wrong with this code.I can't change JformattedTextfield's format. I use different classes
My main class
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 class
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) {
//
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.