Highlighting text in JTextPane

Hi all, (sorry if this is similar post from others).
I'm trying to highlight text from when it is selected by the mouse, I want it to work by first I click a button to go in "text highlighting mode" then I select the text by pressing the mouse at the start of the selection and then releasing it - voila highlighted text..
I ain't got it right here, anyone know the fix?
//Create ActionListener for drawLine Button
          new ActionListener() {
          //Do some highlighting
          public void actionPerformed( ActionEvent event ) {
                        if( event.getSource() == drawLine );
                         text_chat.addMouseListener( new MouseAdapter() {
                         public void mousePressed( MouseEvent e ) {
                         //text_chat.setSelectedTextColor( Color.green );
                         change.highlight( text_chat.getSelectionStart(), text_chat.getSelectionEnd() );
                             String strStart = "" + text_chat.getSelectionStart();
                             String strEnd = "" + text_chat.getSelectionStart();
                             sendHighlight( strStart, strEnd );
                         public void mouseReleased( MouseEvent e ) {
                         change.highlight( text_chat.getSelectionStart(), text_chat.getSelectionEnd() );
                             String strStart = "" + text_chat.getSelectionStart();
                             String strEnd = "" + text_chat.getSelectionStart();
                             sendHighlight( strStart, strEnd );
                             //Parse back Integer start = Integer.parseInt(strStart);

can you send me the hole source too figure your problem out.

Similar Messages

  • JEditorPane - backmapping highlighted text to source html document

    Hello everyone,
    I have the following problem: I display a HTML document in JEditorPane. Then I highlight some chunk of text and get the start and the end selection offsets by using getSelectionStart() and getSelectionEnd() methods, respectively. What I would like to know is the exact position (character offset) of the highlighted text in the original HTML document that is being displayed.
    Does anybody know the solution for that?

    I don't think it's possible. The parser builds a Document out of the text and doesn't keep the original source text. For example if your html is like this:
    String text = "<html><body>     some     text     </body></html>";All the multiple spaces between the text is parsed out.
    Use a JTextPane and use attributes. Its easier to work with than using HTML.

  • Syntax Highlight in a JTextPane

    How can I create syntax highlight in a JTextPane?

    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.Color;
    public class MultiColouredText extends JFrame {
       public MultiColouredText() {
          StyledDocument doc = new DefaultStyledDocument();
          JTextPane pane = new JTextPane(doc);
          pane.setFont(new java.awt.Font("Verdana", 0 , 16));
          MutableAttributeSet mas = new SimpleAttributeSet();
             try {
                StyleConstants.setForeground(mas, Color.yellow);
                StyleConstants.setBackground(mas, Color.blue);
                doc.insertString(doc.getLength(), " Hello ", mas);
                StyleConstants.setForeground(mas, Color.red);
                StyleConstants.setBackground(mas, Color.lightGray);
                doc.insertString(doc.getLength(), " World ", mas);
             catch (Exception ignore) {}
          setDefaultCloseOperation( EXIT_ON_CLOSE );
       public static void main(String[] args) {
          new MultiColouredText().show();

  • How to listen and obtain caret positions of changed text in JTextPane?

    I have a JTextPane that displays text with some styles on particular words in the text. For example, I highlight words with red color if they are in my dictionary file. I use regular expression to find matches, replace them with their corresponded definitions, and call setCharacterAttributes to add styles to the definitions. I store all start and end caret positions of the matched words/definitions in a vector. So, I can redisplay styles of all previous matches.
    The problem is that I'd like to be able to edit the text of some previous matches. So, with those changes all caret positions that I already store in the vector need to be updated.
    How can I obtain caret positions of changed text in JTextPane?
    How can I know that a user is currently changing text in the JTextPane?
    How can I get style of text such as color that is being changed?
    Thank you very much.

    Thank you very much, camickr, for your reply.
    I think that I might not know the right way to handle JTextPane and Document object.
    What I have done are:
    - Add style to JTextPane using, for example,
    Style targetCurrentMatchStyle = this.targetWindowTextPane.addStyle("Red", null);
    StyleConstants.setForeground(targetCurrentMatchStyle, Color.red);//For highlight - Then, I use regular expression (Pattern and Matcher) to find a match in text. For each match, I get start and end position from matcher.start() and matcher.end().
    String term=matcher.group();
    int start=matcher.start();
    int end = matcher.end();
    //find definition for the matched term.
    String definition=mydictionaryHash.get(term);
    //Store caret positions in lists
    //Add changed to text in textpane
    StringBuffer sb=new StringBuffer();
    matcher.appendReplacement(sb, definition);
    //Get translated text from StringBuffer after replacement
    String translatedText=sb.toString();
    //Update start position for next search
    //Add style to matched regions below.
    }- From the lists of start positions and matched lengths, I use the following code to add "Red" color to the matched text regions including all previously matched.
    for(int i=0;i<startPositionList.size();i++){
    }My issue is that I'd like to be able edit previously matched regions and update all positions of the matched regions stored in the lists.

  • Highlighting Text & Auto Text Options

    I've recently migrated from MS Word to Pages, and I'm determined to make this new relationship work. In Word I used its highlighting text (as with a real highlighter--yellow, pink, etc.) feature, as well as its Auto Text feature, in which I had a number of prescribed, hyperlinked comments. (I'm a teacher, and I would auto text things like, "comma error," which would then be linked to a web page about comma errors.
    Can anyone help me discover ways to do both of these in Pages? Also, is there anything for Pages like MS Word's macros?
    Thanks in advance,
    Brad Carpenter

    Welcome to the forums, Brad.
    There is no direct analogue in Pages to Word's highlighting feature. However, you can use the Comments feature to add specific comments to a specific piece of text (with the advantage that the comments can be easily hidden, since you are not changing the characteristics of the actual text). Simply select a bit of text and click the Comment icon in the toolbar, or from the Insert menu select "Comment". The selected text will highlight in document, and a comments box will appear to the left of the page, in which you can type whatever you want. In the comments box you can select whatever text you want and make it a hyperlink, by going to the Hyperlink Inspector. Unfortunately, there is not an easy way to automate the creation of a hyperlink, at least as far as I know.
    There are plenty of third-party applications that will let you store and add snippets of text easily to any Mac document, including Pages. These include TypeIt4Me, Butler, and even Quicksilver, which has a fanatical following and may be more than you need for this kind of task. I'm sure others here might be able to suggest other apps.
    One caution, however: If you intend to be sharing documents with others, they must be using Pages themselves to take advantage of these features. Unless your students are also using Pages, these workarounds won't really help.

  • Is there a way in Pages to show and print all the added "comments?" (those you add by highlighting text)  I'd like students to be able to view my comments on printed version of their papers.

    Is there a way in Pages to show and print all the added "comments?" (those you add by highlighting text)  I am a teacher, and I would  like students to be able to view my comments on printed versions of their papers. I know word displays the comments off to the side of the document, and those comments can be viewed all at once and printed alongside the document.

    There doesn't appear to be a way to print conmments in either Pages or Numbers in the Mavericks versions.

  • Highlighting Text

    Since updating Adobe Reader XI I have been unable to highlight text in any pdf document. While other functions, such as the insert text function work, highlighting does not. I have tried to change the base colour but that does not work either.
    I would be grateful for any help/tips.

    I opened three files:
    One says "Adobe PDF Library 2"
    Another says "Acrobat Distiller 8.0.0. (Windows)"
    Another says "1.5 (Acrobat 6.x)"
    However, in none of the does the text highlight work.
    Is a UK/English encoding problem eg Unicode instead of Western?

  • Highlighting text in  Preview

    Unexpectedly, a strange alteration to how I can highlight text,  when reading a pdf in Preview has occurred. I used to be able to check the highlight icon in the annotations bar and that's about it. Now when I intend to highlight text, the colour extends to being four lines wide, and when I finish - it gets even stranger. Instead of  the text with a straight band of colour - like with a real high-liner pen - odd shapes occur across the text! How to revert to a simple, colour line highlighting the text I feel is important?

    No it has nothing to do with, if the PDF is protected or not. It is not in this case, it is having the PDF open in Preview and holding down the mouse button and dragging and highlighting the information in whatever direction you push your mouse in. Preview seems to only highlight what ever is showing on the screen instead of highlighting from page to page.

  • Highlighting Text In Pages 4 and 5: How To/Best Methods?

    Hello Fellow Macers:
    I need advice on the best way to highlight text in Pages 4 and 5:
    I use and prefer Pages 4.3. Yes, I have Pages 5.2, and I find that using it good for some things (dictating into a document), but not so good for others - highlighting text.
    For example, I read tons of documents and like to make notes, comments and especially highlight text according to a color-code scheme that I have used for years.
    For those who use Word, this process of highlighting text is pretty straight-forward:
    You highlight your text and then simply click your highlight box on the Word toolbar (choose a default color, usually yellow) and you're done. If you need to use another color, just use the pull-down, as seen above. The drawback in Word - no Orange, or sectrum option.
    But let's face it - Word as a program is just too much. As someone who has used every word processing program under the sun, I find Pages excellent. It does what I need.
    However, I find highlighting text in Pages clumsy. Here's what I need to do to highlight text in Pages 4.3:
    Go to your toolbar, and then choose a highlight/background color. It's more labor intensive then Word, above, but you do have the option of more colors.
    Then there's this method, where basically you get to simply use yellow as your color:
    IMHO both are clumsy methods compared to Word, but have become even more so in Pages 5.2....
    Lots of clicks to achieve simple color highlights.
    Am I missing something, or is there a simpler way, in either Pages 4 or Pages 5, to achieve the above results???
    Any suggestions appreciated!

    Dear VikingOSX:
    As Ed MacMahon once said (before your time?) re Pages 5 Character Fill Color: "You are correct, sir!!"
    The gear icon IS where the function that I'm talking about is located. Still, it's annoying.
    Q: Why?
    A: Because from a work-flow perspective as someone who is highlighting it makes no sense. We in the Western world read and write from top to bottom, left to right. Making a mark on a doc should follow the same flow.
    You should not have to keep looking over to the right side of the doc to find your function. Logically it should be at the top (e.g., toolbar), on the left, or perhaps some contextual pop-up option.
    Furthermore, it's more "labor intensive" from this point of view: In Word, once you set the color (which, granted, you may need to change from time to time IF you, like me, use multiple colors as part of your highlighting workflow), you just click the box after highlighting the text - 1 click vs 2, and possibly 3.
    The "richer color" part of my comment was just to note that a flaw, in my view, of Word's basic scheme is that:
    1) It lacks an Orange. And we've all used an Orange highlighter at somepoint, right? Or perhaps, you were like me in High School and threw them instead. But you get the point...and
    2) Having a wider pallatte available as an option - which you do in Apple's scheme - is nice. I use color coding/highlighting to make notes and save time. The colors tell me what's important, what's next important, key terms, etc. Also, for example, if I wanted to outline my highlighting notes, the color scheme (which has a heirarchy based on color, right?) allows me to do this quickly because it's already there.
    As for the Dictation function working on Pages '09/4.3 - again, you are correct, sir! And that's great to know. I had tried to use it before when the new Pages 5 (hitting fn twice on Pages 4.3) but it didn't activate. I tried it after reading your response and Bingo! It's Alive! So thanks for that tip.
    Your keyboard shortcut scheme is an interesting idea, and I may try it as an experiment. My intuition is that it will not work as straight-forwardly as Word's toolbar pull-down will, but I might be wrong. We'll see.
    And yes, I do use Pages '09 (4.3) instead of Pages 5 for exactly this reason (see my discussion)
    Great reply! Thanks!

  • Highlighting Text in pdf Documents using Preview

    Am using Snow Leopard (10.6.3) with Preview version 5.0.2.
    Tried highlighting text using the instructions in Preview help. No success.
    I click Annotate; chose the Highlight Colour but then appear to be unable to select the text.
    Anyone any ideas?

    Thanks for the hint. I think I've just discovered why I cannot do it. The pdf document I'm trying to highlight is the result of the output from a scanner. Hence it is effectively a "picture" - not text. The only thing that I have been able to do is to choose Annotate and then choose "Add Oval" or whatever and select its size and colour and then position this over the text I wish to highlight.
    Because it is effectively a picture you cannot select text. I tried it on another pdf document that IS text and your solution works a treat.
    Thanks again

  • Highlighting text in Reader

    When highlighting text using the highlighting tool, Reader is highlighting a block instead of a row of text.  How do I correct this?

    Hi Pat
    Nope not an image - but I'll tell you, when checking the document's properties, I again tried the highlight function and without amending anything to the document or the application; it now highlights correctly ... very odd

  • Highlighting Text Acting Like REDACTING Text

    Dunno what's goin on.   A Pro 9.  Windows Home, ALL UPDATES.
    I highlight text and instead of seeing the text highlited, the highligt COVERS the tex.  It's YELLOW colored REDTACTION. 
    I've REPAIRED the app TWICE.  I REBOOTED each time.   That APPEARED to have corrected the problem.   It DIDN'T because the REPAIR failed and the HIGHLIGHT BLOTTOED OUT the text.   I uninstalled and reinstalled the app.   I opened the last thing I was working on where the YELLOW HIGHLIGHT redacted the text and the text WAS VISIBLE while highlighted.   THEN I opened another PDF file and same deal, the YELLOW highlighting REDACTED rather than HIGHLIGHTED.
    Uh, is it a me thing?  Is it a thing I can chek or uncheck in A Pro 9?  Wassup with this weirdness?

    CS4 is not kind to those who don't know HTML/CSS well enough
    to avoid these
    errors, I'm afraid. It's really all about how much HTML and
    CSS you feel
    comfortable with, not the tool you are using. Should the
    page/site be
    rebuilt? I think so - at the least it should be taken apart
    reassembled. Are the mistakes you made ones that you will
    make again? Yes,
    I think so.
    I don't want to sound harsh or pessimistic, but I think
    that's the bottom
    line. I assume you are building the site for her so that she
    wouldn't have
    the expense of paying someone to do it, right? But wouldn't
    you also want
    her site to be the best it can be, and not to look/behave
    like someone's
    first site?
    Now - if you want to bite the bullet and shove down some HTML
    & CSS, and
    muddle through, there is plenty of help here, but if you
    aren't carrying
    your load, people will tire of solving the problems you
    Murray --- ICQ 71997575
    Adobe Community Expert
    (If you *MUST* email me, don't LAUGH when you do so!)
    - DW FAQs, Tutorials & Resources
    http://www.dwfaq.com - DW FAQs,
    Tutorials & Resources
    "rob717" <[email protected]> wrote in
    news:gpbkbn$d8c$[email protected]..
    > Murray, My wife owns the company but I made the website
    for her using DW 8
    > (The
    > Macromedia version). In December/January I upgraded all
    my Adobe programs.
    > I
    > like the home page layout but I guess I could just redo
    it and hope DW CS4
    > holds my hand enough to avoid all the errors. I'm
    retired and not really
    > very
    > good at all this. Do you think I should just start over
    because I really
    > don't
    > have the experience to fix the errors? Rob

  • Highlighting  text (as in Word)

    Hi - Is is possible to highlight text, that is color over text, in a similar way to how it can be done in Word. I use this feature in Word a lot for editing & redrafting purposes!
    Many thanks!

    pete scott1 wrote:
    Hi - Is is possible to highlight text, that is color over text, in a similar way to how it can be done in Word. I use this feature in Word a lot for editing & redrafting purposes!
    Many thanks!
    *A slightly more efficient system of highlighting text: (and possibly slightly more efficient than MSword as well as you can highlight text with a keyboard shortcut after a simple initial setup.)*
    *+step 1+*
    select text. apply background colour.
    *+step 2+*
    open styles drawer through menu or shiftcmdt
    *+step 3+*
    _select again_ the text you applied the b/g colour to previously. in the styles drawer, under character styles, click the little upside-down triangle and click +create new character style from selection+. name your style.
    ( *+step 4+* - *Keyboard shortcut and use* )
    click on the upside-down triangle for options on your new style and add a hotkey. f6 works fine as it's not usually already assigned to another function. _*now, highlight away by selecting text and either clicking on the highlight style in the styles drawer, or simply pressing f6.*_
    ( *+step 5+* )
    to maintain the highlight style you created in the styles drawer as a default style for all documents, use this tip by Dennis from another post
    +Start with a blank document, set it up the way you want (fonts, views, rulers, styles, etc.), then click on File > Save As Template. Finally, click on Pages > Preferences > and set that template as your startup document. That way, every time Pages opens, you'll have your preferred styles right there handy.+
    If you don't want to mess with templates for some odd reason, you can always import the style from a previously highlighted document to which the style you created is attached. Use Format -> +Import Styles+

  • Highlighting text in emails

    Is there anyway to highlight text of emails I receive? I would like to be able to highlight the important sections in some the very wordy emails I get, so I go back to read them, I don't have to reread the whole thing!
    Thanks for your help!
    Message was edited by: pb_raza

    How could I suggest this to Apple?

  • Highlighting text in JtextArea

    I want to highlight text in a JTextArea each time the text changes and therefore i use a DocumentListener and also the following :
    public Highlighter.HighlightPainter redHighlightPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.red);
    Highlighter hilite = gJTextArea.getHighlighter();
    hilite.addHighlight(bufferBegin , bufferEnd, redHighlightPainter);
    but the text remains black
    How can you explain this?

    I have used the same exact piece of code in a JTextArea and it works perfectly. Maybe your problem is elsewhere in the code because this should work fine. Are you sure that the Document listener is fireing when changes occur?

Maybe you are looking for

  • Performance decrease with xorg 1.6 and intel

    i have a box with gigabyte ga-8ig1000mk mainboard intel 865G graphics celeron 2.6 cpu 512 M ram arch linux x does not start with xorg 1.5/intel or xorg 1.6/intel (video memory related error message) kde 4 performance with xorg 1.6/intel-legacy is not

  • How to reduce the amount of transparency levels in photoshop

    So I am making a t-shirt design and I have some transparent images (I cannot control the opacity) and since it's for a t-shirt it cannot have half transparent pixels, so is there any kind of filter or operation to make it so that there are only two l

  • Macbook Pro will not load OS and will not boot from Install Disk

    When I try to turn on my macbook pro it goes to the gray logo screen with the spinning gear however it does not progress from there and then after several minutes it proceeds to restart itself and the same process repeats itself. I have tried to boot

  • CProjects 3.1: Project phase can be approved without individual approval

    Hi Guys, My requirement is to prevent cProjects phase from being approved if all of the individual approvers have not given their decisions. I've tried using the customising for phase types to achieve this. Under the setting "Granting of App. with Re

  • FormatDate, what decides the language?

    Hi, Im developing a site with JSTL in my jsp-pages and have done the formatting of a date the way I want. The month is shown with three letters, jan, feb, mar, apr.... This is the code for the outprint: <fmt:formatDate value="${ad.date}" pattern="MMM