Adding Highlight in JTextPane ?

Hi All,
I have a interface where i can display a html page.
Now i want to highlight few text from the displayed page in any color.
Can anyone tell me how can do that ?
I am trying to make a demo of search where user enters some key words and i wanna highlight all the words in the page.
Is it possible ?
Hopeing for a favourable response.
Kind Regards
Chetan Chandarana

You might be able to use a Highlighter. Here is some code I copied from another thread that gives you an idea how to use a Highlighter.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.util.*;
public class HighlightTest
     public static void main(String[] arguments)
     JFrame f = new JFrame("3plus4 HLM test");
     JEditorPane e =
     new JEditorPane("text/plain", "hfhk shhdf kdsh kshfdsh fh fkshfhs fh fhf dhf hfhf hfd hfdf ");
     f.getContentPane().add(new JScrollPane(e));
     HLM h = new HLM(e);
     h.addHighlight(3, 5);
     h.addHighlight(9, 12);
     h.addHighlight(16, 22);
     f.pack();
     f.setSize(200, 200);
     f.show();
     static class HLM implements CaretListener
          static class HighlightInterval
               int p0, p1;
               Object tag;
               HighlightInterval(int p0, int p1, Object tag)
                    this.p0 = p0;
                    this.p1 = p1;
                    this.tag = tag;
               boolean contains(int dot)
                    return dot >= p0 && dot < p1;
          ArrayList highlights = new ArrayList();
          Highlighter h = new DefaultHighlighter();
          Highlighter.HighlightPainter p = new DefaultHighlighter.DefaultHighlightPainter(Color.yellow);
          HLM(JEditorPane editor)
               editor.setHighlighter(h);
               editor.addCaretListener(this);
          public void caretUpdate(CaretEvent e)
               for (Iterator i = highlights.iterator(); i.hasNext();)
                    HighlightInterval hi = (HighlightInterval) i.next();
                    if (hi.contains(e.getDot()))
                         h.removeHighlight(hi.tag);
                         i.remove();
                         break;
          public void addHighlight(int p0, int p1)
               try
                    Object tag = h.addHighlight(p0, p1, p);
                    highlights.add(new HighlightInterval(p0, p1, tag));
               catch (BadLocationException e)
                    e.printStackTrace();
}

