clickable text from jTextPane

筅森魡賤 提交于 2019-12-13 21:25:59

问题


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??


回答1:


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.




回答2:


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();
    }
}    



回答3:


//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();
    }
} 


来源:https://stackoverflow.com/questions/30898039/clickable-text-from-jtextpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!