Colors in a TextArea

Is there anyway to make a char or substring a different color from the rest of the text in a Text Area?

If you can use Swing. You will have JTextPane do the job. And JLabel can accept html too ( change color by using html code ).
If you must use AWT, you have to dvlp your own TextArea to do the job.

Similar Messages

  • How can I have different colors in a TextArea ?

    hi
    i�m writing a little chat, but i�ve a problem.
    I have a big TextArea, where all messages from the different chatters are shown. But with setForegroundColor( .. online the full TextArea is changed ?
    How can I do this ? Or should (mus) I override the method by myself ?!?

    You have to use the StyledDocument model for implementing the feature you require.
    You can set the font colors using StyleConstants.

  • Flash CS5 How does one change background color in TextArea Component

    I'm trying to change the background color of a TextArea Component.  Does anyone know how to do this?
    function processXML(e:Event):void {
      var strTextBox:String;
      myText.visible=true;
      myText.setStyle("contentBackgroundColor", 0x000000);  //Does not work..nor does   myText.setStyle("backgroundColor",0x000000);
      myXML = new XML(e.target.data);
      strTextBox = "<a href='" + myXML.Branch[0].linkURL + "'> More Info...</a>";
      myText.htmlText=strTextBox;

    for that one you have use styles:
    var newFormat:TextFormat = new TextFormat;
       newFormat.bold = true;
       newFormat.size = 18;
       newFormat.color = 0xFFFFFF;  //I'm wanting white font on black background.
       //apply the format:
    myText.setStyle("textFormat",newFormat);
       myText.visible=true;
      myText.textField.background=true;
      myText.textField.backgroundColor=0x000000;

  • Textarea color

    I am building a chat. Is it possible to have different rows in different colors in a textarea? Is there any solution, like adding colored labels into a textarea?
    I am java newbie!
    Thanks / Henrik

    I am java newbie!Okay, next time try searching before you ask. If you search this forum for "chat" I predict you will find hundreds of links, many asking this question. And the answer usually given is:
    If you are using Swing, then use a JTextPane or some such complex object. If not, then you can use a Canvas and draw the text on it yourself.

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

  • Colorful Web Forms does not work in IE like it does in Firefox

    Well, I have this great colorful web form that I got from Paulo Vale. The colors of the textareas, textfields, and select lists all change colors as they should when I mouseover them and when I set focus and type in them .......... if I am in Firefox.
    When I try to run my application in IE, none of this works.
    Does anyone know what I need to do to get this to work in IE like it does in Firefox?
    Thank you,
    Maggie

    Yes, I can provide the code.
    Thank you, Andy
    &lt;style type="text/css"&gt;
    select.slCamp {
    color:
    #000000;
    background-color:
    #FFFFCC;
    cursor:
    pointer;
    select:hover.slCamp {
    color:
    #000000;
    background-color:
    #E6FF97;
    select:focus.slCamp {
    color:
    #FFFFFF;
    background-color:
    #81AD00;
    textarea.taCamp
    background-color:
    #FFFFCC;
    textarea:hover.taCamp {
    background-color:
    #E6FF97;
    color: #000000;
    cursor: pointer
    textarea:focus.taCamp {
    color:
    #FFFFFF;
    background-color:
    #81AD00;
    cursor: text;
    textarea.taOptCamp
    background-color:
    #FFFFFF;
    textarea:hover.taOptCamp {
    background-color:
    #E6FF97;
    color: #000000;
    cursor: pointer
    textarea:focus.taOptCamp {
    color:
    #FFFFFF;
    background-color:
    #81AD00;
    cursor: text;
    select.slOptCamp {
    color:
    #000000;
    background-color:
    #FFFFFF;
    cursor:
    pointer;
    select:hover.slOptCamp {
    color:
    #000000;
    background-color:
    #E6FF97;
    select:focus.slOptCamp {
    color:
    #FFFFFF;
    background-color:
    #81AD00;
    &lt;/style&gt;
    Andy, anything that has an 'Opt' in the name is used with a field or list that is optional. I did this so that there would be a difference in the manddatory and optional fields.
    Edited by: mjhamilton on Jan 5, 2009 12:01 PM

  • AS3 TextArea Style

    I'm having an issue trying to change the font/font size/font
    color in my textarea component using AS3 in Flash CS3.
    For AS2 to style a comboBox for example it was as easy as:
    yourComboBox.rollOverColor = 0xe8e8e8;
    yourComboBox.textRollOverColor = 0x333333;
    yourComboBox.selectionColor = 0xe8e8e8;
    yourComboBox.textSelectedColor = 0x333333;
    But for AS3 I'm kinda lost.. Any help would be greatly
    appreciated. Attached is a copy of some code I'm working on.
    import fl.controls.*;
    var info_txt:TextArea = dyna_mc.info_txt;
    var date_txt:TextArea = date_mc.date_txt;
    var record_xml:XML;
    var xmlReq:URLRequest = new URLRequest ("file.xml");
    var xmlLoader:URLLoader = new URLLoader ();
    function xmlLoaded(event:Event):void
    record_xml = new XML(xmlLoader.data);
    date_txt.text = record_xml..Date[2];
    info_txt.text = record_xml..Aries[2];
    xmlLoader.load(xmlReq);
    xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
    Thanks
    Tim

    StyleManager.setStyle("embedFonts", true);
    StyleManager.setStyle("textFormat", new TextFormat(new
    iFont().fontName, 28, 0x000000));
    That will set the global style for all components used. If
    you want to set them separately, replace "StyleManager" with the
    instance name of the object.
    For this example to work, you'll need a font in your library
    that is set to export with Class name "iFont".

  • JTextArea selection color

    Hi
    I want to be abel to set diffrent colors in a textarea by selecting a
    interval of text och and then selecting a color.
    How is this done?
    I dont mean the selection color, the actual text. The purpose is to have an textarea with lots of news text, and be abel to color mark them for diffrent priority.
    Thanks.

    hi,
    JTextField, JPasswordField, and JTextArea each use a PlainDocument model. PlainDocument allows one font, one color, and is limited to character content. JEditorPane and JTextPane use a StyledDocument model. StyledDocument is much more complex, allowing multiple fonts, colors, embedded images and components, and various sets of hierarchically resolving textual attributes. So either use JEditorPane or JTextPane as per u'r requirement -
    -manoj

  • Style TextArea

    Hello,
    I am trying to change the background color of the textarea using in-line css.
    responseBox = new TextArea();
    responseBox.setStyle("-fx-font: 14px 'Courier New';-fx-background-color: #000000;");The background color should be black, but it remains as defult (white).
    What am i doing wrong?
    How do I change the color of the font within TextArea?
    Gabor

    Hi Darryl,
    It doesn't work for me. I got this error message permanetly on the console on the other hand either using style or not:
    knownConverters does not contain mapping for javafx.geometry.Insets
    INFO: com.sun.javafx.css.StyleableProperty error
    Styleable [email protected]
    (inherits=false, converter=, initial=2, property=-fx-margin) for
    com.sun.javafx.scene.control.skin.TextInputSkin margin:
    Could not infer 'converter'I saw in a previous post that one experienced this and he was told that TextArea is buggy.
    Thanks, Gabor

  • TextArea sizing question

    hi,
    i have a TextArea. when i try to set its size it does not work before the window is shown.
    after the window is shown there is no problem with the setSize.
    any idea why?
    import java.awt.*;
    import java.awt.event.*;
    public class Components extends CloseableFrame
         TextArea textArea = null;
         public static void main(String[] args)
              Components f = new Components();
              f.setBounds(100, 100, 340, 300);
              f.setVisible(true);
         public Components()
              super("Components");
              setLayout(new java.awt.FlowLayout(
                   java.awt.FlowLayout.CENTER, 20, 10));
              textArea = new TextArea("my text", 20, 40, TextArea.SCROLLBARS_VERTICAL_ONLY);
              textArea.setForeground(Color.red);
              add(textArea);
              textArea.setSize(10, 10); // <<=== NOW WORKING !!!!!!
              Button btn = new Button("button");
              add(btn);
              btn.addActionListener(
                   new ActionListener() {
                        double sizeFactor = 0.5;
                        public void actionPerformed(ActionEvent e) {
                             Dimension size = textArea.getSize();
                             size.width = (int) (size.width * sizeFactor);
                             size.height = (int) (size.height * sizeFactor);
                             textArea.setSize(size); // <== WORKING !!!
                             if (sizeFactor == 0.5)
                                  sizeFactor = 2;
                             else
                                  sizeFactor = 0.5;
    }

    When you call setSize or pack the layout manager collects the preferred sizes of the components and lays them out. Until then it doesn't have the information available to give you.

  • OwnerDraw TextArea

    Hello:
    Is there a way to control all drawing in a TextArea just like one can for a List Control in Visual C++. The idea I have is to be able to change the color of text on a word by word basis inside a text area.
    Thank you for your help,
    Dan

    You can't change text color in a textarea. To do this, you've got to use a TextPane. Check out the tutorial for text components on this site

  • Defaults.getColor("some color object") question

    Hi:
    I am using the UIDefaults and UIManager to get the default color
    of some component. Then, I set this default color to my textArea's background. Everything works fine. But when I tried to print out the default color using
    System.out.println(defaultColor.toString());It throw a NullPointerException and tells me that the defaultColor is NULL. I am kinda confused about this. If the defaultColor is null, then howcome I can do
    mytextArea.setBackground(defaultColor); and it worked fine??
    Thanks

    The doc for setBackground() says that you can pass it a null parameter:
    public void setBackground(Color c);
    the color to become this component's color; if this parameter is null, then this component will inherit the background color of its parent

  • Question Re: Color Codes/Component Style Declaration

    I am not understanding the use of hexidecimal color codes for
    component styles. What is the "0x" for? This is not addressed in
    flash help. What I am trying to figure out is if I can specify
    alpha transparency with style declaration and I'm wondering if the
    seemingly superfluous prefix may have something to do with it.
    Either way I'd at least like to know what it means.
    Thanks in advance,
    JeevesQuimby

    JQ you could actually pass that number in decimal value or
    string. example:
    //blue
    myTextArea.setStyle( "backgroundColor", "blue" );
    // red
    myTextArea.setStyle( "backgroundColor", 0xFF0000 );
    // green
    myTextArea.setStyle( "backgroundColor", 65280 );
    the real color number is actually the decimal number, but
    when ported to an
    hexadecimal system it's just easier to use... Check the doc
    on getPixel32 in
    flash, on top of RGB values you also have alpha values, which
    gives a number
    like 0xAARRGGBB
    JG
    "JeevesQuimby" <[email protected]> wrote in
    message
    news:ejntk8$74h$[email protected]..
    >I understand hex codes, it just seems to me that if
    you're specifying a
    >color
    > as in: TextArea.setStyle("backgroundColor", 0xFFFFFF);
    flash would be
    > expecting
    > the color code, so the 0x seems unnecessary, since it is
    always the same.
    > Any
    > other program identifies the color with just 6
    characters. What would
    > happen if
    > I were to put 2xFFFFFF for example? It just seems wierd
    to me. Thanks
    > anyways
    > though.
    > JQ
    >
    >
    >

  • Dissolve image and textarea?

    Hi!
    I'm currently working on a small Flex-application that
    rotates ads on a web page. The concept is simple: an image is
    dissolved, followed by dissolving text on top of the image.
    I have no problem with the image but when I try to dissolve
    the text, the underlying image is also dissolved.
    Anyone know what I'm doing wrong or how I should go about
    making the ad-rotator work as intended?
    I'm a newbie on this, but I send along some of the MXML-code
    I have:
    <mx:states>
    <mx:State name="RotatingState"/>
    </mx:states>
    <mx:transitions>
    <mx:Transition fromState="" toState="RotatingState">
    <mx:Sequence>
    <mx:Dissolve target="{ImageCanvas}" alphaFrom="0.0"
    alphaTo="1.0" duration="3000"/>
    <mx:Dissolve target="{QuestionTextArea}" alphaFrom="0.0"
    alphaTo="1.0" duration="3000"/>
    </mx:Sequence>
    </mx:Transition>
    </mx:transitions>
    <mx:Canvas width="100%" height="100%">
    <mx:Canvas id="ImageCanvas" width="100%"
    height="100%">
    <mx:Image id="CampaignImage" width="140" height="240"
    x="0" y="0" source="1.jpg"/>
    </mx:Canvas>
    <mx:Canvas id="QuestionCanvas" width="100%"
    height="100%">
    <mx:TextArea id="QuestionTextArea" width="140"
    height="240" x="0" y="0" borderStyle="none" backgroundAlpha="0.0"
    wordWrap="true" text="Question text goes here!"/>
    </mx:Canvas>
    </mx:Canvas>
    I'd be grateful if someone could shed some light on this!
    //Marcus

    I think it's because you're setting the TextArea
    non-transparent at one point, and it is being displayed "on top" of
    the picture, overwriting it. If you move the text area over a
    little (set it's x value to 220 or something), you'll probably see
    what I mean (if the *overall* background color of your app is not
    white, because the TextArea's background color is white...)
    The text area consists of a big white background, initially
    transparent (as you specified in the tag), but you say
    alphaTo="1.0" which seems to make the backgroundAlpha="1.0".
    Does that make sense to you?
    (Below I changed the background color of the TextArea and
    moved it over so you can see what's happening, maybe this will shed
    some light on things.) I had to add a button to trigger the state
    change for me, too.
    <mx:states>
    <mx:State name="RotatingState"/>
    </mx:states>
    <mx:transitions>
    <mx:Transition fromState="" toState="RotatingState">
    <mx:Sequence>
    <mx:Dissolve target="{ImageCanvas}" alphaFrom="0.0"
    alphaTo="1.0" duration="3000"/>
    <mx:Dissolve target="{QuestionTextArea}" alphaFrom="0.0"
    alphaTo="1.0" duration="3000"/>
    </mx:Sequence>
    </mx:Transition>
    </mx:transitions>
    <mx:Canvas width="100%" height="100%">
    <mx:Canvas id="ImageCanvas" width="100%"
    height="100%">
    <mx:Image id="CampaignImage" width="140" height="240"
    x="0" y="0" source="assets/1.jpg"/>
    </mx:Canvas>
    <mx:Canvas id="QuestionCanvas" width="100%"
    height="100%">
    <mx:TextArea id="QuestionTextArea"
    backgroundColor="#882222" width="140" height="240" x="220" y="0"
    borderStyle="none" backgroundAlpha="0.0"
    wordWrap="true" text="Question text goes here!"/>
    <mx:Button x="193" y="49" label="Button"
    click="dis();"/>
    </mx:Canvas>
    </mx:Canvas>
    <mx:Script>
    <![CDATA[
    private function dis():void {
    currentState='RotatingState';
    ]]>
    </mx:Script>

Maybe you are looking for