Java JFormattedTextField number field for float type

Description

Java JFormattedTextField number field for float type

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

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

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

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    JLabel nameLabel = new JLabel("Value:");
    //from w ww  .  j a v  a  2 s .com
    JFormattedTextField name = new JFormattedTextField(new DecimalFormat("#.0"));
    name.setValue(new Float(123.4F));

    Float value = (Float) name.getValue();
    System.out.println(value);
    
    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