no text highlight in a textarea

H

hijinks

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
 
R

Rhino

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

Thomas Fritsch

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

Bart Rider

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

Fred Kleinschmidt

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

Oliver Wong

Fred Kleinschmidt said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top