Getting the visible text in a JEditorPane

僤鯓⒐⒋嵵緔 提交于 2019-12-19 04:46:47

问题


I have a JeditorPane in a JScrollPane. At certain points in the application, I would like to retrieve the text that is visible in the scrollPane (the text that is currently showing) and only this text. Is there a way to do this?

Thank you,

Elliott


回答1:


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.



来源:https://stackoverflow.com/questions/4628674/getting-the-visible-text-in-a-jeditorpane

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