Search code examples
javaswingjtextfield

JNumberTextField: Why is not work


but i read about it on youtube and how to set JTextField as a JNumberTextField . because i want input only number. If anyone press other key then char not show in text field

import javax.swing.*;
    public class example {
        public static void main(String aa[])
        {
            JNumberTextField num = JNumberTextField();
            num.setColumns(15);
            num.setFormat(JNumberTextField.DECIMAL);
            num.setPrecision(4);
            num.setAllowNegative(false);

            JFrame f = new JFrame();
            f.add(num);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(200,200);
            f.setVisible(true);
        }

    }

I am using JNumberTextField because of i want input only number but it is not work. i am reading about it on youtube tutorials

error is

loop.java:5: cannot find symbol
symbol  : class JNumberTextField
location: class example
        JNumberTextField num = JNumberTextField()
        ^
loop.java:5: cannot find symbol
symbol  : method JNumberTextField()
location: class example
        JNumberTextField num = JNumberTextField()
                               ^
loop.java:7: cannot find symbol
symbol  : variable JNumberTextField
location: class example
        num.setFormat(JNumberTextField.DECIMAL);

They are using there own custom class.


Solution

  • They are using there own custom class. and if you want only input number then use formated textfield