Getting the visible text in a JEditorPane

前端 未结 1 1541
情话喂你
情话喂你 2021-01-07 08:56

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 currentl

相关标签:
1条回答
  • 2021-01-07 09:12

    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.

    0 讨论(0)
提交回复
热议问题