Jaskaranbir Singh
Jaskaranbir Singh

Reputation: 2034

Implement regex into JFormattedTextField

I would like to use regex patterns in jFormattedTextField (or if possible in JTextField, doesnt really matter if the job is done). I know about using MaskFormatter and DocumentFilter but I was wondering if it was possible by using regex pattern.

Here's the code I tried:

import java.awt.GridLayout;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

public class Test1 {
    public static void main(String args[]) {
        JFrame frame = new JFrame();
        String regex = "[a-z]";
        Pattern pt = Pattern.compile(regex);
        Matcher r = pt.matcher("(.*)([a-z])");
        JFormattedTextField ft = new JFormattedTextField(pt);

        JTextField testField = new JTextField();
        ft.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                if (!r.find()) {
                    testField.setText("not found");
                } else
                    testField.setText("found");
            }
        });
        frame.setLayout(new GridLayout(2, 1));
        frame.add(ft);
        frame.add(testField);
        frame.pack();
        frame.setVisible(true);
    }
}

But it doesnt work (keeps displaying "not found"). Is there some way to do this? Because I feel more comfortable using regex since I have been practicing it for a while.

Upvotes: 1

Views: 1611

Answers (2)

Bohemian
Bohemian

Reputation: 424973

You don't need a Matcher at all; String has the matches() method that is more convenient.

Assuming "found" means finding a letter in the input:

ft.addCaretListener(new CaretListener() {
    public void caretUpdate(CaretEvent e) {
        if (ft.getText().matches(".*[a-z].*"))
            testField.setText("not found");
        else
            testField.setText("found");
    }
});

Or if you prefer you can use a ternary to express it in one line:

testField.setText((ft.getText().matches(".*[a-z].*") ? "" : "not ") + "found");

Upvotes: 2

Ya Wang
Ya Wang

Reputation: 1808

I am not sure why you are searching your pattern for matches are you trying to do the following?

import java.awt.GridLayout;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

public class Test1 {

    public static void main(String args[]) {
        JFrame frame = new JFrame();
        String regex = "[a-z]";
        Pattern pt = Pattern.compile(regex);
        JFormattedTextField ft = new JFormattedTextField(pt);

        JTextField testField = new JTextField();
        ft.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {

                Matcher r = pt.matcher(ft.getText());
                if (!r.find()) {
                    testField.setText("not found");
                } else
                    testField.setText("found");
            }
        });
        frame.setLayout(new GridLayout(2, 1));
        frame.add(ft);
        frame.add(testField);
        frame.pack();
        frame.setVisible(true);
    }

}

Upvotes: 2

Related Questions