• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Narayan Panda
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • 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.
 
Fred Kleinschmidt
Bartender
Posts: 453
3
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!