highlighting text

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tussom
    New Member
    • Mar 2007
    • 16

    highlighting text

    Hello

    I want to highlight a text in a text area that matches a pattern written in a text field

    help me
    i need it asap
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by tussom
    Hello

    I want to highlight a text in a text area that matches a pattern written in a text field

    help me
    i need it asap
    you would
    (1) read the pattern from the TextField using getText()
    (2) read the text from the TextArea using getText()
    (3) serarch for the start and end of the pattern using getIndex()
    (4) if found use setSelectionSta rt() and setSelectionEnd () to highlight the text in the TextArea

    Comment

    • tussom
      New Member
      • Mar 2007
      • 16

      #3
      Originally posted by horace1
      you would
      (1) read the pattern from the TextField using getText()
      (2) read the text from the TextArea using getText()
      (3) serarch for the start and end of the pattern using getIndex()
      (4) if found use setSelectionSta rt() and setSelectionEnd () to highlight the text in the TextArea

      can u please be more specific like how do i use the "setSelectionSt art"
      thank u

      Comment

      • tussom
        New Member
        • Mar 2007
        • 16

        #4
        Originally posted by horace1
        you would
        (1) read the pattern from the TextField using getText()
        (2) read the text from the TextArea using getText()
        (3) serarch for the start and end of the pattern using getIndex()
        (4) if found use setSelectionSta rt() and setSelectionEnd () to highlight the text in the TextArea

        the issue is about highlighting the text after finding it i don't know how to do that i need help quickly please.

        Comment

        • tussom
          New Member
          • Mar 2007
          • 16

          #5
          Originally posted by horace1
          you would
          (1) read the pattern from the TextField using getText()
          (2) read the text from the TextArea using getText()
          (3) serarch for the start and end of the pattern using getIndex()
          (4) if found use setSelectionSta rt() and setSelectionEnd () to highlight the text in the TextArea

          I have done 1 and 2 but couldn't do 3 and 4 sorry for nagging but can anyone help me please

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by tussom
            the issue is about highlighting the text after finding it i don't know how to do that i need help quickly please.
            try this simple program
            Code:
            import java.awt.*;
            import javax.swing.*;
            
            public class TextAreaDemo extends JFrame {
               JTextArea resultArea = new JTextArea(10, 50);
                    
                public TextAreaDemo() {
                    resultArea.setText("abcdefghijklmnopqrst");
                    resultArea.setSelectionStart(3);
                    resultArea.setSelectionEnd(15);
                    JScrollPane scrollingArea = new JScrollPane(resultArea);
                    
                    JPanel content = new JPanel();
                    content.setLayout(new BorderLayout());
                    content.add(scrollingArea, BorderLayout.CENTER);
                    
                    this.setContentPane(content);
                    this.setTitle("TextAreaDemo");
                    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    this.pack();
                }
                
                public static void main(String[] args) {
                    JFrame win = new TextAreaDemo();
                    win.setVisible(true);
                }
            }

            Comment

            • tussom
              New Member
              • Mar 2007
              • 16

              #7
              Originally posted by horace1
              try this simple program
              Code:
              import java.awt.*;
              import javax.swing.*;
              
              public class TextAreaDemo extends JFrame {
                 JTextArea resultArea = new JTextArea(10, 50);
                      
                  public TextAreaDemo() {
                      resultArea.setText("abcdefghijklmnopqrst");
                      resultArea.setSelectionStart(3);
                      resultArea.setSelectionEnd(15);
                      JScrollPane scrollingArea = new JScrollPane(resultArea);
                      
                      JPanel content = new JPanel();
                      content.setLayout(new BorderLayout());
                      content.add(scrollingArea, BorderLayout.CENTER);
                      
                      this.setContentPane(content);
                      this.setTitle("TextAreaDemo");
                      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      this.pack();
                  }
                  
                  public static void main(String[] args) {
                      JFrame win = new TextAreaDemo();
                      win.setVisible(true);
                  }
              }


              I got that part but the thing is that i want either to highlight or change font of any word that matches the pattern and they may be many so how do i get their index and highlight them accordingly??

              Comment

              • tussom
                New Member
                • Mar 2007
                • 16

                #8
                Originally posted by tussom
                I got that part but the thing is that i want either to highlight or change font of any word that matches the pattern and they may be many so how do i get their index and highlight them accordingly??

                we have to use the method Highlight (...) but i don't know how ??
                sorry again for nagging !

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by tussom
                  we have to use the method Highlight (...) but i don't know how ??
                  sorry again for nagging !
                  Don't be sorry for nagging. That's how we all learn.

                  Have a look at this and try to do the hightlight. You can post if you get further problems.

                  Comment

                  Working...