Search code examples
javajformattedtextfield

how to set a jformattedtextfield to take only 2 values?


i know how to set a jformattedtextfield to allow the user to write only numbers, letters...but i want that a user can write only two values: 1 or 0. How i can do that. I wrote this:

MaskFormatter mf6;
    try {
        mf6 = new MaskFormatter("#");
        txtformatted = new javax.swing.JFormattedTextField(mf6);
    } catch (ParseException e) {
        //e.printStackTrace();
    }

Solution

  • i want that a user can write only two values: 1 or 0.

    Check out the setValidCharacters(...) method of the MaskFormatter class.