Highlight all words in jTextField that the user puts into search for Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Highlight all words in jTextField that the user puts into search for without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Hey all the following code below works with highlighting the supplied word to search for but if there are more than just one (1) of that word in the sentence then it wont find it. It just stops at finding one (1) match.

private JFrame frame;
private static JTextField textField;
private static JTextField txtWorld;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                CaretDemo window = new CaretDemo();
                window.frame.setVisible(true);

                String text = "hello world. How are you?";

                textField.setText(text);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public CaretDemo() {
    initialize();
}

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    textField = new JTextField();
    textField.setBounds(21, 11, 350, 36);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    txtWorld = new JTextField();
    txtWorld.setColumns(10);
    txtWorld.setBounds(21, 156, 350, 36);
    frame.getContentPane().add(txtWorld);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Highlighter highlighter = textField.getHighlighter();
            HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.pink);          
            int p0 = textField.getText().indexOf(txtWorld.getText());
            int p1 = p0 + txtWorld.getText().length();

            try {
                highlighter.removeAllHighlights();
                highlighter.addHighlight(p0, p1, painter);
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
        }
    });

    btnNewButton.setBounds(221, 203, 128, 29);
    frame.getContentPane().add(btnNewButton);
}

The above code looks like this when ran and supplied the word “world” to find.

enter image description here

But now if I add to the sentence and add another “world” then hit the button this is what it looks like:

enter image description here

As you see, it still has the same highlighted word without also highlighting the second (2) “world”.

UPDATE 1

I have attempted using regex in order to loop and find the needed word.

btnNewButton_3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        System.out.println("hi");
        String theSentence = txtTheSentence.getText();
        String WordToFind = txtWordToFind.getText();
        Highlighter h = txtWordToFind.getHighlighter();

        Pattern pattern = Pattern.compile("\b"+WordToFind+"\b", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(theSentence);

        if(matcher.find()){
            String extractedWord = matcher.group(0);
            System.out.println(extractedWord);
        }
    }
});

But this does not produce even one word found.

Answer

Got it finally!

btnNewButton_3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        System.out.println("hi");
        String theSentence = txtTheSentence.getText();
        String theWord = txtWordToFind.getText();
        Highlighter h = txtWordToFind.getHighlighter();
        Pattern pattern = Pattern.compile(theWord, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(theSentence);

        while (matcher.find()) {
            String extractedWord = matcher.group();
            System.out.println(extractedWord);
        }
}
We are here to answer your question about Highlight all words in jTextField that the user puts into search for - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji