identify line in a jtextpane when the row number is entered

好久不见. 提交于 2019-12-11 18:02:05

问题


           public static void setJTextPaneFont(JTextPane jtp, Color c, int start_index,int end_index) {

    MutableAttributeSet attrs = jtp.getInputAttributes();
    StyleConstants.setForeground(attrs, c);
    StyledDocument doc = jtp.getStyledDocument();
    doc.setCharacterAttributes(start_index, end_index, attrs, false);
}

i created above code to change the forground of of specific word when i enter the start ndex and end index.But now i need to change the the forground when i pass the row number,start_index, and end index.Can you help me with this.How i identify a specific line when i enter the row number.

     public void gotoStartOfLine(JTextComponent component, int line) {
           Element root = component.getDocument().getDefaultRootElement();
           line = Math.max(line, 1);
           line = Math.min(line, root.getElementCount());
           component.setCaretPosition(root.getElement(line - 1).getStartOffset());
     }

i tried above code to go to specific row.but it didint work


回答1:


How i identify a specific line when i enter the row number.

I think you mean you want the offset of the text for the given row. If so then take a look at the gotoStartOfLine() method from Text Utilities.

That is the code that sets the caret position will give you the starting offset of the line. Then you just add the start/end values to get the offsets of the text to highlight.




回答2:


Look at using the javax.swing.text.Utilities class, especially the getRowStart(...) and getRowEnd(...) methods.



来源:https://stackoverflow.com/questions/18754940/identify-line-in-a-jtextpane-when-the-row-number-is-entered

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