Getting the visible text in a JEditorPane

霸气de小男生 提交于 2019-12-01 01:39:21
camickr

You can use the viewport to get the view position and size.

JViewport viewport = scrollPane.getViewport();
Point startPoint = viewport.getViewPosition();
Dimension size = viewport.getExtentSize();
Point endPoint = new Point(startPoint.x + size.width, startPoint.y + size.height);

Once you know the start/end points of the viewport you can use:

int start = editorPane.viewToModel( startPoint );
int end = editorPane.viewToModel( endPoint );

Once you know the offsets of the text you want you can get the text from the component:

String text = editorPane.getText(start, end - start);

None of the code is tested.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!