Search code examples
javaswingfractionsjformattedtextfield

How to set format to a JFormattedTextField


I want to use a JFormattedTextField that can input fraction numbers. As Java doc specifies its format should set within the constructor. But I’m using NetBeans IDE, the constructor of the JFormattedTextField is a generated code , so I don’t know how to specify a format.

And if you can please direct me to a helpful tutorial.


Solution

  • To customize the creation code of a Swing element in NetBeans, right-click the element and select Customize code.... In the following pop-up, click the drop-down menu next to the code snippet and select Custom creation. Then you can specify the code yourself as such:

    DecimalFormat df = DecimalFormat.getInstance();
    // do some manipulation to the DecimalFormat object if desired
    jFormattedTextField = new javax.swing.JFormattedTextField(df);