Over a million developers have joined DZone.

How to "Create" an Editor for Java Applications

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Rather than creating your own editor whenever you're working on a Java application that needs one, why not use the JSyntaxPane? By means of one single JAR file, your application has access to all of the following editor kits:

How to use it? Nothing simpler than... (1) create a JFrame, (2) add a JEditorPane, and then...


Note: That's all you need to do. No need to declare "jsyntaxpane" and then initialize it. Simply use it, exactly as above, i.e., use its "initKit" call and then you're done. That one line of code is literally all that's needed, after which you can set the MIME type of your choice. For "text/java", you'll get all of this when you run your JFrame:

From the first screenshot above, you can see which editor kits are available. Then look in the 'jsyntaxpane.kitsfortypes.properties' file, within the META-INF/services folder, and you will see the mappings between MIME types and editor kits:


The editors have a lot of predefined functionality. For example, press Ctrl-F (Find) or Ctrl-H (Replace) and you will have a Find/Replace dialog, which can even highlight the specified word in the editor:

Another very cool predefined feature is code completion. Press Ctrl-Space and your abbreviations will expand to full code snippets. These are defined in 'jsyntaxpane.javasyntaxkit.completion.properties', in the case of Java, as follows:

pu=public |
pr=private |
st=static |
cl=class |
St=String |
fri=for(int i=0; i<10; i++) {\n|
psvm=public static void main(String[] args) {\n|

Note: The pipe symbol indicates where the cursor will find itself when the abbreviation has been expanded.

Similarly, there is such a file for Groovy as well, in 'jsyntaxpane.groovysyntaxkit.completion.properties', in the META-INF/services folder.

And what determines the colors and styles of the code for the editor kits? Yet another properties file, this one called 'jsyntaxpane.syntaxstyles.properties'. Here's the default content:

OPERATOR = 0x000000, 0
KEYWORD = 0x3333ee, 0
KEYWORD2 = 0x3333ee, 3
TYPE = 0x000000, 1
TYPE2 = 0x000000, 3
STRING = 0xcc6600, 0
STRING2 = 0xcc6600, 1
NUMBER = 0x999933, 1
REGEX = 0xcc6600, 0
IDENTIFIER = 0x000000, 0
COMMENT = 0x339933, 2
COMMENT2 = 0x339933, 3
DEFAULT = 0x000000, 0
WARNING = 0xCC0000, 0
ERROR = 0xCC0000, 3

What does it all mean? The keys are the token types, used throughout the editor kits. The values are hexes for the colors, followed by 1, 2, or 3, for "bold", "italic", or "bold & italic". Simple and effective.

At some point you might want to extend/customize the features provided by the JSyntaxPane. There's no way to do so from your own code, as far as I am aware, (which would have been nice): you need to tweak the sources of the JAR. Check out the sources and you'll find you have a Maven POM, among all the other source files. Open the POM in the IDE of your choice and then you can build the sources and browse through them, noticing the JFlex sources (more about these here), which are turned into Java sources in the "target" folder (which is also where the binary JAR will be found):

You can even run the Maven project. You'll find that a tester application is included in the sources, which will be deployed when you run the Maven project, giving you a Swing application where you can try out the different editors, just to see whether they provide the functionality you need:

In short, this is a very powerful JAR that you can simply drop into your application. Read the blog of JSyntaxPane's creator and maintainer, Ayman Al-Sairafi, here. It's a very interesting blog, if you want to keep up with the latest developments of this project.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}