Download NetBeans!

20070718 Wednesday July 18, 2007

NetBeans Java Editor in JEditorPane

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.

Jul 18 2007, 03:43:31 PM PDT Permalink