2013-06-12 47 views
0

我使用jEditorPane行事有點像一個編輯器,誰要求我這樣做,也需要一個查找替換搜索...所以我想讓他選擇查找和然後根據需要進行更換。爪哇,選擇文本恰好在JEditorPane

所以我提供了這個簡單的代碼:

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {           
    int start = (jEditorPane1.getSelectionStart()>jEditorPane1.getSelectionEnd())?jEditorPane1.getSelectionEnd():jEditorPane1.getSelectionStart(); 

    int max = (start>jEditorPane1.getSelectionStart())?start:jEditorPane1.getSelectionStart(); 

    String searchWord = jTextField3.getText(); 
    int searchIndex = jEditorPane1.getText().indexOf(searchWord, max); 
    if(searchIndex != -1){ 
     jEditorPane1.select(searchIndex, searchIndex+searchWord.length()); 
    } 
    else{ 
     jEditorPane1.setSelectionStart(-1); 
     jEditorPane1.setSelectionEnd(-1); 
    } 
}  

fortunatelly的應用程序返回良好​​指標,但可惜的是在視圖窗口我看沒有選擇。

我也是新來的Java,請幫助


PS。通過NetBeans自身也


提供按鈕操作的樣本的你請

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/* 
* test.java 
* 
* Created on Jun 12, 2013, 8:23:19 PM 
*/ 
package wordchecker; 

/** 
* 
* @author Hassan 
*/ 
public class test extends javax.swing.JFrame { 

    /** Creates new form test */ 
    public test() { 
     initComponents(); 
    } 

    /** This method is called from within the constructor to 
    * initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is 
    * always regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextPane1 = new javax.swing.JTextPane(); 
     jTextField1 = new javax.swing.JTextField(); 
     jButton1 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jScrollPane1.setViewportView(jTextPane1); 

     jTextField1.setText("jTextField1"); 

     jButton1.setText("jButton1"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(24, 24, 24) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
         .addComponent(jButton1)) 
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jButton1)) 
       .addContainerGap(14, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold> 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
     int startFrom = jTextPane1.getSelectionStart(); 
     if(jTextPane1.getSelectionStart() == jTextPane1.getSelectionEnd()){ 
      startFrom = -1; 
     } 

     String searchWord = jTextField1.getText(); 
     int searchIndex = jTextPane1.getText().indexOf(searchWord, startFrom); 
     if(searchIndex != -1){ 
      jTextPane1.select(searchIndex, searchIndex+searchWord.length()); 
     } 
     else{ 
      jTextPane1.setSelectionStart(0); 
      jTextPane1.setSelectionEnd(0); 
     } 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       new test().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    private javax.swing.JButton jButton1; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTextField jTextField1; 
    private javax.swing.JTextPane jTextPane1; 
    // End of variables declaration 
} 
+1

'我正在uning的JEditorPane爲一些editor'還挺行動 - 不使用一個JEditorPane。它旨在顯示HTML。而是使用JTextPane,因此您不必擔心HTML。發佈證明你的問題的[SSCCE](http://sscce.org/)。 – camickr

+0

我使用純文本,但我也測試, –

+0

我將其更改爲jTextPane,但它再次沒有工作.... 我張貼在一個答案全班,似乎在這裏我有限帖子 –

回答

2

文本組件的選擇當文本組件具有焦點時,纔會顯示。當你點擊按鈕時,它獲得焦點,所以你看不到文字選擇。你可以使按鈕不可聚焦,或者你可以添加以下到您的actionPerformed()方法的底部:

jTextPane1.requestFocusInWindow(); 

此外,不使用的getText()方法。這會導致偏移問題。

int searchIndex = jTextPane1.getText().indexOf(searchWord, startFrom); 

有關更多信息和解決方案,請參閱Text and New Lines。這個環節的基礎是使用:

int length = textPane.getDocument().getLength(); 
String text = textPane.getDocument().getText(0, length); 

以上僅返回「\ n」作爲EOL字符串,所以當你做一個搜索的偏移量將會匹配,然後選擇文本。

編輯:

我一般使用類似下面的代碼:

int startFrom = jTextPane1.getCaretPosition(); 
+0

謝謝你,我雖然你離開了,所以我去嘗試其他的東西希望它的工作,像混合熒光筆和脫字符號,但因爲它處理\ n和其他逃逸2字符,他們沒有匹配: 一個問題,我做了要求重點,不可聚焦你告訴我,但是我的選擇不移動到下一個找到的位置是:'( 什麼ü想我錯過 –

+0

我記住這爲答案已經和去?嘗試其他選擇,但我希望你仍然想看看:*謝謝 –

+0

'我雖然你離開了' - 是的,我們都有其他的事情要做,所以你應該總是發佈一個正確的SSCCE與原始如果我們有空閒時間,你可以馬上得到一個完整的答案,你永遠不會知道我們什麼時候會有空閒時間,但是我的選擇不會移動到下一個找到的位置 - 見我的編輯 – camickr