0

I developing some calculator, so in the JTextField above can have only some characters.
What is the best way to make that happened?

Let's say that I have this char[] values = 0,1,2,3,4,5,6,7,8,9,+,-,*,/,(,),., which those are the values which the user can type.

Nivetha T
  • 481
  • 1
  • 3
  • 17
roeygol
  • 4,908
  • 9
  • 51
  • 88

3 Answers3

3

Use a JFormattedTextField. You can use a MaskFormatter with the setValidCharacters(...) method and specify a String containing valid characters.

Read the section from the Swing tutorial on Using a MaskFormatter for more information.

Or the other approach is to use a JTextField with a DocumentFilter. Read the Swing tutorial on Implementing a DocumentFilter for more information.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • @RoeyGolzarpoor, You can search the forum or the web for code that uses that method. I'm sure you will find an example. – camickr Jan 28 '15 at 15:55
2

There are already several solved questions like yours:

Filter the user's keyboard input into JTextField (swing)

No blanks in JTextField

According to these, you should use a DocumentFilter or a JFormattedTextField.

Community
  • 1
  • 1
wyr0
  • 103
  • 1
  • 13
  • I can't find some example which is using `char[]` as a valid input – roeygol Jan 28 '15 at 15:46
  • Well, if you choose to use the DocumentFilter, you could verify for every character typed by the user if this is contained or not in your char array. – wyr0 Jan 28 '15 at 16:00
  • Try to take a look at this for a good example about how use it: http://stackoverflow.com/questions/24844559/jtextfield-using-document-filter-to-filter-integers-and-periods – wyr0 Jan 28 '15 at 16:06
0

Eventually I managed to create my own JTextField which implemented like this:

public class MyTextField extends JTextField {

    //final value for maximum characters
    private final int MAX_CHARS = 20;

    /**
     * A regex value which helps us to validate which values the user can enter in the input
     */
    private final String REGEX = "^[\\d\\+\\/\\*\\.\\- \\(\\)]*$";

    public MyTextField(){

        //getting our text as a document
        AbstractDocument document = (AbstractDocument) this.getDocument();        

        /**
         * setting a DocumentFilter which helps us to have only the characters we need
         */
        document.setDocumentFilter(new DocumentFilter() {
            public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException {

                String text = fb.getDocument().getText(0, fb.getDocument().getLength());
                text += str;

                if ((fb.getDocument().getLength() + str.length() - length) <= MAX_CHARS && text.matches(REGEX)){
                    super.replace(fb, offs, length, str, a);
                } 
            }

            public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {

                String text = fb.getDocument().getText(0, fb.getDocument().getLength());
                text += str;

                if ((fb.getDocument().getLength() + str.length()) <= MAX_CHARS && text.matches(REGEX)){
                    super.insertString(fb, offs, str, a);
                }
            }
        });
    }

}
roeygol
  • 4,908
  • 9
  • 51
  • 88