Geertjan's Blog

  • March 7, 2006

Line Numbers in JEditorPane

Geertjan Wielenga
Product Manager
I've been working on adding line numbers to the MIDI Editor, which is implemented in a JEditorPane. I guess I could've saved myself a lot of trouble if I used the NetBeans editor instead, but I wanted to see if it was possible—i.e., how much of the NetBeans editor can I provide in a JEditorPane? Well, here's the result, thanks to this helpful document:

As you can see, I don't have syntax highlighting in the JEditorPane anymore (compare it to yesterday's blog entry to see the difference). I assumed that there was some relationship between StyledEditorKit and NbEditorKit that would allow me to have both line numbers and syntax highlighting, but that doesn't seem to be the case (so far). Weird things happened when I extended NbEditorKit instead of StyledEditorKit, because of the line numbering code. I haven't worked out why yet.

Another cool thing is how the linebreaks are extracted from the string that is played by the JFugue API:

String good = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#[]_";
String result = "";
for ( int i = 0; i < MidiEditor.getText().length(); i++ ) {
if ( good.indexOf(MidiEditor.getText().charAt(i)) >= 0 )
result += MidiEditor.getText().charAt(i);
Player player = new Player();
Pattern pattern = new Pattern(result);

Here, only the "good" characters (i.e., excluding, for example, "\\n") are included in the result, which is obtained by iterating through each character in the JEditorPane. Pretty useful trick.

Be the first to comment

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