How to create an autocomplete pop-up window in jtextpane in Java?

I am creating an SQL editor I am using jtextpane as the editor I want to implement autocompletion for table names such as eclipse

Solution

I think the appropriate class to display information on another component is JPopupMenu, which has handled layering correctly to display itself JPopupMenu has a show () method, which takes the 'parent' component as a parameter, which will be displayed in the coordinate space of the component The menu seems appropriate because you want to display a choice of terms for the user to choose

To check for text changes, you need to add the documentlistener to the document wrapped by jtextpane; You can access it using getdocument ()

To find the position of the cursor (actually the caret), you can use getcaretposition() This returns the position of the caret in the text stream as int You can use modeltoview() to convert this position to actual (x, y) coordinates This in turn will tell you where to display your menu

You can use addkeylistener () to capture keyboard events on jtextpane, such as pressing Ctrl - space

The combination of all this should allow you to do what you want to do

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>