6 Replies Latest reply on Aug 9, 2005 3:35 AM by 843804

    Searching text in files & highlighting that  text

    843804
      How to serach text files and highlighting the text?

      PLZ help me regarding this.

      Regards
        • 1. Re: Searching text in files & highlighting that  text
          camickr
          What is exactly your problem:

          a) Searching - use String.indexOf(....);
          b) Highlighting = use JTextComponent methods setCaretPosition(...) followed by a call to moveCaretPosition(..).
          • 2. Re: Searching text in files & highlighting that  text
            843804
            Here is some simple find function:
            /*
             * Test_Find.java
             */
            import java.awt.*;
            import java.awt.event.*;
            import javax.swing.*;
            import javax.swing.text.*;
            public class Test_Find extends JFrame {
                public Test_Find() {
                    initComponents();
                    setExtendedState(JFrame.MAXIMIZED_BOTH);
                    textarea.setText(initialText);
                    textarea.setCaretPosition(0);
                    setVisible(true);
                    textarea.requestFocusInWindow();
                }
                private void initComponents() {
                    scrollTextarea = new JScrollPane();
                    textarea = new JTextArea();
                    toolbar = new JToolBar();
                    findB = new JButton();
                    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                    setTitle("Test Find");
                    scrollTextarea.setViewportView(textarea);
                    getContentPane().add(scrollTextarea, BorderLayout.CENTER);
                    toolbar.setFloatable(false);
                    findB.setText("Find");
                    findB.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            findBActionPerformed(evt);
                        }
                    });
                    toolbar.add(findB);
                    getContentPane().add(toolbar, BorderLayout.NORTH);
                }
                private void findBActionPerformed(ActionEvent evt) {
                    String input = JOptionPane.showInputDialog(
                            "Find what:", pattern);
                    if(input==null){
                        input="";
                    }
                    pattern=input;
                    doFind();
                }
                private int doFind(){
                    textarea.requestFocusInWindow();
                    currentCaretPosition = textarea.getCaretPosition();
                    text = textarea.getText();
                    if(!pattern.equals("")){
                        int pos=0;
                        pos = text.toLowerCase().indexOf(pattern.toLowerCase(), currentCaretPosition);
                        if(pos>-1){
                            scrollTextarea.getHorizontalScrollBar().setValue(0);
                            textarea.setCaretPosition(pos+pattern.length());
                            highlight(textarea, pattern);
                            return 1;//found
                        }
                    }else{
                        removeHighlights(textarea);
                        return 0;//no pattern
                    }
                    return -1;//not found
                }
                // Creates highlights around all occurrences of pattern in textComp
                public void highlight(JTextComponent textComp, String pattern) {
                    // First remove all old highlights
                    removeHighlights(textComp);
                    try {
                        Highlighter hilite = textComp.getHighlighter();
                        Document doc = textComp.getDocument();
                        String text = doc.getText(0, doc.getLength());
                        int pos = 0;
                        // Search for pattern
                        while ((pos = text.toLowerCase().indexOf(pattern.toLowerCase(), pos)) >= 0) {
                            // Add highlight around pattern using private painter
                            hilite.addHighlight(pos, pos+pattern.length(), textPainter);
                            pos += pattern.length();
                        }
                    } catch (BadLocationException e) {
                    }
                }
                // Removes only our private highlights
                public void removeHighlights(JTextComponent textComp) {
                    Highlighter hilite = textComp.getHighlighter();
                    Highlighter.Highlight[] hilites = hilite.getHighlights();
                    for (int i=0; i<hilites.length; i++) {
                        if (hilites.getPainter() instanceof TextPainter) {
            hilite.removeHighlight(hilites[i]);
            }
            }
            }
            // An instance of the private subclass of the default highlight painter
            private Highlighter.HighlightPainter textPainter = new TextPainter(Color.yellow);
            // A private subclass of the default highlight painter
            class TextPainter extends DefaultHighlighter.DefaultHighlightPainter {
            public TextPainter(Color color) {
            super(color);
            }
            }
            public static void main(String args[]) {
            new Test_Find();
            }
            private String input, pattern, text;
            private int currentCaretPosition;
            private JButton findB;
            private JScrollPane scrollTextarea;
            private JTextArea textarea;
            private JToolBar toolbar;
            private String initialText =
            "Swing JTable and JScrollPane Problem\n" +
            " tlloreti 4 Jul 5, 2005 4:15 PM\n" +
            "by Tom.Sanders \u00bb \n" +
            "Swing CTRL-A (select all) ignored in applet\n" +
            " ilmbeachgirl 4 Jul 5, 2005 4:10 PM\n" +
            "by ilmbeachgirl \u00bb \n" +
            "Swing drawing problem\n" +
            " gnosnahz 0 Jul 5, 2005 4:09 PM\n" +
            "by gnosnahz \u00bb \n" +
            "Java Event Handling events effecting panels in multiple tabs\n" +
            " sean_hagen 1 Jul 5, 2005 4:06 PM\n" +
            "by es5f2000 \u00bb \n" +
            "Swing Searching text in files & highlighting that text\n" +
            " sketty 1 Jul 5, 2005 3:57 PM\n" +
            "by camickr \u00bb \n" +
            "Swing JTextField displaying prtially its contents\n" +
            " SreeJay 1 Jul 5, 2005 3:55 PM\n" +
            "by camickr \u00bb \n" +
            "Swing how to get BufferedImage of a JPanel\n" +
            " praveen.Arora 1 Jul 5, 2005 3:53 PM\n" +
            "by camickr \u00bb \n" +
            "Swing Creating a form from a xml schema..\n" +
            " zoltern 0 Jul 5, 2005 3:51 PM\n" +
            "by zoltern \u00bb \n" +
            "Swing Da Suckers: Java and Windows XP\n" +
            " selamiski@sahinski.com 1 Jul 5, 2005 3:42 PM\n" +
            "by selamiski@sahinski.com \u00bb \n" +
            "Java 2D Rescaling using JAI\n" +
            " G-Rehuku 0 Jul 5, 2005 3:30 PM\n" +
            "by G-Rehuku \u00bb \n" +
            "Swing how to get Page Preview of multiple page JPanel\n" +
            " Sonu.Arora 0 Jul 5, 2005 2:20 PM\n" +
            "by Sonu.Arora \u00bb \n" +
            "Swing IDE for Swing Development\n" +
            " JavaEnthusiastt 8 Jul 5, 2005 2:17 PM\n" +
            "by uhrand \u00bb \n" +
            "Java 2D Can't connect to X11 window server\n" +
            " kusigubi 0 Jul 5, 2005 2:06 PM\n" +
            "by kusigubi \u00bb \n";
            }
            • 3. Re: Searching text in files & highlighting that  text
              843804
              if (hilites.getPainter() instanceof TextPainter) {
              Not Found getPainter() method.

              How to find text in the upward direction?
              • 4. Re: Searching text in files & highlighting that  text
                camickr
                Not Found getPainter() method.
                There is a bug in the forum. All [ i] are treated as an HTML italic tag.

                if (hilites.getPainter() instanceof TextPainter)

                So when the above line got rendered the [ i] was removed from the code. The actual code should be:

                if (hilites[ i].getPainter() instanceof TextPainter) {
                How to find text in the upward direction
                In the forward direction the String.indexOf(...) method was used. So read the String API for a method that will search backwards.
                • 5. Re: Searching text in files & highlighting that  text
                  843804
                  friends,

                  also because of the bug in the forum,

                  this line: "hilite.removeHighlight(hilites);" should be

                  "hilite.removeHighlight(hilites);"

                  regards,
                  james
                  • 6. Re: Searching text in files & highlighting that  text
                    843804
                    sorry, it should be
                    "hilite.removeHighlight(hilites[ i]);"

                    regards,
                    james