2013-05-16 24 views
0

如何使用JEditorPane获取选定文本的索引?使用JEditorPane获取选定文本的索引

我想在编辑器窗格中获取选定文本的起始和结束索引。

当我在编辑器窗格中选择文本时,编辑器窗格返回的索引与实际HTML文件中字符串的索引不同。当我选择单行文本时,两个索引都是相同的,但如果我选择多行,则索引是不同的。

我正在使用以下代码。

int start = editorpane.getSelectionStart(); 
int end = editorpane.getSelectionEnd(); 
JOptionPane.showMessageDialog(null, "Start index "+ start); 
JOptionPane.showMessageDialog(null, "End index "+end); 

它返回的索引较小。

当使用下面的代码它返回实际的索引,但我想在上面的代码相同的索引。

try 
    { 
    File f1= new File("path of the file"); 
    FileReader fin= new FileReader(f1); 
    BufferedReader br= new BufferedReader(fin); 
    String s=br.readLine(); 
    String con=""; 
    while(s!=null) 
    { 
    con=con+s; 
    s=br.readLine(); 

    } 
    int l=con.lastIndexOf("CASE CITED"); 

    System.out.println("index="+l); 


    } 
    catch(Exception ex){} 

}    
+1

你在你的最后一个问题,你会“照顾”,并称在需要大写字母表示。这个问题没有表明这一点,甚至更糟糕。 -1这两个问题。 –

+0

这个问题需要大写的地方 –

+1

试着看[编辑](http://stackoverflow.com/posts/16585264/revisions)! –

回答

3

使用editorpane.getSelectionStart()/getSelectionEnd()

+0

这将返回编辑器窗格的索引,但我希望根据编辑器窗格中显示的html文件指定索引。 –

+0

convert查看文档 – mKorbel

+0

什么是“根据html文件的索引”? – StanislavL