X

Geertjan's Blog

  • July 18, 2007

NetBeans Java Editor in JEditorPane

Geertjan Wielenga
Product Manager
Finally, thanks to Josh Sandusky, I managed to get code completion in a JEditorPane, by reusing the NetBeans Java Editor. Here's a TopComponent that contains the NetBeans Java Editor in a JEditorPane:

The syntax coloring is very basic, but that's the syntax coloring in my current version of NetBeans IDE 6.0. The code that you see above is the constructor of the TopComponent, which associates the NetBeans Java Editor with the JEditorPane.

One needs to create a Java file somewhere, as shown above. Would be better, and is possible I think, to create the file in memory. I've also noticed that the code completion doesn't work in some cases, such as sometimes when the JEditorPane is empty. So one could use the setText() method on the JEditorPane to add some text programmatically.

And now I can use code completion, as well as some of the other features of the NetBeans Java Editor. Here's what I see when I press Ctrl-Space, i.e., code completion, in the Exception statement below:

Would be nice if this were also possible in other Swing components, but without being able to set a MIME type, that's not possible, it seems. So, for example, a JTextField could not be used in this way.

Join the discussion

Comments ( 5 )
  • Daniel Thursday, July 19, 2007
    Is there any step-by-step tutorial for doing this? For absolute Netbeans beginners? Would be glad to have one, since I'm exactly looking for a tutorial which explains how to build/enhance your own editor component.
  • Geertjan Thursday, July 19, 2007
    Here:
    1. In New Project wizard, choose Module Project template. This creates source structure of your module.
    2. Next, use New Window Component Wizard. This creates new window.
    3. Right-click the project, choose Properties, go to the Libraries panel, click Add next to the 'Module Dependencies' list, and make sure that the following are in the list: Datasystems API, Editor, File System API, Java Source, Java Source UI, Nodes API, Swing Layout Extension integration, Text API, UI Utilities API, Utilities API, Window System API.
    4. Add a JEditorPane to the TopComponent that you created in the 2nd step above, name it 'jep'.
    5. In the TopComponent, add the code shown in the first screenshot in this blog entry, to the constructor.
    6. Install the module. (Right-click the project and choose install.)
  • Daniel Sunday, July 22, 2007
    Another small question: With these simple steps I could also create a html-editor component just by replacing the "text/x-java" in the function call with "text/html" (or whatever)?
    And: Is it even possible to create a component (or "modify" a JEditorPane), which highlights HTML, CSS and PHP at once?
  • Popov Andrey Tuesday, October 28, 2008

    I need add xml/xsd support for my JEditorPane component.

    Therefore i wrote simple TopComponent code:

    private javax.swing.JEditorPane jep;

    private void initPane() {

    EditorKit kit = CloneableEditorSupport.getEditorKit("text/xml");

    jep.setEditorKit(kit);

    jep.setText(getText());

    }

    All needed schemas included in DTD's and Schema's Catalog.

    But there are problem if getText() contains xsd-code (for example:

    <tag xmlns:xsi=...

    xsi:schemaLocation=...

    xmlns=...>

    if i use Ctrl+Space then have exception:

    java.lang.AssertionError: Null file object.

    at org.netbeans.modules.xml.retriever.catalog.Utilities.createModelSource(Utilities.java:622)

    at org.netbeans.modules.xml.retriever.catalog.Utilities.getModelSource(Utilities.java:609)

    at org.netbeans.modules.xml.schema.completion.util.DefaultModelProvider.getCompletionModel(DefaultModelProvider.java:102)

    at org.netbeans.modules.xml.schema.completion.util.DefaultModelProvider.getModels(DefaultModelProvider.java:87)

    at org.netbeans.modules.xml.schema.completion.util.CompletionContextImpl.initModels(CompletionContextImpl.java:488)

    at org.netbeans.modules.xml.schema.completion.CompletionQuery.getCompletionItems(CompletionQuery.java:102)

    at org.netbeans.modules.xml.schema.completion.CompletionQuery.query(CompletionQuery.java:85)

    at org.netbeans.spi.editor.completion.support.AsyncCompletionTask.run(AsyncCompletionTask.java:218)

    at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:561)

    [catch] at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:986)

    )

    if i use standard netbeans xml editor then my schemas assists show up by Ctrl+Space

    Can you help me?


  • kgopolo Thursday, September 29, 2011

    Hi

    I want to view an html page in my swing application that i used drag and drop in netbeans. im jst a student...please help


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.