user2771059
user2771059

Reputation: 308

identify line in a jtextpane when the row number is entered

           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

Upvotes: 0

Views: 359

Answers (2)

camickr
camickr

Reputation: 324098

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.

Upvotes: 3

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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

Upvotes: 1

Related Questions