Current Position:Home > Highlighting text fieldon focus with components

Highlighting text fieldon focus with components

Update:10-11Source: network consolidation
Advertisement
Is there a way to highlight a text field when the focus is in that field. I know on a web page you can do this with JS, but wasn't sure about with AS. I am using components also.

The Best Answer

Advertisement
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 fieldon focus with components Update:10-11

    Is there a way to highlight a text field when the focus is in that field. I know on a web page you can do this with JS, but wasn't sure about with AS. I am using components also.If you just want to highlight one word at a time then you can use: textC

  • Highlighting text in a table cell Update:10-11

    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,         O

  • Highlighting text won't stay on Update:10-11

    When I highlight text it often won't stay on, and when it does and I try to move the selection, it just beeps at me and sits there.  What's up?Ok. In System Preferences > General, Apple applies the term "Highlight" instead of "Selection&

  • How to highlight text in OA page Update:10-11

    I am adding some javascript logic in my OAF page and would like to be able to highlight words in the oamessagetextinput bean. Is this possible?Its not recommended to use JavaScript to set the focus and cause the highlight. Firstly, its against the co

  • Changing highlight text color / using Highlighters Update:10-11

    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){

  • [SOLVED] Highlighted text in the shell, appearance rxvt-unicode Update:11-30

    Hi, Short question, when I highlight text in the shell or when text get highlighted in ncmpcpp or outer app running in the shell it gets basically impossible to read. Because background to text get the same color and strength then the text. what sett

  • Find an highlight text with Excel vba an Acobat XI Pro Update:11-30

    Hello, i want to write a VBA Script to search for and highlight text. Until now my Script open the PDF and search for text. But how can i highlight it ? Thank you. Axel My Code : Sub OpenPDFPageView()     Dim PDFApp As AcroApp     Dim PDFDoc As AcroA

  • Highlighting Text & Auto Text Options Update:10-11

    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 num

  • 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. Update:10-11

    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 com

  • Highlighting Text Update:10-11

    Hello 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.