views:

80

answers:

1

Hey everyone,

I have a very simple Swing GUI with just a JTetxtArea. I am trying to programmatically select a part of text using:

textArea.select(startSelection,endSelection);

This work. However as soon as I add some other components to the GUI I do not see selection anymore

frame.getContentPane().add(button);     
frame.getContentPane().add(textArea);
textArea.select(startSelection,endSelection);

I suspect that during layouting the gui, some event causes the text to be deselected. Am I right? And could anybody suggest a solution?

My goal is to have a program which displays a text, and allows the user to input start and end selection position, and a selection appears between these two position. Thank you.

+1  A: 

Text selection only shows when the text component has focus.

Text components also support "highlighting" by using the getHighlighter().addHighlight() method. In this case the highlighting remains whether the component has focus or not.

If you need more help post your SSCCE that demonstrates the problem.

camickr
This did solve the problem! Thanks!!!
HH