Highlighting/text color

I have a quick question :
I have a program that check every words in a JTextArea and I need to highlight or change the color of the incorrect word. So, when the program is over, I will see some words in a different color(or highlighted, whichever is the easiest to do).
I only need to know how to do the change of color (I already have the program which give me the location of every wrong words)
Thank you.

is there a lot of change to do to switch to JTextPane? There's some work since, inevitably, you have to do more than getText(),
setText().
As well as the section on text components in the Swing section of
Sun'sTutorial, there is this article from a while back:
http://java.sun.com/developer/JDCTechTips/2002/tt0821.html#tip2
Google about! If you get stuck you'll find it best to post code and
questions to the Swing Forum.

Similar Messages

  • Changing highlight text color / using Highlighters

    I've subclassed JTextArea, and among other things, am trying to add the functionality for drawing the active selection even when the TextArea doesn't have focus. I've overridden processFocusEvent like so:
    public void processFocusEvent(FocusEvent e){
            Highlighter h = this.getHighlighter();
             if (e.getID() == e.FOCUS_GAINED){
                //remove the inactive highlight
                h.removeAllHighlights();
            //this will put the selection highlight back in for us, among other things,
            //if we just gained focus.
            super.processFocusEvent(e);
            //now add the inactive-selection highlight if we just lost focus
            if (e.getID() == e.FOCUS_LOST){
                try {
                    h.addHighlight(
                            this.getSelectionStart(),
                            this.getSelectionEnd(),
                            new DefaultHighlighter.DefaultHighlightPainter(
                                new Color(212,212,212)
                } catch (Exception ex){
                    ex.printStackTrace();
        }and it works quite nicely. Notice that I draw the inactive selection gray.
    There's a slight problem with this: JTextArea doesn't treat the text in the highlighted section any differently, as opposed to how it would when the component is focused (i.e. rendering it in the highlighted text color). I'm guessing this is because it knows that it's not focused, and therefore assumes that there's no highlighting to be drawn. So as a result, all text in the text area is rendered in the same color --- the foreground color --- when the textArea doesn't have focus, regardless of what is being highlighted. For this reason, setSelectedTextColor() has no effect.
    But I want to be able to change the highlighted text color, since, in theory, the user's system could be configured to show text in the exact same gray that I'm highlighting with. I want to just be able to set it to be white or black.
    I am rather averse to using a TextPane, since what I'm writing is designed to edit plain text, and a TextArea has many wheels that I don't want to reinvent, like setTabSize(), for example.
    Clearly, there is code already in place somewhere to draw text a different color for highlighted portions of a TextArea. Is there some way I could perhaps trick only the drawing code into thinking that it has focus? What is the cleanest way to accomplish what I want, i.e. the way that involves rewriting the least amount of functionality?
    Any suggestions/leads would be greatly appreciated.

    Here's a better way to keep the selection visible. All those hard-coded colors are a big no-no, but this is just to demonstrate the technique.
    import java.awt.Color;
    import javax.swing.text.DefaultCaret;
    import javax.swing.text.Highlighter;
    import javax.swing.text.DefaultHighlighter;
    * usage:
    * textComponent.setCaret(new GhostCaret());
    public class GhostCaret extends DefaultCaret
      private static final Highlighter.HighlightPainter unfocusedPainter =
          new DefaultHighlighter.DefaultHighlightPainter(new Color(230, 230, 210));
      private boolean isFocused;
      protected Highlighter.HighlightPainter getSelectionPainter()
        return isFocused ? SimpleHighlighter.defaultPainter : unfocusedPainter;
      public void setSelectionVisible(boolean hasFocus)
        if (hasFocus != isFocused)
          isFocused = hasFocus;
          super.setSelectionVisible(false);
          super.setSelectionVisible(true);
          getComponent().setSelectedTextColor(isFocused ? Color.WHITE : Color.BLACK);
    }

  • Preview - Highlight Text Color - Keyboard Shortcut

    Hi!
    I've loving the markup capabilities of Preview in Mountain Lion and was wondering if there was a way to quickly change colours (colors) of the highlighted text via keyboard shortcut?
    I found this thread for Snow Leopard, but couldn't figure out how to get it to work for Mountain Lion:
    https://discussions.apple.com/thread/3180984?start=0&tstart=0
    It seems there are some shortcuts possible, for instance when I click the Markup dropdown box if I type "g" it will highlight Green, "b" it will select Blue etc.  But it won't actually select these options.
    The quickest way I've found is the right-click after highlighting which brings up a contextual menu to select the colour of that highlight. 
    Ideally I'd press a shortcut combination to change the highlight colour before selecting what to highlight.  Does anyone know of a way to get this functionality?
    Thanks so much for your help! =)
    Derek.

    Check out this article: http://support.apple.com/kb/PH3957?viewlocale=en_US&locale=en_US
    and do as explained in the link you posted yourself.
    Worked for me by adding shortcuts from General settings-->Keyboard-->Shortcuts and define them for Preview.

  • Highlighting text in a table

    I want to highlight text (colored band) in a table. Not able to do so. The highlighting icon goes off when the cursor is on the text in table. It reappears when the cursor is outside the table.

    Hi Fruhulda,
    Thanks.
    The method shown below also works. But one has to be careful because among the menu options shown is "document color" (icon nos 4 from left). This is the correct one. I had been trying the icon for text color ignoring document color because it reads as if it for the entire document. Just now I tried the document color and it worked as a highlighter after the text was selected.

  • Can you change the color of highlighted text?

    I want to know if there's a way to change the color of text that has been highlighted, not the highlight itself. I know that to change the color of the highlight itself, you go to the Appearance Preference Pane and change the highlight from there, but I want to know if there's a way to change it so that when I highlight text, the text itself changes color for higher contrast against a dark colored highlight. I think MSWord does it, I have some memory of working on Windows computers back in the day working like that, is there a way to make that sort of change on a Mac?

    No, I know how to change the color of the HIGHLIGHT, I want to change the color of the TEXT that's being highlighted, so if, for example, I highight black text with a dark red/blue/black highlight color, the text goes white for greater contrast and doesn't turn invisible for lack of contrast. Is there a way of doing that?

  • How to highlight text with a translucent color

    I have to highlight text from a JTextArea, but the color is too dark... So I'd like to highlight with a translucent color. How can I do it ?
    Here is my code :
    JTextArea jt;
    int beginSelect;
    int endSelect;
    Highlighter hl = jt.getHighlighter();
    DefaultHighlighter.DefaultHighlightPainter hld = new DefaultHighlighter.DefaultHighlightPainter(Color.BLUE);
    try {
    hl.addHighlight(beginSelect, endSelect, hld);
    catch (BadLocationException e) {}
    Thank you.

    import javax.swing.plaf.*;
            Color color = new Color(220, 200, 240, 150);
            ColorUIResource colorResource = new ColorUIResource(color);
            UIManager.put("TextArea.selectionBackground", colorResource);Default for windows is [r=49, g=106, b=197]
    and for metal is [r=204, g=204, b=255].

  • Changing the hightlight color for all existing highlighted text

    Is there a quick way to change the hightlight color of all existing highlighted text in a document? Thanks.

    Never mind I found it. Open the Comment panel, select all existing comments, right-click and select Properties, change to your desired setting, click OK.

  • 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
              drawLine.addActionListener(
              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);
              );thanks..

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

  • 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 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
    and
    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
    encounter.
    Murray --- ICQ 71997575
    Adobe Community Expert
    (If you *MUST* email me, don't LAUGH when you do so!)
    ==================
    http://www.projectseven.com/go
    - DW FAQs, Tutorials & Resources
    http://www.dwfaq.com - DW FAQs,
    Tutorials & Resources
    ==================
    "rob717" <[email protected]> wrote in
    message
    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 a JTextField.

    Ok I've seen this mentioned a few times but I haven't seen my concern addressed yet. Basically I have a spell checking JDialog that interacts with a JTextComponent that is passed to it. So what I need to do is to be able to highlight an area in the JTextComponent based off of data in the JDialog. Ok I've got this working (using Highlighters and the addHighlight function) but it only highlights using the default color, I need it to also change the text color just like it is selecting it. I can't actually select it since the focus is on the JDialog and I only need it to look highlighted anyway, I don't need the selection funcitonality. In the Metal skin this is not a big deal, the highlight is blue and the text is black but in Motif and Windows the highlight color is too close to the font color so it looks like a black box with no text. This is how I do the highlighting :
    JTextComponent SourceArea = new JTextComponent(); // this is
    //actually passed into a function but this is good enough for an example.
    SourceArea.getHighlighter().removeAllHighlights();
    SourceArea.getHighlighter().addHighlight(Index,(Index+Temp.length()),new DefaultHighlighter.DefaultHighlightPainter(NullColor)); Is there a way to attach a new font to the highlighter (not that I could see) so that a highlighted area will contrast enough. Or is there a way to selectivly change the text attributes (so I can just drop the normal selectedTextColor into the attributes)? The attribute method should also allow other fun stuff like context sensitive attribute changes (nouns in Italic maybe and verbs in SanSerif for example). So does anyone have ideas and keep in mind that the SourceArea will not have focus at the time the words are highlighted.
    Thanks.

    If you just want to highlight one word at a time then you can use:
    textComponent.setSelectionStart(...);
    textComponent.setSelectionEnd(...);
    The above code will use the default selection background/foreground font colours.
    Here is an example of a Find/Replace dialog that uses this approach.
    http://www.discoverteenergy.com/files/FindReplace.java
    If you want to get fancier and use different styles of fonts and colours for different words then you have to use JTextPane which supports StyledDocuments.
    The basic approach is to create attribute sets as follows:
    MutableAttributeSet comment = new SimpleAttributeSet();
    StyleConstants.setForeground(comment, Color.gray);
    StyleConstants.setItalic(comment, true);
    Document doc = textPane.getDocument();Insert new text into the document with the 'comment' attribute:
    doc.insertString( doc.getLength(), "comment text", comment);
    Change the attribute of existing text in the document with the 'comment' attribute:
    doc.setCharacterAttributes( 0, 3, comment, false );
    Here is a simple Java syntax highlighter that uses this approach:
    http://www.discoverteenergy.com/files/SyntaxDocument.java
    Note, most is the above code is devoted to handling multi-line comments. The main highlighting code is in the 'checkForTokens()' method.

  • 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?
    Thanks

    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?

  • Highlighting text in a table cell

    Im trying to get text in a table have it background shaded a different colour to the rest of the table cell but it doesnt work. No exceptions are thrown does anyone have any idea.
    public Component getTableCellRendererComponent(JTable table,
            Object value,
            boolean isSelected,
            boolean hasFocus,
            int row,
            int column)
            setFont(table.getFont());     
            this.setText(value));
            this.selectAll();
            this.setSelectionColor(new Color(0,0,255,100);
            return this;
        }

    The answer is similar to what someone else asked recently for highlighting text in a JFormattedTextField cell.
    class MyFocusListener extends FocusAdapter
    public void focusGained(FocusEvent e)
    SwingUtilities.invokeLater(new Runnable()
    public void run()
    selectAll();
    Put that in your cell editor or its base class. You can change the color or whatever you want. I think the problem is a timing thing where normal focus processing is interrupting your changes. So give the focus processing time to run with this Runnable triggering your processing to occur on the Awt thread (as is proper) after the system is doing whatever it wants to do. Let us know if it works for you.

  • Highlighting text in Find

    Hello, while looking for text in a Mail message by doing Command-F the found text is highlighted in a faint grey box.  Is there a way to change the box color to make it easier to find on the page?  Thanks!

    Install espeak and write this script:
    sayit.sh
    #!/bin/sh
    espeak "`xsel -p -o`"
    Put that in a folder in your $PATH and bind a key to it. It'll speak any highlighted text anywhere in your X11 environment.

Maybe you are looking for

  • Error Uninstalling SharePoint Foundation 2013

    I foolishly installed Sharepoint foundation on an exchange server and it broke my access to ECP and OWA.  I have attempted uninstallig but get the message "One or more required Office components failed to complete successfully.  For more information,

  • How do I install OS 9

    Hi, Not sure if anyone can help me but here it goes. I bought my Mac from EBay because it was what I could afford at the time. It came with OS X 10.4.9 pre-installed and came with what I believe is all the original CD-ROMs. I am tring to get iDVD and

  • How to install BDB XML on shared server?

    Hi All, 1. I want to install BDB XML on a shared server (bluehost). Can anyone suggest me how to do it? Since I dont have enough permissions, some of the 'php shared extensions' are not being installed. I get warning like- Installing shared extension

  • Can my QuickTime Pro 7 for Windows License key be transferred to my new Mac

    I have used QuickTime Pro on Windows for the past 8-10 months. I have now moved from Windows to Mac. However, the Pro registration key is invalid on the Mac because it is Windows-specific. Will Apple convert my Windows key to a Mac key, or do I need

  • Slow wireless speed with new iMac Intel- problem with 802.11g?

    There IS a problem with the new Intel Mac, I have tried both the Intel iMac and the new Intel Mac mini and they both have the same problem with slow wireless speed (or rather it variates, if I do a ping to another computer on the network the speed go