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 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);
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);
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.
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);
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.
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.
- Tab panels – uitab and relatives This article describes several undocumented Matlab functions that support tab-panels...
- Customizing Matlab labels Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...
- Hyperlink text labels Hyperlink labels can easily be added to Matlab GUIs. ...
- Images in Matlab uicontrols & labels Images can be added to Matlab controls and labels in a variety of manners, documented and undocumented. ...
- Setting axes tick labels format Matlab plot axes ticks can be customized in a way that will automatically update whenever the tick values change. ...
- Uitable sorting Matlab's uitables can be sortable using simple undocumented features...