A few weeks ago, a reader of my article about rich Matlab editbox contents 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 classes are also described.
SyntaxTextLabel
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)
StyledLabel
More flexibility in the displayed label styles can be achieved with HTML/CSS, and the bundled JIDE class com.jidesoft.swing.StyledLabel provides even more flexibility:
import java.awt.* import com.jidesoft.swing.* str = 'Mixed Underlined Strikethrough Super and Subscript combo Styles'; com.mathworks.mwswing.MJUtilities.initJIDE; 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 + ... StyleRange.STYLE_STRIKE_THROUGH)]; jStyledLabel.setStyleRanges(styles); [jhLabel,hContainer] = javacomponent(jStyledLabel,[10,10,300,20],gcf);
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, which enables easy multi-style text construction.
Finally, JIDE provides the ClickThroughStyledLabel, 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.
SyntaxTextPane
Multi-line syntax-highlighted code can be displayed with Matlab’s SyntaxTextPane class. SyntaxTextPane uses MIME 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' jCodePane.setContentType(codeType) 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' ... 'end']; str = sprintf(strrep(str,'%','%%')); jCodePane.setText(str) jScrollPane = com.mathworks.mwswing.MJScrollPane(jCodePane); [jhPanel,hContainer] = javacomponent(jScrollPane,[10,10,300,100],gcf);

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 and checkClass 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.
Summary
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.








