NetBeans Groovy Editor in a JEditorPane

Even when you're not creating some kind of IDE, you might still have technical end users in mind. For this kind of user, it might be handy if you'd integrate a small scripting editor into your application, as I've done here for Groovy:

The editor area above is a JEditorPane. In the constructor of the TopComponent is this code:

EditorKit kit = CloneableEditorSupport.getEditorKit("text/x-groovy");
FileObject fob;
try {
    //Create a file in memory:
    fob = FileUtil.createMemoryFileSystem().getRoot().createData("tmp", "groovy");
    DataObject dob = DataObject.find(fob);
    DialogBinding.bindComponentToFile(fob, 0, 0, editorPane);
    //Provide some default content:
    editorPane.setText("def scores = [80, 90, 70]\\n\\n\\"\\"\\"Maximum: ${scores.max()}\\n"
            + "Minimum: ${scores.min()}\\"\\"\\"");
} catch (IOException ex) {

When the "Evaluate" button is clicked, the following happens:

private void evaluateButtonActionPerformed(java.awt.event.ActionEvent evt) {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("groovy");
    try {
        InputOutput inout = IOProvider.getDefault().getIO("Result", true);;
    } catch (ScriptException ex) {

Right now, though syntax coloring works, as well as keyboard shortcuts (such as Ctrl-E to delete a line), the code completion does not work yet. The code comes from here, but somehow ClassPathProvider needs to be hooked into the code so that the source files on the Groovy classpath are found in order to build up the code completion box. However, if I embed a Java editor (i.e., instead of creating "tmp.groovy", I create ""), there's no problem with code completion at all. I believe the DialogBinding statement may be the cause of the problem.


Nice tip, but I have a silly question: what's the difference of using NB's editor from IDE cluster?

Posted by Eduardo Costa on July 05, 2010 at 10:45 PM PDT #

Hi Geertjan,

Thanks for this post, it is very helpfull for me.
Actually I really need to get a Groovy editor in my project and this example sounds like exactly what I need; so that I tried to reproduce your code and I get a very basic error when loading the Document class from the JDK 1.6.
I really have no idea of what is wrong:

INFO []: [PersistenceManager.getTopComponentForID] Problem when deserializing TopComponent for tcID:'GroovyEditorTopComponent'. Reason: Error reading MultiFileObject@5b187658[Windows2Local/Components/GroovyEditorTopComponent.settings]: java.lang.reflect.InvocationTargetException
java.lang.ClassNotFoundException: javax.swing.text.Document
at org.apache.felix.framework.ModuleImpl.findClassOrResourceByDelegation(
at org.apache.felix.framework.ModuleImpl.access$100(
at org.apache.felix.framework.ModuleImpl$ModuleClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
Caused: java.lang.NoClassDefFoundError: javax/swing/text/Document
at org.openmole.editor.GroovyEditorTopComponent.<init>(
at org.openmole.editor.GroovyEditorTopComponent.getDefault(
Caused: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at org.netbeans.modules.settings.convertors.XMLSettingsSupport$SettingsRecognizer.createFromMethod(
Caused: Error reading MultiFileObject@5b187658[Windows2Local/Components/GroovyEditorTopComponent.settings]
at org.netbeans.modules.settings.convertors.XMLSettingsSupport$SettingsRecognizer.createFromMethod(
at org.netbeans.modules.settings.convertors.XMLSettingsSupport$SettingsRecognizer.instanceCreate(
at org.netbeans.modules.settings.convertors.SerialDataConvertor$SettingsInstance.instanceCreate(
at org.netbeans.core.GuiRunLevel$
at java.awt.event.InvocationEvent.dispatch(
at java.awt.EventQueue.dispatchEvent(
at org.netbeans.core.TimableEventQueue.dispatchEvent(
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(
[catch] at

Thanks for your help!

Posted by Mathieu on July 27, 2010 at 07:13 PM PDT #

Hello !

I've got a problem with this :

DialogBinding.bindComponentToFile(fob, 0, 0, editorPane);

I've try to put :

But the application doesn't find the package, I don't know wich dependencies I have to add.

It woul'd be a pleasure if you could help me.


Posted by Lo on March 08, 2011 at 04:48 PM PST #

I have the same problem as the above user, that package seems to have disappeared.

Posted by philip on June 30, 2011 at 03:31 AM PDT #

Tried it a few seconds ago in 7.0. Make sure the Groovy & Grails plugin is installed. Uploading the completed sample to the versions/7.0/misc folder here now:

Posted by Geertjan on June 30, 2011 at 07:40 AM PDT #

hi Geertjan,
thank you for your great blog. Your posts helps me much to code a prototyp for my master thesis.
I have checked out the sources from and always get a nullpointer with same stacktrace mentioned here
I have already 7.01... the bug seems not to be solved. Maybe some readers can give me a piece of advice.

Posted by projo on August 12, 2011 at 01:50 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« May 2016