Java Swing DefaultFormatterFactory create

Description

Java Swing DefaultFormatterFactory create


import java.awt.FlowLayout;
import java.math.BigDecimal;
import java.text.DecimalFormat;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.DefaultFormatter;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;


public class Main extends JFrame {
  public Main() {
    super("JButton");

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    JLabel nameLabel = new JLabel("Value:");
    /*  w ww  . j  a va2  s .  c  om*/
    JFormattedTextField name = new JFormattedTextField(new BigDecimal(123.123));
    DefaultFormatter fmt = new NumberFormatter(new DecimalFormat("#.0###############"));
    DefaultFormatterFactory fmtFactory = new DefaultFormatterFactory(fmt, fmt,fmt);
    
    name.setFormatterFactory(fmtFactory);
    
    JTextField text = new JTextField("Click here to see the validation result");

    getContentPane().add(nameLabel);
    getContentPane().add(name);
    getContentPane().add(text);

  }


  public static void main(String[] args) {
    Main frame = new Main();
    frame.pack();
    frame.setVisible(true);
  }
}



PreviousNext

Related