How to find an exact string, but it is not a substring in JtextPane

advertisements

I had write a code for find a given string.It is find out the given word,it may be a substring of word or a word.But,I want to find the exact word not substring that word.For example SearchExactword is a one word,I search for string as word.If word is available in text it will highlight but not highlight the substring of word(SearchExactword).Please help me,Thank you.

My code is:

public class FindAWord extends javax.swing.JFrame {

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new FindAWord().setVisible(true);
        }
    });
}
private javax.swing.JScrollPane scrollPane;
private javax.swing.JTextField searchText;
private javax.swing.JTextPane textPane;
private javax.swing.JButton search;

public FindAWord() {
    initComponents();
}

public void highLight(JTextComponent component, String patteren) {
    try {
        Document doc = component.getDocument();
        String text = component.getText(0, doc.getLength());
        int pos = component.getCaretPosition();
        if (pos == doc.getLength()) {
            pos = 0;
        }
        int index = text.toUpperCase().indexOf(patteren.toUpperCase(), pos);
        int start = Utilities.getWordStart(component, index);
        int end = Utilities.getWordEnd(component, index + patteren.length());

        int patterenLn=patteren.length();
        int diff=end-start;
        if (index >= 0) {
            if((start==index)&&(end==index+patterenLn+1)){
            component.setSelectionStart(index);
            component.setSelectionEnd(index + patteren.length());
            component.getCaret().setSelectionVisible(true);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@SuppressWarnings("unchecked")

//
private void initComponents() {

    search = new javax.swing.JButton();
    searchText = new javax.swing.JTextField();
    scrollPane = new javax.swing.JScrollPane();
    textPane = new javax.swing.JTextPane();
    searchText.setText("test");
    textPane.setText("test qweqw test asdasdas test");

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    search.setText("Search");
    search.setFocusable(false);
    search.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            searchActionPerformed(evt);
        }
    });

    scrollPane.setViewportView(textPane);

    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(36, 36, 36)
            .addComponent(search, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(32, 32, 32)
            .addComponent(searchText, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(114, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(scrollPane)
            .addContainerGap()));
    layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addGap(36, 36, 36)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
            .addComponent(search)
            .addComponent(searchText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)));

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

private void searchActionPerformed(java.awt.event.ActionEvent evt) {
    highLight(textPane, searchText.getText());
}

}


Use javax.swing.text.Utilities. It has 2 methods

public static final int getWordStart(JTextComponent c, int offs)
public static final int getWordEnd(JTextComponent c, int offs)

When a match is found you can check found word start and end. If it's the same as found string start and end offsets then exact match (whole word) is found