4

i have questions how to make a simple text editor used for editing Java Language? Could anyone give some tips, steps, references, or something else.. I just want to make a text editor only for java language that has functions like syntax coloring, and an auto generate functions for try catch, and if else..

This is for my lesson in Java Programming..

thx for any helps.. :D

tenorsax
  • 21,123
  • 9
  • 60
  • 107
ordinaryBoys
  • 51
  • 1
  • 3
  • Take a look at [jEdit](http://www.jedit.org/), it's an open source text editor written in Java so you can have a look at the source and see what they do. – joshuahealy Apr 11 '12 at 03:32
  • I already take a look at JEdit.. But perhaps its to complicated for me.. Is there any simple example? First step i just want to build syntax coloring. – ordinaryBoys Apr 11 '12 at 03:42
  • Writing a syntax-aware editor involves a large amount of work. If you have to write everything from scratch it is, for one person, much more than one semester's worth of work. Especially if you are not already an expert Java developer. Is this something assigned to you or did you choose this yourself? How long do you have? – Jim Garrison Apr 11 '12 at 03:50
  • I think so, because it is quite complex for me if i make a "trully" java editor.. I choose this because i think that syntax coloring is not too complicated, so syntax coloring is my purpose in develop this program. I have maybe 3-4 months.. – ordinaryBoys Apr 11 '12 at 03:56
  • I think you'd need to have a look at [abstract syntax trees](http://en.wikipedia.org/wiki/Abstract_syntax_tree) and [parse trees](http://en.wikipedia.org/wiki/Parse_tree) and generate such a representation from the input src code using the rules defined in the [java language specification](http://docs.oracle.com/javase/specs/). Why is this necessary? Because you cannot simply search for "int" in the src code and colour that as a primitive data type. What if the int is within a comment? Constructing these representations from scratch seems like an incredibly complex task though. – Alderath Apr 11 '12 at 09:43

2 Answers2

2

Check out JSyntaxPane.

It's an open source project which sounds like it does exactly what you're after. Looking at how someone else accomplished a task like this will probably give you lots of ideas.

Here's a screenshot of what it looks like (the panel on the right):

enter image description here

ulmangt
  • 5,343
  • 3
  • 23
  • 36
  • +1 Nice library. In the past, I use Netbean's editor plugin library. – ecle Apr 11 '12 at 05:07
  • @ulmangt Thx for the link.. I already take a look at the classes.. And i think that its quite complex to do.. I just want to make only syntax highlighting.. From that code, It was very professional programmer i think.. – ordinaryBoys Apr 11 '12 at 07:35
1

Check out How to Use Editor Panes and Text Panes.

Also, here are some examples - Highlighting Words in a JTextComponent and Inserting Styled Text in a JTextPane Component.

And here is one with lexer - Text Editor Tutorial

tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • Thx, but i should build my own class.. I have some references like java lexer, token. I think that it will not be used when i use JEditorPanes.. is that right? – ordinaryBoys Apr 11 '12 at 03:41
  • @ordinaryBoys, lexer gives you tokens. you can use token properties such as type of text and its position to apply corresponding styles in text pane. – tenorsax Apr 11 '12 at 03:54
  • nah, Max.. U got what i want.. I already download the syntaxdemo, and see what is inside the classes.. Uhmm, Did u already see that program? is that simple to build? – ordinaryBoys Apr 11 '12 at 04:01
  • I have to learn for Lexer and Tokens.. i think that they are the keys in syntax coloring.. Is that right? Do u have links or references for lexer and tokens? – ordinaryBoys Apr 11 '12 at 04:02
  • @ordinaryBoys , You may find this [answer](http://stackoverflow.com/q/611820/1048330) helpful. – tenorsax Apr 11 '12 at 04:07
  • I think for my assignment i have to choose option 3 from ur link u gave "Do it yourself (I guess I could write a simple token parser and highlight the source code).".. T_T so i have to understand lexer and tokens first.. but thank Max, for helping.. If u have any references about Lexer and Tokens, I would be pleased to know.. :D – ordinaryBoys Apr 11 '12 at 04:14