-1

I have made a program, it has show file option, means I have a txt file in which some information is store and when I click on show button it shows in a JTextArea.

I have one problem - I have make a JTextField and wanted to write something in that text field and have it search for that word in the text area.

The problem is I found codes with highlighter which highlight the word, but I want code when I write showing in that search box, the text area automatically scroll down to that word, but don't cancel words around that search word.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ahmed Ali
  • 1
  • 1

1 Answers1

1

This question has already been answered here: Java - Scroll to specific text inside JTextArea

Once you have the position pos, you can scroll the JTextField to it using this:

// Get the rectangle of the where the text would be visible...
Rectangle viewRect = textArea.modelToView(pos);
// Scroll to make the rectangle visible
textArea.scrollRectToVisible(viewRect);
Dmich
  • 130
  • 6