Java - Scroll to specific text inside JTextArea

前端 未结 4 2105
生来不讨喜
生来不讨喜 2020-11-27 07:51

I\'m trying to implement a feature inside the current program that I\'m writing and I wanna learn how to scroll down to specific text inside a JTextArea. For example, lets s

4条回答
  •  [愿得一人]
    2020-11-27 08:37

    Add on to the comment by MadProgrammer:

    scrollRectToVisible(viewRect) is deprecated as of Java SE9, and it has been replaced by scrollRectToVisible2D(viewRect)

    The proper way to get the text to display without using deprecated functions would be:

    java.awt.geom.Rectangle2D view = area.modelToView2D(pos); // View where pos is visible
    area.scrollRectToVisible(view.getBounds()); // Scroll to the rectangle provided by view
    area.setCaretPosition(pos); // Sets carat position to pos
    

提交回复
热议问题