Forums Register Login

disable selection on double click in textpane

+Pie Number of slices to send: Send
For my application i want to disable the textpane from selecting a word when double click is performed on the word. Is there a way to do this?

+Pie Number of slices to send: Send
The only relatively easy and implementation-independent way I can see is to cache the JTextPane's MouseListeners to an instance field, remove them from the JTextPane and add your own listener which loops over the cached listeners invoking their methods, but in mouseClicked(...) and mousePressed(...) tests for the mouse button being BUTTON1 and clickCount > 1 (assuming you also want to prevent line selection by triple-click).

A more correct but possibly L&F dependent approach would be to create a custom UI extending BasicTextPaneUI and override createCaret to return a custom subclass of BasicCaret that overrides mouseClicked(...) / mousePressed(...) to return without action when getButton() == BUTTON1 && getClickCount() > 1, otherwise invoke the super implementation.


edit: What about selection by dragging? or via the keyboard?
+Pie Number of slices to send: Send
 

Darryl Burke wrote:A more correct but possibly L&F dependent approach would be to create a custom UI extending BasicTextPaneUI and override createCaret to return a custom subclass of BasicCaret that overrides mouseClicked(...) / mousePressed(...) to return without action when getButton() == BUTTON1 && getClickCount() > 1, otherwise invoke the super implementation.

edit: What about selection by dragging? or via the keyboard?



Thanks for the input Darryl, I think I prefer the second approach, I will look into this approach further. What did you mean by L&F dependent apporach?

Selection via dragging and keyboard will also require some custom handling. I guess again in that case also extending the TextPaneUI and handling keyboard inputs etc. would be more practical.

+Pie Number of slices to send: Send
 

Poorav Chaudhari wrote:Thanks for the input Darryl, I think I prefer the second approach, I will look into this approach further. What did you mean by L&F dependent apporach?


You're welcome. For this approach I suggested extending BasicTextPaneUI, which also happens to be the UI delegate used by the MetalLookAndFeel. It's entirely possible, even probable, that some custom L&Fs may use their own UI delegate which extends the one from javax.swing.plaf.basic, so to retain whatever customizations are provided by that L&F you would need to extend that class instead of BasicTextPaneUI -- or end up with a text pane that doesn't visually gel with the rest of the GUI.


Poorav Chaudhari wrote:Selection via dragging and keyboard will also require some custom handling. I guess again in that case also extending the TextPaneUI and handling keyboard inputs etc. would be more practical.


The real work is in extending BasicTextUI.BasicCaret as that is the class where all this happens. But it probably won't be as difficult as it looks, because you'll be removing behavior rather than adding custom behavior. So long as you don't trip up on private fields and/or private or package-private methods -- the bane of Swing customization.

Good luck!

db
+Pie Number of slices to send: Send
Hi Daryl, In fact the textpane will have lots of custom behaviour, and maybe you can give me some more pointers on how you think i should approach this. I am fairly new to swing development and I haven't realized the 'power' of extending components. For starter, I have extended the JTextPane component because i needed to disable to line wrapping functionality. So my first question ixts, can i still implement a similar approach as you suggested with JTextPane instead of BasicTextPaneUI. I looked at the JTextPane API and I don't see any method similar to createCaret, so I don't know how this can be possible.

Secondly, I would love to hear your input on some design related question. In my application, the text entered in the textpane is going to be a 'coded' language. So it should be able to parse the text and mark invalid words. certain words will be highlighted automatically, others will be highlighted depending on its 'property'. So, each word will have an object associated with it and it's properties can be changed. Properties will be changed by clicking on the word and a dialog box will come up to change its properties. Words will be further divided into subwords, and these could be highlighted differently from the rest of the word (but we can leave that from this discussion).

My challenges are tracking the word with the object, especially because if you delete a word or insert a word in the sentence.

At the moment I am achieving some of this by just going through the entire text everytime a key is pressed and marking the invalid words and highlighting others. But somehow i don't feel this is a very good design and if it makes sense to extend the textpane to handle this inherently, and if yes how?

I would really appreciate some inputs on this.

Thanks.
+Pie Number of slices to send: Send
 

Poorav Chaudhari wrote:Hi Daryl, In fact the textpane will have lots of custom behaviour, and maybe you can give me some more pointers on how you think i should approach this. I am fairly new to swing development and I haven't realized the 'power' of extending components. For starter, I have extended the JTextPane component because i needed to disable to line wrapping functionality.


Rob Camick's No Wrap Text Pane may help there.

Poorav Chaudhari wrote:So my first question ixts, can i still implement a similar approach as you suggested with JTextPane instead of BasicTextPaneUI. I looked at the JTextPane API and I don't see any method similar to createCaret, so I don't know how this can be possible.


The Caret belongs to the UI delegate, not the component.

Poorav Chaudhari wrote:Secondly, I would love to hear your input on some design related question.


Not really. I'm not a programmer and have absolutely no education, formal or otherwise, in programming.

However, there are many skilled and experienced professionals here who would be in a position to advise you on design.

Poorav Chaudhari wrote: In my application, the text entered in the textpane is going to be a 'coded' language. So it should be able to parse the text and mark invalid words. certain words will be highlighted automatically, others will be highlighted depending on its 'property'.


Sounds a lot like syntax highlighting <- Google

Poorav Chaudhari wrote:So, each word will have an object associated with it and it's properties can be changed. Properties will be changed by clicking on the word and a dialog box will come up to change its properties. Words will be further divided into subwords, and these could be highlighted differently from the rest of the word (but we can leave that from this discussion).


I think we'd better, you lost me there.

Poorav Chaudhari wrote:My challenges are tracking the word with the object, especially because if you delete a word or insert a word in the sentence.

At the moment I am achieving some of this by just going through the entire text everytime a key is pressed and marking the invalid words and highlighting others. But somehow i don't feel this is a very good design and if it makes sense to extend the textpane to handle this inherently, and if yes how?


Have you considered using a DocumentListener? or maybe a DocumentFilter?
+Pie Number of slices to send: Send
 

Darryl Burke wrote:
Rob Camick's No Wrap Text Pane may help there.

In fact that's exactly where I got the idea to extend the TextPane.

Darryl Burke wrote:The Caret belongs to the UI delegate, not the component.

Does that mean I cannot disable the selection functionality if using textpane? I would imagine that it is possible.

Darryl Burke wrote:Not really. I'm not a programmer and have absolutely no education, formal or otherwise, in programming.

Could have fooled me :-)

Darryl Burke wrote:However, there are many skilled and experienced professionals here who would be in a position to advise you on design.

Hopefully, somebody will give their input.

Darryl Burke wrote:Sounds a lot like syntax highlighting <- Google


Darryl Burke wrote:Have you considered using a DocumentListener? or maybe a DocumentFilter?

Thanks for the tip. I'll look into both.
+Pie Number of slices to send: Send
 

Poorav Chaudhari wrote:

Darryl Burke wrote:The Caret belongs to the UI delegate, not the component.

Does that mean I cannot disable the selection functionality if using textpane? I would imagine that it is possible.


No, it doesn't mean that.
Yes, it's possible.

It means that to use a customized Caret, you have to associate that Caret with the UI delegate. Not directly with the text component.
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1874 times.
Similar Threads
control JTextPane
JTextPane
MySQL Workbench - how to get the text pane back?
File save and display in Swing
Word wrap and other issues with JTextPane
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:45:05.