Get XY position from caret position in JTextArea

为君一笑 提交于 2019-12-22 10:44:17

问题


I'm trying to make an editor which have ability show pop up (JPopupMenu) each time users type any letter. Maybe, to imagine how my editor works its like any compiler, such as NetBeans when we type dot, the function appear.

I have learned JPopupMenu from here. JPopupMenu needs XY position. My idea is to get XY position from JTextArea's caret position. My question is: Is there possible to get XY position from caret position (text cursor) in JTextArea or other JTextComponent? Or maybe is there any solution better to this case?

Thx before. Sorry, not good in english. :D


回答1:


Rectangle rectangle = textArea.modelToView( textArea.getCaretPostion() );

So you would probably use x and y + height from the Rectangle to position the popup.



来源:https://stackoverflow.com/questions/18864368/get-xy-position-from-caret-position-in-jtextarea

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