I need to make an applet to view sourcecode and not be able to
highlight or select any of the text to copy it.. is there a way to make
a textarea do this.. or am I gonna have to make my own widget to handle
this?
Thanks
Rhino - 09 May 2006 15:43 GMT
>I need to make an applet to view sourcecode and not be able to
> highlight or select any of the text to copy it.. is there a way to make
> a textarea do this.. or am I gonna have to make my own widget to handle
> this?
I haven't tried this recently but I _think_ if you set the area disabled you
won't be able to highlight it or select any of it:
JTextArea myTextArea = new JTextArea("bla bla bla");
myTextArea.setEnabled(false);
Of course, this will grey out the text too, which you might not like too
much since it makes the text harder to read.
If you simply want to prevent anyone from dragging and dropping all or part
of the text, I think
myTextArea.setDragEnabled(false);
should do the job. The user will probably still be able to highlight parts
of the text area - again, I haven't tried this in a while so I could be
wrong - but they won't be able to drag it anywhere so they can't do much
harm.
--
Rhino
Thomas Fritsch - 09 May 2006 16:01 GMT
hijinks@gmail.com schrieb:
> I need to make an applet to view sourcecode and not be able to
> highlight or select any of the text to copy it.. is there a way to make
> a textarea do this.. or am I gonna have to make my own widget to handle
> this?
JTextComponent (and all of its subclasses: JTextArea, JTextField,
JEditorPane) has a method
public void select(int selectionStart, int selectionEnd)

Signature
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
Bart Rider - 09 May 2006 16:24 GMT
> I need to make an applet to view sourcecode and not be able to
> highlight or select any of the text to copy it.. is there a way to make
> a textarea do this.. or am I gonna have to make my own widget to handle
> this?
>
> Thanks
The swing text components can show html-formated text. I recommend
looking to the java tutorial for some help:
http://java.sun.com/docs/books/tutorial/uiswing/components/text.html
Using this you might search/replace words in your sourcecode
to insert html-color-style-tags. Best to use regular expressions
for searching and replacing :)
I remember to have written a makro programm in visual basic to
format pascal sourcecode for MS word. Ahh, ... Good old times.
Fred Kleinschmidt - 09 May 2006 17:51 GMT
>I need to make an applet to view sourcecode and not be able to
> highlight or select any of the text to copy it.. is there a way to make
> a textarea do this.. or am I gonna have to make my own widget to handle
> this?
>
> Thanks
As others have said, try setDragEnabled().
But the real question is: Why do you need to do this? If you are trying to
keep your source code "provate", so that is cannot be copied, you
are fooling yourself by making it non-draggable. What's to keep
the user from opening another window and typing it in manually?

Signature
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Oliver Wong - 09 May 2006 20:21 GMT
>>I need to make an applet to view sourcecode and not be able to
>> highlight or select any of the text to copy it.. is there a way to make
[quoted text clipped - 3 lines]
> What's to keep
> the user from opening another window and typing it in manually?
Or using OCR, or disassembling your applet to figure out where you're
getting the strings from, and getting those strings directly.
- Oliver