DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. How to "Create" an Editor for Java Applications

How to "Create" an Editor for Java Applications

Geertjan Wielenga user avatar by
Geertjan Wielenga
·
Nov. 13, 08 · News
Like (0)
Save
Tweet
Share
37.75K Views

Join the DZone community and get the full member experience.

Join For Free
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...

jsyntaxpane.DefaultSyntaxKit.initKit();
jEditorPane.setContentType("text/java");

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:

text/c=jsyntaxpane.syntaxkits.CSyntaxKit
text/cpp=jsyntaxpane.syntaxkits.CppSyntaxKit
text/java=jsyntaxpane.syntaxkits.JavaSyntaxKit
text/groovy=jsyntaxpane.syntaxkits.GroovySyntaxKit
text/javascript=jsyntaxpane.syntaxkits.JavaScriptSyntaxKit
text/xml=jsyntaxpane.syntaxkits.XmlSyntaxKit
text/sql=jsyntaxpane.syntaxkits.SqlSyntaxKit
text/properties=jsyntaxpane.syntaxkits.PropertiesSyntaxKit
text/python=jsyntaxpane.syntaxkits.PythonSyntaxKit
text/tal=jsyntaxpane.syntaxkits.TALSyntaxKit
text/jflex=jsyntaxpane.syntaxkits.JFlexSyntaxKit
text/ruby=jsyntaxpane.syntaxkits.RubySyntaxKit

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|
sout=System.out.println(|)
serr=System.err.println(|)
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.

application Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 9 Ways You Can Improve Security Posture
  • Top Five Tools for AI-based Test Automation
  • Learning by Doing: An HTTP API With Rust
  • The New DevOps Performance Clusters

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: