clickable text from jTextPane

1.2k views Asked by At

I am implementing a function which sets text to jTextPane. So when user clicks certain word in jTextPane, the definition of that word should be displayed in jTextArea. I know how to display text in jTextPane and in jTextArea. What's troubling me is that when I click in jTextPane the whole text is getting selected instead of selecting that particular word :'( . I have done some research on caret positions and all but I can't quite get it. This is as far as I got:

private void jTextPane1MouseClicked(java.awt.event.MouseEvent evt) {                                        
        try
        {
           StyledDocument doc=(StyledDocument) jTextPane1.getDocument();;
            Element ele = doc.getCharacterElement(jTextPane1.viewToModel(evt.getPoint()));
            AttributeSet as = ele.getAttributes();
           /* Here after getting the word from jTextPane we print the definition 
             of that word in jTextArea... I got the code for this part */ 

        }
}

How can I get only the clicked word??enter image description here

3

There are 3 answers

1
StanislavL On BEST ANSWER

You can use viewToModel() method (in fact you already use it) to detect charatcer position for the clicked point.

Then use javax.swing.text.Utilities class. It has methods:

public static final int getWordStart(JTextComponent c, int offs)
public static final int getWordEnd(JTextComponent c, int offs)

Just pass the position obtained from viewToModel() call and get start and end positions for the clicked word. Then you can select it by setSelectionStart()/setSelectionEnd() calls passing the word start/end offsets.

0
Gogo On

People who got the same problem... Here's how I solved it. First, add mouseClicked event to your jTextPane (this can be done in design tab in netbeans). Write the code to get the clicked text from the jTextPane. Here's the code:

   private void jTextPane1MouseClicked(java.awt.event.MouseEvent evt) {                                        
   try
    {
            String wrd=null;
            int pt=jTextPane1.viewToModel(evt.getPoint());
            int spt=Utilities.getWordStart(jTextPane1,pt);
            int ept=Utilities.getWordEnd(jTextPane1,pt);
            jTextPane1.setSelectionStart(spt);
            jTextPane1.setSelectionEnd(ept);
            wrd=jTextPane1.getSelectedText();
            System.out.println("TextPane word="+wrd);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}    
0
Norman Pilusa On
//You can get text without highlighting it like:

private void jTextPaneMouseClicked(java.awt.event.MouseEvent evt) {                                        
try
    {

            String word = null;
            int point = jTextPane.viewToModel(evt.getPoint());
            int startPoint = Utilities.getWordStart(jTextPane,point);
            int endPoint = Utilities.getWordEnd(jTextPane,point);

            word = jTextPane.getText(startPoint, endPoint-startPoint);
            System.out.println("Clicked word"+word);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}