NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Java Syntax Highlighting and Code Completion in JEditorPane

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Platform Users
View previous topic :: View next topic  
Author Message
chaddaniels



Joined: 16 Oct 2008
Posts: 37

PostPosted: Wed Dec 31, 2008 8:44 am    Post subject: Java Syntax Highlighting and Code Completion in JEditorPane Reply with quote

Does anyone know if the sample explained at http://wiki.netbeans.org/DevFaqEditorCodeCompletionAnyJEditorPane is still current as of netbeans 6.5? I tried following the sample which does give correct syntax highlighting, but any automatic code completion fails.

This is the code I am using:
Code:

JEditorPane editorPane = new JEditorPane();

// This will find the Java editor kit and associate it with
// our editor pane. But that does not give us code completion
// just yet because we have no Java context (i.e. no class path, etc.).
// However, this does give us syntax coloring.
EditorKit kit = CloneableEditorSupport.getEditorKit("text/x-java");
editorPane.setEditorKit(kit);

try {
    // You can specify any ".java" file.
    // The contents of the file does not matter.
    // The extension must be ".java", however.
    File tmpFile = File.createTempFile("temp", ".java");
    FileObject fob = FileUtil.toFileObject(FileUtil.normalizeFile(tmpFile));
    DataObject dob = DataObject.find(fob);
    editorPane.getDocument().putProperty(
        Document.StreamDescriptionProperty,
        dob);
    // This sets up a default class path for us so that
    // we can find all the JDK classes via code completion.
    DialogBinding.bindComponentToFile(fob, 0, 0, editorPane);
} catch (IOException ex) {
    Exceptions.printStackTrace(ex);
}

// Last but not least, we need to fill the editor pane with
// some initial dummy code - as it seems somehow required to
// kick-start code completion.
// A simple dummy package declaration will do.
editorPane.setText("package dummy;");


I get the following exception whenever I try to auto-complete something:

java.lang.IllegalStateException: Caller of TokenSequence forgot to call moveNext/Previous() or it returned false (no more tokens)
TokenSequence for text/x-java at tokenIndex=0. TokenList contains 0 tokens:


at org.netbeans.api.lexer.TokenSequence.checkTokenNotNull(TokenSequence.java:736)
at org.netbeans.api.lexer.TokenSequence.offset(TokenSequence.java:251)
at org.netbeans.modules.java.source.save.Reformatter$Pretty.<init>(Reformatter.java:380)
at org.netbeans.modules.java.source.save.Reformatter$Pretty.<init>(Reformatter.java:348)
at org.netbeans.modules.java.source.save.Reformatter$Pretty.reformat(Reformatter.java:393)
at org.netbeans.modules.java.source.save.Reformatter.reformatImpl(Reformatter.java:169)
at org.netbeans.modules.java.source.save.Reformatter.reformat(Reformatter.java:94)
at org.netbeans.modules.editor.indent.TaskHandler$MimeItem.runTask(TaskHandler.java:504)
at org.netbeans.modules.editor.indent.TaskHandler.runTasks(TaskHandler.java:271)
at org.netbeans.modules.editor.indent.IndentImpl.reformat(IndentImpl.java:293)
at org.netbeans.modules.editor.indent.FormatterImpl.reformat(FormatterImpl.java:187)
at org.netbeans.lib.editor.codetemplates.CodeTemplateInsertHandler.run(CodeTemplateInsertHandler.java:342)
at org.netbeans.editor.GuardedDocument.runAtomicAsUser(GuardedDocument.java:324)
at org.netbeans.lib.editor.codetemplates.CodeTemplateInsertHandler.insertTemplate(CodeTemplateInsertHandler.java:279)
at org.netbeans.lib.editor.codetemplates.CodeTemplateInsertHandler.processTemplate(CodeTemplateInsertHandler.java:236)
at org.netbeans.lib.editor.codetemplates.CodeTemplateManagerOperation.insert(CodeTemplateManagerOperation.java:238)
at org.netbeans.lib.editor.codetemplates.api.CodeTemplate.insert(CodeTemplate.java:104)
at org.netbeans.lib.editor.codetemplates.AbbrevDetection.expand(AbbrevDetection.java:506)
at org.netbeans.lib.editor.codetemplates.AbbrevDetection.expand(AbbrevDetection.java:415)
at org.netbeans.lib.editor.codetemplates.AbbrevDetection.checkExpansionKeystroke(AbbrevDetection.java:319)
at org.netbeans.lib.editor.codetemplates.AbbrevDetection.keyPressed(AbbrevDetection.java:279)
at java.awt.AWTEventMulticaster.keyPressed(AWTEventMulticaster.java:233)
at java.awt.Component.processKeyEvent(Component.java:6092)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2799)
at java.awt.Component.processEvent(Component.java:5911)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:704)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:969)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:841)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:668)
at java.awt.Component.dispatchEventImpl(Component.java:4373)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Window.dispatchEventImpl(Window.java:2458)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:104)
[catch] at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
[/url]
Code:
Code:
Back to top
NicolasDumoulin



Joined: 27 Jan 2009
Posts: 21

PostPosted: Tue Apr 14, 2009 1:13 pm    Post subject: Reply with quote

I'm also interested by finding an answer Smile
I've obtained a similar result.

I've followed the tuto from Geertjan : http://blogs.sun.com/geertjan/entry/netbeans_java_editor_in_jeditorpane
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Platform Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo