- Undocumented Matlab - http://undocumentedmatlab.com -

Syntax highlighted labels & panels

Posted By Yair Altman On July 14, 2010 | 18 Comments

A few weeks ago, a reader of my article about rich Matlab editbox contents [1] asked whether it is possible to display syntax-highlighted contents, i.e. contents whose color changes based on its underlying text, often called syntax hilite in affection. I gave a very specific answer in a reply comment, which I expand in today’s full-length article.

Matlab has two built-in Java classes that can present syntax-highlighted text: SyntaxTextLabel presents single-line labels, while SyntaxTextPane presents a multi-line editor pane. Both of these classes support C, HTML/XML, Java and Matlab syntax highlighting, as well as standard plaint-text. Some related JIDE [2] classes are also described.


SyntaxTextLabel is used to display a syntax-highlighted single-line text label according to the specified programming language: C_STYLE, HTML_STYLE, JAVA_STYLE, PLAIN_STYLE and of course M_STYLE for Matlab code:

str = 'for id=1:3, set(h(id),''string'',num2str(id)); end  % Matlab code';
codeType = com.mathworks.widgets.SyntaxTextLabel.M_STYLE;
jCodeLabel = com.mathworks.widgets.SyntaxTextLabel(str,codeType)
[jhLabel,hContainer] = javacomponent(jCodeLabel,[10,10,300,20],gcf);

SyntaxTextLabels (different code styles)
SyntaxTextLabels (different code styles)


More flexibility in the displayed label styles can be achieved with HTML/CSS [3], and the bundled JIDE class com.jidesoft.swing.StyledLabel [4] provides even more flexibility:

import java.awt.*
import com.jidesoft.swing.*
str = 'Mixed Underlined Strikethrough Super and Subscript combo Styles';
jStyledLabel = StyledLabel(str);
styles = [StyleRange(0,5,  Font.BOLD,   Color.BLUE), ...
          StyleRange(6,10, Font.PLAIN,StyleRange.STYLE_UNDERLINED),...
          StyleRange(17,13,Font.PLAIN,  Color.RED,   ...
                           StyleRange.STYLE_STRIKE_THROUGH), ...
          StyleRange(31,5, Font.PLAIN,  Color.BLUE,  ...
                           StyleRange.STYLE_SUPERSCRIPT), ...
          StyleRange(37,3, Font.ITALIC, Color.BLACK), ...
          StyleRange(41,9, Font.PLAIN,  Color.BLUE,  ...
                           StyleRange.STYLE_SUBSCRIPT), ...
          StyleRange(51,5, Font.PLAIN,  StyleRange.STYLE_WAVED + ...
[jhLabel,hContainer] = javacomponent(jStyledLabel,[10,10,300,20],gcf);

JIDE StyledLabel (different font styles)
JIDE StyledLabel (different font styles)

StyledLabels have subclasses that can be used to present styled text in tables, trees or lists. JIDE also provides the convenient StyledLabelBuilder [5], which enables easy multi-style text construction.
Finally, JIDE provides the ClickThroughStyledLabel [6], a StyledLabel extension that allows setting a target component, so that mouse clicks on the label will actually trigger the target component. This can be useful in forms where components have adjacent descriptive labels.


Multi-line syntax-highlighted code can be displayed with Matlab’s SyntaxTextPane class. SyntaxTextPane uses MIME [7] types rather than styles for syntax-highlighting, but the end-result appears similar:

jCodePane = com.mathworks.widgets.SyntaxTextPane;
codeType = jCodePane.M_MIME_TYPE;  % ='text/m-MATLAB'
str = ['% create a file for output\n' ...
       '!touch testFile.txt\n' ...
       'fid = fopen(''testFile.txt'', ''w'');\n' ...
       'for i=1:10\n' ...
       '    % Unterminated string:\n' ...
       '    fprintf(fid,''%6.2f \n, i);\n' ...
str = sprintf(strrep(str,'%','%%'));
jScrollPane = com.mathworks.mwswing.MJScrollPane(jCodePane);
[jhPanel,hContainer] = javacomponent(jScrollPane,[10,10,300,100],gcf);

SyntaxTextPane panel (Matlab MIME type)
SyntaxTextPane panel (Matlab MIME type)

The nice thing about SyntaxTextPane is that it syntax-highlights on-the-fly as you type or edit in the SyntaxTextPane (assuming you have not disabled editing with the setEditable(flag) method). This is exactly the behavior we have come to expect in the full-blown Matlab editor, and can now be embedded as a simple panel within our GUI.
Despite its misleadingly-simple look, SyntaxTextPane actually has most capabilities of the full-blown editor, not just syntax highlighting. This includes multiple undo/redo actions; smart indentation/commenting; automatic indication of corresponding block elements (if-end, for-end, etc. – also known as delimiter matching); search/replace, drag-and-drop and cut-copy-paste support; and many more.
Interested readers can use the uiinspect [8] and checkClass [9] utilities to explore the full capabilities offered by SyntaxTextPane. In this respect it would be helpful to also look at its super-class (SyntaxTextPaneBase) and the related SyntaxTextPaneUtilities class.


These Java classes are examples of built-in classes that can be used in Matlab applications, enabling a much richer GUI experience than possible using the standard (documented/supported) Matlab widgets.
As I have shown above, using these classes is extremely easy, and requires absolutely no Java knowledge. On the flip side, these internal Matlab classes may easily break in any future Matlab release, so be extra careful when deciding to use them. Future articles in this website will describe other similarly-useful built-in classes.
Have you found any other useful built-in Matlab class? If so, please post a comment [10].

Categories: GUI, High risk of breaking in future versions, Java

18 Comments (Open | Close)

18 Comments To "Syntax highlighted labels & panels"

#1 Comment By kmilo17pet On July 20, 2010 @ 23:30

I tried with this code:


to change font type,size and style, but it does not work.
Also, how display the “line numbers”?

#2 Comment By Yair Altman On July 21, 2010 @ 00:31

I’m not sure about the font, but here’s one way to display line numbers:

glyph = org.netbeans.editor.GlyphGutter(jCodePane.getEditorUI);
glyph.setSize(java.awt.Dimension(20, jCodePane.getHeight));

jPanel1 = javax.swing.JPanel;

jPanel2 = javax.swing.JPanel(jPanel1.getLayout);

jScrollPane = com.mathworks.mwswing.MJScrollPane(jPanel2);
[jhPanel,hContainer] = javacomponent(jScrollPane,[10,10,300,50],gcf);

Note that even in the Matlab Editor line numbers are displayed in a separate glyph panel, and are not part of the syntax pane.

#3 Comment By Jesper On January 21, 2014 @ 14:19

Hi Yair,

I’m trying to get the linenumbers in, but simply adding your code example to the syntaxpane example above, gives a very weird result, both in matlab 2012b and 2013b.

I must admit that I’m new to java in matlab, and i understand little of the code i am copy-pasting from your examples;
is there a way you should edit the position vector for it to work correctly, or is it simply broken in the newer matlab releases?

#4 Comment By Oscar On October 23, 2010 @ 07:41

Thank you very much for sharing this extremely useful undocumented Matlab feature. I use it in several GUIs. One minor thing I am running into is that if a large text is loaded in the pane it automatically generates a scroll bar (great!), but the focus of the pane is on the end of the text. Is there a way to force the focus towards the top of the text (automatically move the scroll-bar all the way up)?

#5 Comment By Yair Altman On October 23, 2010 @ 09:00

@Oscar – of course it can be done – [17]

Also read the related [18]


#6 Comment By Sune On November 27, 2011 @ 03:40

I’m having some troubles loading text into the CodePane directly from .m files. If I use a standard fopen fgets/fgetl approach to populate a string with the contents of a .m file, I miss all the string formatting and have to insert formatting myself. But this becomes troublesome when I try to correct for i.e. sprintf’s interpretation of apostrophes, backslash and so on. Does a more direct method exist for doing this job?

#7 Comment By Yair Altman On November 27, 2011 @ 05:08

@Sune – not that I know of. There’s a setFilename(string) method, but apparently it doesn’t do what you need (I’m not exactly sure what it does). I suggest reading the file as a simple text file, character-by-character, without any formatting, and let the code pane do all the formatting for you.

#8 Comment By Jesper On January 21, 2014 @ 06:22

Hi Yair,

First of all, love your work, love your book.

I am currently implementing the syntaxpane in a GUI, where i would like to validate the matlab code entered.
Do you know if its possible to extract information about syntax-errors from the jCodePane object?

Hope you can help,


#9 Comment By Yair Altman On January 21, 2014 @ 06:49

@Jesper – thanks 🙂

I’m not sure if it’s possible to extract the information directly, but you could always [19].

#10 Comment By Jesper On January 21, 2014 @ 10:07

Hi Yair,

That could indeed solve my issue. Thank you for the swift response !

#11 Pingback By Animated busy (spinning) icon | Undocumented Matlab On April 18, 2014 @ 08:29

[…] how to use other internal components in Matlab’s com.mathworks.widgets package, including syntax-highlighted labels and text panes that can be embedded in our Matlab GUI.Have you used BusyAffordance or some other internal Matlab […]

#12 Comment By Adam On September 13, 2014 @ 19:00

Hi Yair,

Thank you for this great article and the accompanying chapter in your book. I’ve enjoyed both thoroughly. The SyntaxTextPane seems perfect for an application I’m looking to write (a simple XML editor in MATLAB), but the only acceptable MIME-type as far I can tell is the MATLAB one. Passing in the XML MIME type does not seem to have any effect and passing in the HTML MIME type throws a Class Cast Exception (or something similar). I am on MATLAB R2013a.

Are you aware of any syntax highlighting functionality for languages other than MATLAB within SyntaxTextPane? If so, how can I activate that functionality? If not, is there a way for me to write my own syntax highlighting rules and pass them into an instance of SyntaxTextPane? Or, I noticed that the MATLAB Editor has rules for XML/HTML syntax highlighting… can I make use of that some how?

Thank you for your great work!

#13 Comment By Yair Altman On September 15, 2014 @ 14:42

@Adam –


The rest is as described in the main article above.

#14 Comment By Björn On January 21, 2015 @ 00:39

Hi Yair,
This is a great blog! I wouldn’t be able to do much java programming without it.

The SyntaxTextPane and the possibility to add line numbers as you showed in a comment is great. Is it also possible to include the code folding capability of the Matlab editor in this textpane? I haven’t been able to find any way of including a com.mathworks.widgets.text.fold.MWCodeFoldingSideBar which I think is the way to do it.
Your help is highly appreciated.


#15 Comment By John Smith On July 7, 2015 @ 18:52

Hi, Yair,

Thank you very much for the info on your blog and book. They are very useful.

I came to this “syntax hilite” blog because I was searching how to delete all comments in an M-file. Since Matlab recognizes comments, and it doesn’t get confused by, for example, % comment text…, fprintf(‘%10.3f’), or sprintf(‘%% comment’), I want to utilize it and not reinvent the wheel like some existing file on the FileExchange.

But the question is: where can I find that method and in which class? What is the syntax for that method?

If you already have posts regarding to this topic, could you show me that article?

Thank you very much!

#16 Comment By Yair Altman On July 8, 2015 @ 00:55

@John – to the best of my knowledge there is no direct method to do this. You are welcome to try to poke around in the Java classes mentioned in this article, but I don’t think it will get you what you want.

I’ve created a utility that does this (stripping comments from m-files) for my personal use and it works splendidly. I’m willing to sell you a copy if you wish – contact me by email (altmany at gmail) if you are interested.

#17 Comment By Shalin On January 7, 2016 @ 10:54


Thanks for so much work. So, I was wondering how can I check the documentation of all the JAVA classes I can use? For example, the method setContentType sets the syntax highlight. I would want to see the list of all the available constant values such as MIME.

#18 Comment By Yair Altman On January 7, 2016 @ 10:59

@Shalin – the easiest way would be to simply Google it with the word “java” and the relevant keyword. For example, in your case: Google “java setContentType”. There’s plenty of online documentation on Java, way more than on Matlab.

Article printed from Undocumented Matlab: http://undocumentedmatlab.com

URL to article: http://undocumentedmatlab.com/articles/syntax-highlighted-labels-panels

URLs in this post:

[1] rich Matlab editbox contents: http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/#comment-10760

[2] JIDE: http://undocumentedmatlab.com/blog/tag/JIDE/

[3] HTML/CSS: http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/

[4] com.jidesoft.swing.StyledLabel: http://www.jidesoft.com/javadoc/com/jidesoft/swing/StyledLabel.html

[5] StyledLabelBuilder: http://www.jidesoft.com/javadoc/com/jidesoft/swing/StyledLabelBuilder.html

[6] ClickThroughStyledLabel: http://www.jidesoft.com/javadoc/com/jidesoft/swing/ClickThroughStyledLabel.html

[7] MIME: http://en.wikipedia.org/wiki/MIME

[8] uiinspect: http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methodspropertiescallbacks-of-an-object

[9] checkClass: http://www.mathworks.com/matlabcentral/fileexchange/26947-checkclass

[10] post a comment: #respond

[11] Scrollable GUI panels : http://undocumentedmatlab.com/articles/scrollable-gui-panels

[12] Tab panels – uitab and relatives : http://undocumentedmatlab.com/articles/tab-panels-uitab-and-relatives

[13] Transparent labels : http://undocumentedmatlab.com/articles/transparent-labels

[14] Customizing Matlab labels : http://undocumentedmatlab.com/articles/customizing-matlab-labels

[15] Toolbar button labels : http://undocumentedmatlab.com/articles/toolbar-button-labels

[16] Hyperlink text labels : http://undocumentedmatlab.com/articles/hyperlink-text-labels

[17] : http://undocumentedmatlab.com/blog/setting-line-position-in-edit-box-uicontrol/

[18] : http://undocumentedmatlab.com/blog/customizing-listbox-editbox-scrollbars/

[19] : http://undocumentedmatlab.com/blog/parsing-mlint-code-analyzer-output/

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.