• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable to select text with number and underscore on DoubleClick in JTextPane

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a text like, "test_1_another_2_test3" in my JTextPane. When I double click on the text, it does not select the whole text (sometimes, it selects only one character or number or the text between underscores). I can implement a mouseactionlistener, but have no idea how to implement the action.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

So what happens? Do you select only text between successive _ characters? That may be an operating system feature, in which case you may not be able to change that behaviour. You can probably highlight a block of text with ctrl‑click or shift‑click or by dragging the mouse.
Not sure myself. Does the Java™ Tutorials section←(Link. Note it is in 3 parts and the left margin contains links to other text classes.) say anything about clicking text? Answer: not a lot. It does mention selecting text and getSelectedText methods. If you go through the JTextComponent documentation, you find there are various listeners you can add to one, but action listener doesn't seem to be among them.
Mouse listeners are maybe not a good idea because they are too low level. You can add an associated button and give that an action listener. You can use a key binding to associate an action with pushing a particular key, e.g. shift, ctrl‑shift, ctrll‑f10, or whatever you want. Some of the links in the Java™ Tutorials section will take you there.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you add the text to the JTextPane? This behavior is likely due to the StyledDocument you are using, or the AttributeSet it is using for that text.
If the content of your JTextPane is purely text, perhaps you should consider switching to a JTextArea instead.
reply
    Bookmark Topic Watch Topic
  • New Topic