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

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

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");

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);
    // 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) {

// 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(
at org.netbeans.api.lexer.TokenSequence.offset(
at org.netbeans.modules.editor.indent.TaskHandler$MimeItem.runTask(
at org.netbeans.modules.editor.indent.TaskHandler.runTasks(
at org.netbeans.modules.editor.indent.IndentImpl.reformat(
at org.netbeans.modules.editor.indent.FormatterImpl.reformat(
at org.netbeans.editor.GuardedDocument.runAtomicAsUser(
at org.netbeans.lib.editor.codetemplates.CodeTemplateInsertHandler.insertTemplate(
at org.netbeans.lib.editor.codetemplates.CodeTemplateInsertHandler.processTemplate(
at org.netbeans.lib.editor.codetemplates.CodeTemplateManagerOperation.insert(
at org.netbeans.lib.editor.codetemplates.api.CodeTemplate.insert(
at org.netbeans.lib.editor.codetemplates.AbbrevDetection.expand(
at org.netbeans.lib.editor.codetemplates.AbbrevDetection.expand(
at org.netbeans.lib.editor.codetemplates.AbbrevDetection.checkExpansionKeystroke(
at org.netbeans.lib.editor.codetemplates.AbbrevDetection.keyPressed(
at java.awt.AWTEventMulticaster.keyPressed(
at java.awt.Component.processKeyEvent(
at javax.swing.JComponent.processKeyEvent(
at java.awt.Component.processEvent(
at java.awt.Container.processEvent(
at java.awt.Component.dispatchEventImpl(
at java.awt.Container.dispatchEventImpl(
at java.awt.Component.dispatchEvent(
at java.awt.KeyboardFocusManager.redispatchEvent(
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(
at java.awt.Component.dispatchEventImpl(
at java.awt.Container.dispatchEventImpl(
at java.awt.Window.dispatchEventImpl(
at java.awt.Component.dispatchEvent(
at java.awt.EventQueue.dispatchEvent(
at org.netbeans.core.TimableEventQueue.dispatchEvent(
[catch] at java.awt.EventDispatchThread.pumpOneEventForFilters(
at java.awt.EventDispatchThread.pumpEventsForFilter(
at java.awt.EventDispatchThread.pumpEventsForHierarchy(
at java.awt.EventDispatchThread.pumpEvents(
at java.awt.EventDispatchThread.pumpEvents(
Back to top

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 :
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 cannot 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