How to implement an auto-complete for a text editor written in java?

泄露秘密 提交于 2019-12-06 09:14:03

问题


I'm trying to create a text editor like program for coding in mips assembly using java , the point i got mixed up alittle was the part i was trying to provide a Control-Space feature like that of eclipse's.For example when the user enters add $s1 , then presses ctrl+Space , i would replace this string:"add $s1 , $s2 , $s3" with what he has typed! ( that's an example .. i know add can be of immediate type instructions :D ), I'm using a hashmap to bind key strokes to what will actually happen in my JTextPane , sth like :

InputMap inputMap = textPane.getInputMap();
KeyStroke key = KeyStroke.getKeyStroke('some keys') ;
inputMap.put(key, 'some action') ;

well the problem is , when i want to implement Ctrl+Space for my program , i am using this :

key = KeyStroke.getKeyStroke(Event.CTRL_MASK , KeyEvent.VK_SPACE) ;
inputMap.put(key, DefaultEditorKit.insertContentAction);

but where can I select , what string should be inserted instead of the already typed part of the pattern ? other words , where does the insertContentAction bring it strings from ? how can i define what String should be replaced ? or even is there any other better ways for replacing , when Ctrl+Space has been pressed ? (what im asking for is a way to insert the string pattern into a jTextPane , not the algorithm to implement the pattern matching )
Thanks in advance :)


回答1:


You can get the Document of the text pane to make tightly controlled changes such as these. The Document also has some useful events of its own that you can listen for.



来源:https://stackoverflow.com/questions/9032969/how-to-implement-an-auto-complete-for-a-text-editor-written-in-java

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