2013-05-16 28 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