aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Multiple text selection in JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Multiple text selection in JTextArea" Watch "Multiple text selection in JTextArea" New topic
Author

Multiple text selection in JTextArea

Frank Truong
Ranch Hand

Joined: Nov 19, 2002
Posts: 39
I wonder how, if possible, I can maintain multiple text selection in JTextArea/JTextPane/JEditorPane.

Any hints are very appreciated.

Frank :roll:
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Originally posted by Frank Truong:
I wonder how, if possible, I can maintain multiple text selection in JTextArea/JTextPane/JEditorPane.

Any hints are very appreciated.

Frank :roll:


There might be something in JTextComponent#setHighlighter and Highlighter#addHighlight()

Note: This is just an off the cuff reply as I personally have never tried it.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

It isn't possible. In fact, I have yet to meet the first text editor / IDE which does allow for multiple text selections.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
G Estes
Ranch Hand

Joined: Oct 22, 2002
Posts: 62
Rob, I'm not sure you are correct. Open a Microsoft Word document (I know, I hate MS too). Now, use the mouse to select the first paragraph. Next, press the Control key and use the mouse to select a second paragraph elsewhere in the document. Do a "Copy" and then open a new Word document and do a Paste. The two paragraphs will show up on the document, but not the unselected portions of the original.

Now, can the current version of the JTextArea do this too? I don't know...but I may spend some time trying it out...
[ July 30, 2008: Message edited by: Gil Estes ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

I tried the CTRL thing in both a JTextArea and MS Word 2000, and neither could do it. Perhaps MS have added this in a later version.
G Estes
Ranch Hand

Joined: Oct 22, 2002
Posts: 62
I am testing in Word 2002, so not that recent of a thing. Haven't tested or tried to code to it in JTextArea. I'm not saying JTextArea is possible, just that there are apps that can do it...

Cheers!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
I am pretty sure I have got it to work on OpenOffice2.3.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Well that explains it. I never met those applications
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Originally posted by Frank Truong:
I wonder how, if possible, I can maintain multiple text selection in JTextArea/JTextPane/JEditorPane.

Ok.
I toyed around and came up with this. Of course this is not fool proof (e.g. it cannot figure out de-selection, but its a start.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Multiple text selection in JTextArea