Highlighting text

I’m using a Formatted Text Field to restrict users input, before checking the value entered in code. If it is out of range, I warn the user and then reset focus to the control withdef resetFocus(textbox = event.source): textbox.requestFocusInWindow() system.system.invokeLater(resetFocus) As well as resetting the focus I also want to highlight all the existing text in the text field. How do I do this?

You could use html coding to do this. I have a window where I progromatically highlight certain words in the text, using something like this:

<FONT STYLE="BACKGROUND-COLOR: yellow">This is the highlighted text</font>

When I say highlight I mean the selection highlight (like when you click at the beginning of a word and drag you mouse over the rest of the word).

I want to highlight the existing content of the text field so that if the user types something the existing content is overwritten.

Couldn’t you use the java clipboard and progromatically select it? In fact, you posted some code in the FPMI forum that could get you pretty close.

I could if I could remember how! I’ve been looking for that post but couldn’t find it - do you have a reference?

It shows up if you do a search on ‘clipboard’. It’s from March 2010.

That post deals with reading already highlighted text using the getSelectedText() function. A quick Google seems to suggest that likely candidates for setting the selection text are setSelectionStart() and setSelectionEnd(), but they don’t seem to have any effect.

I have noticed that using requestFocusInWindow() with a standard Text Field automatically selects all the text in the field, but not with a Formatted Text Field.

I had to write to the clipboard on another system I was working on, and maybe there is something you could use in this post: http://inductiveautomation.com/forum/viewtopic.php?f=23&t=4379&p=10176&hilit=clipboard#p10176

I wonder if you could point the text property to the setContents parameter… No matter what, post your solution here. I think it could be useful. :slight_smile:

1 Like