Similar Messages

  • Syntax highlighting using JTextPane

    Hello,
    Does somebody know how to achieve quick, efficient syntax highlighting using JTextPane. I have tried it out by actually removing and reinserting the colored text, but it is way too slow. It is worse if I do it in a thread. It will be a great help if somebody bails me out of this problem.
    S. Senthil kumar

    Let me see if I understand your problem...you are attempting to open a fairly large file and then highlight certain portions of the text. You're asking for help on how to efficiently do this and several people gave you links on how to highlight text which leads to your last question about the files taking too long to load. How big are these files you're talking about and how long does it take for them to load? What time is acceptable? Remember, all commercially available document display programs have a certain load time which grows larger as the document it is attempting to display grows in size.
    -Kevin

  • Not retaining the text highlight in jtextpane

    I have a jtextpane and a button in a frame. when the user selects some text in textpane and clicks on the button, a word gets appended to the selected text and also gets highlighted. It works fine when the button is clicked but the when the focus changes, the highlighted text doesnt retain the color, but retains the appended text. is there anything i am missing in the code to retain the highlight?
    private Highlighter.HighlightPainter yellowPainter = new DefaultHighlighter.DefaultHighlightPainter( Color.YELLOW);
    public void actionPerformed(ActionEvent ae){
            JEditorPane textForComments = getEditor(ae);
                    if (textForComments != null) {
                    int caretPos1 = textForComments.getSelectionEnd();
                    int selStart = textForComments.getSelectionStart();
                   textForComments.getDocument().insertString(caretPos1, "ID", null);
                   Object highlightTag = textForComments.getHighlighter().addHighlight(selStart ,caretPos1 , yellowPainter);
    }

    Selected text is only highlighted when focus is on the text component. Since you clicked on the button the button now has focus. You need to invoke
    textForComments.requestFocusInWindow();

  • Adding ScrollBars to JTextPane

    I migrated from TextArea to JTextPane in a simple chat program in order to display colors and styled font sizes. The problem is that the border and scroll bars previously given by TextArea are gone when I switched to JTextPane. I managed to place a border. But I cannot figure out how to add at least vertical scroll bars to the TextArea. I tried to use JScrollPane and then added the JTextPane to it, but then I cannot see what I type.
    Can you please help?
    CS

    You are doing the correct thing. To add scrolling to any Swing component you add the component to a JScrollPane and then add the JScrollPane to your container.
    If you cannot see what you are typing that may be because you didn't give your component enough space and the scrollbars of the JScrollPane are taking up all the space. Give your component more space and see if that fixes the problem.
    HTH
    Bryan

  • Imported PowerPoint slides, added highlights, but not showing in publishing

    Hi...I'm a little confused.  I imported a 15 slide powerpoint
    file into Captivate and added audio, a few highlights and several animated buttons. I published 1 version and the animation showed up, but when I recreated another project, I get none of the animation showing up in the preview.  Any suggestions?

    Hi,
    Can you elaborate more about how did you re-create the project?
    I wanted to know whether you created a similar project from the beginning (New Blank Project) or copy/pasted the slides from one project to the other.
    It would be helpful, if you explain the steps you followed while re-creating the project.
    Thanks,
    Vikranth.

  • Adding text to JTextPane

    I've go a JTextPane placed on a JScrollPane, when I add a lot of text on it, the vertical scrollbar is going to the bottom, I want that TextPane view the top of the text automatically, how can I do this ? I tried to move the vertical scrollbar but it returns to the bottom
    sorry for my english :)

    Questions like this have been asked and answered dozens of times, although I must admit that most can't figure out how to get the text to scroll automatically. But the concept is the same. You get one freebie answer.
    textComponent.setCaretPosition( 0);Next time search the forum before posting.

  • Problem adding Icon to JTextPane

    I want to add an image to my JTextPane but it won't work. First i thought it was something with my path of my file but then i tried with a JFileChooser so i could chose the file so i'm sure that the file is good. But still, i don't get the image on my JTextPane. This is the code:
    File file = chooser.getSelectedFile();
    Icon icon2 = new ImageIcon (file.getAbsolutePath());
    textPane.insertIcon (icon2);I don't know if this would help but i'm using a lot of different SimpleAttributeSets
    My textPane is dissabled, can it be that?

    I have found that the problem is with the setEnable.
    When it is disabled i can't add an Icon but when it is enabled, i can.
    When it is disabled, then i do: textPane.setEnabled(true) and then add an icon, it also won't work. Not even when i set the carretPosition.
    How can i add an icon AND still be disabled?

  • Adding highlights

    so i have a scene with a 3D character I rendered, which i'm compositing over live action footage.
    there should be a brighter rim light behind the character, which i tried to add with a Light Sweep (edge) effect.
    the problem is that the edges are blurred (via track matte) to blend it with the footage, but this blur dulls the rim light.
    anyway for me to get around this?
    I was thinking of tracking the character and adding another layer with a mask for that one area, but wanted to know if there was a better way.
    as always thanks!

    This perhaps http://forums.adobe.com/thread/749933?tstart=0 or this http://forums.adobe.com/thread/751540?tstart=0 ? Also, obviously you could/ could have render(ed) the rim light as a separate pass in your 3d app and thne overlay it separately on top of everything else...
    Mylenium

  • Adding highlight arrows

    I need to add an arrow to my son's movie that point him out as he's playing lacrosse with 30 other people on the field. CAn't seem to figure out how to do it.

    I used the text caption option under insert and the screen shot is where they were inserted - I've had two timelines for objects since I started editing this video. How do you create an additional timeline so text captions will not run into the other objects I mentioned?

  • Can't save files or highlight with Reader 11.7.1 & Kitkat 4.4.2 Lenovo A7 30

    Hi I have installed Adobe Reader 11.7.1 The highlight feature in not working. After highlighting the text and coming out of the application the highlighted portion is not gettig saved Pls advise.

    Thanks for the prompt reply. Android Kitkat has imposed some restrictions on accessing files on external SD card by third party apps and therefore, Adobe Reader is unable to edit the files on the external SD card. In such cases, Adobe Reader gives the users a message when adding highlights to the document asking them to either save a copy on the internal storage or view the file as read-only. Could you please attach a small video of the whole scenario- from the time you open the file, add highlights and then close the file?
    Also, could you try and move the file or make a copy of the file to your internal storage and then try to add highlights to it? Do you still observe the same issue?
    Awaiting your response.
    Thanks,
    Adobe Reader Team

  • Creating a scrollback buffer using a JTextPane

    Hi all, I've run into a problem with an application I'm developing, and I'm hoping to find some help here. This is my first crack at a designing anything worthwhile using Swing, so please bear with me. :)
    My problem: I need a buffer to store a certain number of lines of text, then when the maximum is reached, start removing the oldest lines. eg) max of 10 lines, when line 11 is received, line 1 is removed.
    The JTextPane is contained in a JScrollPane, so the actual scrolling is taken care of.
    Also, somewhat related, how can I force the JScrollPane to stay at the "bottom" of the pane? When new data is received from the server, it's added to the JTextPane, but I can't seem to find a way to force it to stick to the bottom.
    Any help will be greatly appreciated. :)

    Could you use the system line terminator to determine how many lines of text are in the JTextPane? If so, you can simply find the first line, and remove it, then re-set it back into the JTextPane. A StringBuffer would probably be beneficial for the manipulation.
    For the JScrollPane you could try using the getVerticalScrollBar() method to get the vertical scroll bar, then set the it's value to it's maximum value, to try and force it to the bottom of the scrolling region.

  • BACKGROUND COLOUR AND HIGHLIGHT TEXT ISSUE

    Hi, Just registered and I'm keen to ask you a pressing question. Setting background color different from white and adding highlight text causes the two colors to blend together i.e. the highlight color is not as vivid as applied over a white background or for instance as in a MS Word document. I'm expecting and appreciating your opinion.

    Of course. Mix two colors and a new, different color comes to be.
    Two primaries will yield a secondary (yellow + red = orange).
    Optics, not the software application eh.
    Try using a different highlight color with the designated background color.
    Try using  different opacity values for the highlight color.
    Try using different opacity values for the background color.
    Review the theory and practice associated with the color wheel to identify the most harmonious (for you) colors.
    Be well...

  • Need help with publishing -- highlight boxes and recordings

    I'm having a problem with publishing a presentation.  I've recorded audio for PP slides and added highlight boxes.  I've tested these in Edit mode - the recordings work fine and the highlight boxes appear correctly as per their settings.  When I go to publish, some of the recordings and highlight boxes are not working on some of the slides (but work fine on other slides).   Obviously I've missed some setting on these but I cannot figure out what and where.  Could anyone please help??

    Just kidding. Now, the files seem to be uploaded and in the right folder; however, now the page just isn't loading. See what I mean, everything going wrong?!

  • JTextPane and StyledDocument question

    I was just wondering if it is necessary to have a StyledDocument that is added to the JTextPane, or if it is acceptable to have all the components (JLabels in my case) added to the JTextPane rather than a StyledDocument?
    Right now I basically have a JTextPane with a few JLabels in it that is added to a JPanel. This works fine, but I think I might run into a problem if I need to add any text other than JLabels. Any help is appreciated... sorry if this doesn't make much sense.
    dub

    but I think I might run into a problem if I need to add any text other than JLabelsWhy are you adding only labels to a JTextPane. That is what a JPanel is for.

  • Compare 2 different xml versions and highlite the differences

    Hi,
    Currently we have xml and that is displayed on the web page using the css style sheets.
    The xml we have remain the same most part but with small changes (say different version created after a week).
    We want to be able to track the differences from the previous version and highlight them, the differences, when showing it in the web page.
    Please advise if this can be done and how.
    Thanks.

    try
    SQL> set serveroutput on
    SQL> set long 50000
    SQL>
    SQL> declare
      2 
      3    --main
      4    xml1 xmltype := xmltype('<ROWSET>
      5                                <ROW>
      6                                  <EMPNO>7934</EMPNO>
      7                                  <ENAME>MILLER</ENAME>
      8                                  <JOB>CLERK</JOB>
      9                                  <MGR>7782</MGR>
    10                                  <HIREDATE>1982-01-23T00:00:00</HIREDATE>
    11                                  <REMOVED>abc</REMOVED>
    12                                  <SAL>1300</SAL>
    13                                  <COMM/>
    14                                  <DEPTNO>10</DEPTNO>
    15                                </ROW>
    16                              </ROWSET>');
    17    --new
    18    xml2 xmltype := xmltype('<ROWSET>
    19                                <ROW>
    20                                  <EMPNO>7934</EMPNO>
    21                                  <ENAME>MILLER</ENAME>
    22                                  <JOB>CLERK</JOB>
    23                                  <MGR>7782</MGR>
    24                                  <ADDED>xyz</ADDED>
    25                                  <HIREDATE>1982-01-23T00:00:00</HIREDATE>
    26                                  <SAL>1300</SAL>
    27                                  <COMM/>
    28                                  <DEPTNO>10</DEPTNO>
    29                                </ROW>
    30                              </ROWSET>');
    31    diff xmltype;
    32 
    33    xtype varchar2(80);
    34    xpath varchar2(80);
    35    xpathChild varchar2(80);
    36    xnodeChild varchar2(80);
    37    new_value xmltype;
    38 
    39    new_xml xmltype;
    40 
    41 
    42  begin
    43 
    44    -- get diff
    45    select xmldiff(xml1, xml2) into diff from dual;
    46    dbms_output.put_line(diff.getclobval());
    47 
    48    -- that have
    49    select x.xtype, x.xpath, x.new_value
    50      into xtype, xpath, new_value
    51      from xmltable(
    52             xmlnamespaces('http://xmlns.oracle.com/xdb/xdiff.xsd' as "xd"
    53                          ,'http://www.w3.org/2001/XMLSchema-instance' as "xsi"
    54                          )
    55           , '/xd:xdiff'
    56             passing diff
    57             columns xtype     varchar2(80) path 'local-name(xd:insert-node-before)'
    58                   , xpath     varchar2(80) path'xd:insert-node-before/@xd:xpath'
    59                   , new_value xmltype path 'xd:insert-node-before/xd:content/*'
    60           ) x;
    61 
    62     dbms_output.put_line('type of doing: ' || xtype);
    63     dbms_output.put_line('path for doing: ' || xpath);
    64     dbms_output.put_line('value for doing: ' || new_value.getclobval());
    65 
    66     if (xtype = 'insert-node-before') then
    67       xpathChild := substr(xpath, 1, instr(xpath, '/',-1)-1);
    68       xnodeChild := substr(xpath, instr(xpath, '/',-1)+1);
    69 
    70       select insertChildXMLbefore (xml1,
    71                                    xpathChild,
    72                                    xnodeChild,
    73                                    XMLType('<HIGHLIGHT>' || new_value || '</HIGHLIGHT>'))
    74       into new_xml
    75       from dual;
    76     end if;
    77 
    78 
    79     dbms_output.put_line(new_xml.getclobval());
    80 
    81  end;
    82  /
    <xd:xdiff xsi:schemaLocation="http://xmlns.oracle.com/xdb/xdiff.xsd http://xmlns.oracle.com/xdb/xdiff.xsd" xmlns:xd="http://xmlns.oracle.com/xdb/xdiff.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><?oracle-xmldiff operations-in-docorder="true" output-model="snapshot" diff-algorithm="global"?><xd:insert-node-before xd:node-type="element" xd:xpath="/ROWSET[1]/ROW[1]/HIREDATE[1]"><xd:content><ADDED>xyz</ADDED></xd:content></xd:insert-node-before><xd:delete-node xd:node-type="element" xd:xpath="/ROWSET[1]/ROW[1]/REMOVED[1]"/></xd:xdiff>
    type of doing: insert-node-before
    path for doing: /ROWSET[1]/ROW[1]/HIREDATE[1]
    value for doing: <ADDED>xyz</ADDED>
    <ROWSET><ROW><EMPNO>7934</EMPNO><ENAME>MILLER</ENAME><JOB>CLERK</JOB><MGR>7782</MGR><HIGHLIGHT><ADDED>xyz</ADDED></HIGHLIGHT><HIREDATE>1982-01-23T00:00:00</HIREDATE><REMOVED>abc</REMOVED><SAL>1300</SAL><COMM/><DEPTNO>10</DEPTNO></ROW></ROWSET>
    PL/SQL procedure successfully completed
    SQL>
    SQL> select * from v$version where rownum=1;
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
    SQL>

Maybe you are looking for

  • Bluetooth not working, drivers not helping or saying not device found. Pavillion P7-1254

    i have been looking everywhere, and tried almost every driver solution i could find but i still cannot find a driver for windows 7 that will allow me to use my bluetooth. i have used it recently but i had to reinstall the OS now the driver on the HP

  • How can I consolidate (merge) two adobe ID's

    Sorry for putting this here - but the chat (which comes up after a maze in the helps) does not work for me - and there does not seem to be a forum for general questions or an Adobe e-mail address for help requests either. I have two ID's: one for for

  • Best practices for Flash forms

    This question was posted in response to the following article: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec10372 -7fed.html

  • Adobe ID verification not working

    I can't remember my password to the Adobe ID, I tried resetting my password but it wouldn't work. It would ask me for both my Apple ID and password but would only have the slot open for the password. I got frustrated and opened another Adobe account

  • Installing Boxed Version

    My wife and I downloaded the trial version of LR 4 to our computers. She loves it so much that I bought her a boxed copy to arrive on her birthday (I will probably buy a boxed version myself). My question is: when the boxed version arrives, will we h