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);
}

Similar Messages

  • How do i change the text color in ichat?

    How do i change the text color in ichat?

    Hi,
    In One Chat by word or letter go to the Format Menu as you type (Highlight and change like you would in any other App)
    On a more permanent basis the Font, it's colour and the IM background colour are set in iChat Menu > Preferences > Messages
    10:12 PM      Wednesday; November 23, 2011
    Please, if posting Logs, do not post any Log info after the line "Binary Images for iChat"
      iMac 2.5Ghz 5i 2011 (Lion 10.7.2)
     G4/1GhzDual MDD (Leopard 10.5.8)
     MacBookPro 2Gb (Snow Leopard 10.6.8)
     Mac OS X (10.6.8),
    "Limit the Logs to the Bits above Binary Images."  No, Seriously

  • How do I change the text color from blue in Safari on Macbook Pro?

    I have trouble seeing the light blue text color -- many article use this.  How can I change the text color?

    Is this a particular website where you see the light blue color?
    If so, please post a link to that site in your Reply.
    Exactly which light blue color are you referrting to ??

  • How do I change the text color in a call out box?

    How do I change the text color in a call out box?

    See http://help.adobe.com/en_US/acrobat/X/standard/using/WS58a04a822e3e50102bd615109794195ff-7 e5a.w.html
    Also http://helpx.adobe.com/acrobat/kb/unable-edit-properties-text-box.html

  • How to change the text color to red in a combo box?

    I'm writing a java program which have a combo box which shows all the name of the member from the database. However, I would like to change the text color of those member who have now currently on-line.
    Please help, it's URGENT. Thanks in advance.
    Clark

    hi,
    as i mentioned, you would require to use a custom renderer for this, for this
    //Class subclass the JFrame and has a JList in it
    import javax.swing.*;
    import java.awt.*;
    import java.util.Vector;
    public class ListRendererTest extends JFrame
         private JList lstMenu;
         private DefaultListModel defaultListModel;
         private JScrollPane scrollPane;
         private Vector listVector;
         public ListRendererTest()
              init();
              addComponents();
              showFrame();
         public void init()
              lstMenu = new JList();
              defaultListModel= new DefaultListModel();
              lstMenu.setModel(defaultListModel);
              scrollPane      = new JScrollPane(lstMenu);
              listVector = new Vector();
              MyListData m1 = new MyListData();
              m1.setName("Rakesh");
              m1.setOnline(false);
              listVector.addElement(m1);
              m1 = new MyListData();   //represents each User instance
              m1.setName("Makesh");
              m1.setOnline(true);
              listVector.addElement(m1);
              for (int i=0;i < listVector.size(); i++)
                   defaultListModel.addElement(((MyListData)listVector.elementAt(i)));
              lstMenu.setCellRenderer(new MyListRenderer());  //set custom renderer
         public void addComponents()
              getContentPane().add(scrollPane,BorderLayout.CENTER);
         public void showFrame()
              setTitle("List renderer test");
              setSize(300,300);
              setLocation(200,200);
              setVisible(true);
         public static void main(String args[])
              new ListRendererTest();
    }The above class is the Container which houses the JList in it, it uses two other classes, MyListData which is used to represent each user instance ( username, and information about whether he is online) and MyListRenderer (custom renderer).
    // Represents each user instance //
    public class MyListData
         private String name;
         private boolean online;
         public void setName(String name)
              this.name = name;
         public String getName()
              return name;
         public void setOnline(boolean online)
              this.online = online;
         public boolean isOnline()
              return online;
    //custom list renderer
    import javax.swing.*;
    import java.awt.*;
    public class MyListRenderer extends DefaultListCellRenderer
         private MyListData myListData;
         public Component getListCellRendererComponent(JList list, Object value, int index,  boolean isSelected, boolean cellHasFocus)
               myListData = (MyListData)value;
               setText(myListData.getName());
               setBackground(myListData.isOnline() ? Color.red: Color.white);  //check if online, if so show in different color
               setForeground(isSelected ? Color.blue : Color.black);
               return this;
    }hope that helps.
    cheerz
    ynkrish

  • How do change the text color in the variable screen ?

    Hi Experts ,
    I would like to know about , How do change the text color in the variable screen ?
    Using web templates (Analytical) can get the output. It has the variable screen contains 6 fields (Company code, Country , Region , COB, Plant and Purchasing Group). I want to make RED color text on Plant. Please help me .
    Thank you ,
    Prasad.

    Hi,
    I am looking for nearly the same. What I have found is that it seems to manipulate the SAP theme that is used in standard when template is executed in the portal. Just display the source code of the HTML and there you will see the included SAP theme (normally SAP_TRADESHOW). Then you have to go to the SAP portal and change this stuff. But for that you have to know where to find it and what impact this change has.
    I am not pretty sure if this is the right way. But as I want to change the standard layout of a whole template to a customer specific layout I think there is no other way in BI7.0.
    Regards,
    Peter

  • Possible to change review text color and wording

    I've searched high and wide to try changing the text color for the review mode field "CORRECT ANSWERS" that shows on a fill in the blank/ short answer question. Ideally I'd like white text (it's set at black) and I'd like to have it say "Correct answer(s)" or even somethinge else. I don't like the all caps that's used either.
    I've looked at the Object Style Manager but I don't see this particular field available/editable. Has anyone found where in the c:\Program Files\Adobe... files this setting is controlled - a .dll, .xml, .txt? I don't mind changing it to white there if I have to.

    Hi, thanks. Cap 7. I whipped up that course from scratch, no particular theme applied. Also to answer your question, I looked at the master slide and that particular review mode field isn't present, just the initial objects like the text input field.It's frustrating how everything but the CORRECT ANSWERS label is editable via the Object Style Manager. Why they forgot to include it is beyond me. I'm left at using "Hack a Cap" methodology by looking at all the .dll, txt. xml files hoping to fix it there because as far as I know you can't just turn the CORRECT ANSWERS field off even if you wanted to. Unfortunately almost all of our screenshots in our courses will have a royal blue background.

  • Change MVThemeBasedFOI text color

    Hi to all,
    I'm using Oracle maps, the last version that i've downloaded.
    I've defined a layer:
    stradeLayer = new MVThemeBasedFOI('stradeLayer','DBTI.STRADE');
    stradeLayer.enableAutoWholeImage(true);
    stradeLayer.setVisible(false);
    stradeLayer.enableLabels(true);
    for this layer i use some style rules:
    <?xml version="1.0" standalone="yes"?>
    <styling_rules>
    <rule column="DUG,NOME">
    <features style="L.RULE LIGHT 96">
    SELECT TP03_GEOMETRIA,
    D.TP17_DESCRIZIONE as DUG,
    T.TP07_NOME AS NOME,...
    </features>
    <label column="VIA_NOME" style="T.SHIELD2"> 1 </label>
    </rule>
    </styling_rules>
    is it possibile to change temporarily text color or font,
    could you suggest something?
    Thank in advace,
    Francesco

    Add the following style rule to your document
    ul.MenuBarHorizontal ul a {
        color: red;
    changing the colour to suit.

  • Changing Label text color based on some conditions

    Hi,
    I have these labels and I would like to change the text color IF the text has some specific value (coming from an XML created by a program); for example the default text color is red, but if the text value is "Yes", the text color should be green.
    Any helps much appreciatted, as usual...
    Thanks.

    unsure, but you might be able to get away with something like this:
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
        <mx:Script>
            <![CDATA[
                public function changeColor(value:String):uint
                    if(value == "Yes")
                        return 0x007F00;
                    else
                        return 0x7F0000;
            ]]>
        </mx:Script>
        <mx:Panel x="343" y="199" width="250" height="200" layout="absolute">
            <mx:Label id="lbl1" text="Yes" color="{changeColor(lbl1.text)}" x="102.5" y="10"/>
            <mx:Label id="lbl2" text="BlahhhhhBlahhhh" color="{changeColor(lbl2.text)}" x="97" y="36"/>
            <mx:Label id="lbl3" text="Exampleeeeeeee"  color="{changeColor(lbl3.text)}" x="97" y="62"/>
        </mx:Panel>
    </mx:Application>
    using a single label in an item renderer, passing across the 'data.text' value

  • I udate my os to 10.6.7 and since then my Pages have color problems. The text is not visible (white on white background) it does'nt help to change the text color nor background color. Pls help

    I udate my os to 10.6.7 and since then my Pages have color problems. The text is not visible (white on white background) it does'nt help to change the text color nor background color. I just download the recent trail version of Iworks and install it. The first document I opened it was ok. When I open the second one the problem appear again and stayed. Pls help

    This behavior was described many times.
    In every occurrence,the operating system was an uncompleted one resulting of the use of Software Update to update the operating system to 10.6.2, 10.6.3, 10.6.4, 10.6.5, 10.6.6, 10.6.7.
    In such cases, the solace was to apply a combo updater.
    As you are using 10.6.7, download and apply the combo 10.6.7 :
    http://support.apple.com/kb/DL1361
    After that, run Software Update which will urge you to apply :
    Snow Leopard Font Update
    Yvan KOENIG (VALLAURIS, France) dimanche 29 mai 2011 21:15:58
    iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.7
    Please : Search for questions similar to your own before submitting them to the community
    To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

  • How do I change the text color of footers on select pages? (InDesign CS6)

    Good afternoon,
    Months ago, I worked with a graphic designer to put together a 100-page workbook for my training company. He turned all of the files over to me and I'd like to make a small change before we print another run for a new client. Specifically, I'm having trouble changing the text color on select pages. Since a picture says 1,000 words, let me give you an example:
    As you can see, the copyright information on the right side of the page is difficult to read here when placed over the photo (you may have to look closely, but it's along the guy's sleeve). What I'd love to do is change that part of the footer (not the left, page number side) to white text. This would make it quite a bit easier to read. There's a number of other pages in the book that I want to make this change to. I'm not sure why I never noticed it or mentioned it to the designer. I suppose I've become more discerning since then.
    Let me tell you what I've tried to do:
    I learned how to override master pages by CTRL+SHIFT clicking on the footer. However, this has not worked for me. It allows me to change the page number part, but not the copyright side. Additionally, sometimes when I override the master the formatting of the footer goes goofy. For instance, the left side and right side come together and end up in the middle of the page I'm working on.
    I'm looking for a quick way to go through and make the footers easier to read on pages where they are backed by a photo. Hoping you can help me. Please let me know if you need any other information.
    Best,
    Taylor

    I think you should have left it as it was. It seems like what you had was single pages flowing as spreads within InDesign. Like below. That is the correct way, and when you changed it to all single, it did create your footer problems.
    Will you be sending the file as a PDF to your online printer? If so, then leaving the InDesign file set as below is fine. The PDF output will separate the pages.
    All you have to do is make sure that Pages, not Spreads, is chosen when you output the PDF

  • How do you change the text color in your junk mail folder

    How do you change the text color in your junk mail folder?

    Try Mail Preferences/Junk tab and under " When Junk Mail Arrives" click Performe Custom Action then advanced
    you will see the option for colors.
    Hope this helps

  • How do you change the text color in io7 from white to black?

    How do you change the text color on the home page in iO7 from white to black?  I tried to invert the color but that reverses everything inclufing pictures.

    Yes, you can. If you change your wallpaper to a very light or white color the text turns black.

  • Changing hyperlink text colors.

    I saw few posts asking about changing hyperlink text colors. And there are many camps regarding this topic. Suzanne Boben would recommend editing template XML . Some would recommend editing published HTML code. Some would recommend editing CSS code.
    But it came down to couple things.
    1) proficient in XML, HTML or CSS
    2) tried different colors, but it did turned out as expected
    One easy way is editing CSS files, this only apply to pages' hyperlink text; if your text got turn to PNGs, read no further .
    iWeb generates a CSS file for the associated HTML page - CSS file is in the Page_files folder.
    Edit the CSS files and keep copies of them, then you won't have to edit them again the next time you publish you sites.
    These CSS files are quite easy to understand, all you have to do is to look for a { , a:visited { and a:hover { .
    They contain RGB (Red Green Blue) values for link color, visited link color and rollover color.
    But what about those #8000FF and #4c4c4c?
    Well, I know they are RGB hexadecimal color values, but I don't what colors they are, so I came up with Argiope (Argiope is a family colorful spiders).
    It's a colors picker application to help with choosing background color, link color and rollover color. It'll give you the exact RGB hexadecimal value of chosen color.
    You can find Argiope here. Enjoy

    It can be done, but I have to do too much works.
    I already have a a script to do that, but it's quite complicated for an average bear.
    http://www.geocities.com/[email protected]/iWeb_utils --> LinksColors.zip
    Does just that, it is flexible, you can configure the colors for individual page, but it is quite different to configure. Look at the colors.txt and page.txt.
    If you really want to know more, ping me.

  • How can I change the text   color in pages

    Hi , how can I change the text color in pages app

    Select the text you want to change.
    Tap the i in the top bar.
    Tap Style in the top bar.
    Tap the Font name/size
    Tap Color then choose the color you want the text.

Maybe you are looking for

  • Cannot login to my usual account after power outage

    Had two power outages last night; machine survived the first one, but after the second outage and restoration I cannot get in to my usual user account. What happens: Grey apple screen, normal Blue screen with progress bar indicating that OS X is load

  • Asking for user input in the middle of a function

    Here's my issue. I need to launch and input window in the middle of a function for user input.  Before I can continue through the function I need a response back from the user first.  Psuedo code below: function      function begins      pop up windo

  • Hardware best suited (Windows) to run Photoshop?

    I am looking at upgrading my system to run Photoshop/Lightroom. I was looking at a AMD system with an FX-6300 processor (8 cores), 12gb of memory and the R9 255 graphics card w/ 2mb of dedicated memory. Will this system run those programs quickly and

  • When I open a new tab it no longer goes to top sites.

    When I open a new tab it no longer goes to top sites. I started after I downloaded some codex so i could play a video on line but it downloaded a tool bar and another program. I have since uninstalled the program disabled the plug ins but when I open

  • Select into aggregate function

    Hi, I have the following PL/SQL block that I am testing. The block reads a list of values from a local file and inserts the values into a select statement that uses an aggregate function. Here is my code: DECLARE f utl_file.file_type; n COLa%TYPE; --