Three years ago, I wrote an article about modifying Matlab’s look-and-feel (L&F, or LnF), using Java’s built-in support for replaceable LnFs. To date, that article has 27 comments by 13 different commenters (plus 20 responses by me), making it the second most active article on this website. I decided to follow up on that article with a demo that shows the effects that different L&Fs have on GUI controls, and a demonstration of the JTattoo library of professional L&Fs.
Today’s article and the demo are based on original work by Karthik Ponudurai, who has written a guest article here about an interesting technique to interface a Java GUI to a Matlab application, a couple of years ago.
JTattoo is a 3rd-party open-source library. The purpose of including it in the demo, in addition to its natural use as a professional set of L&Fs, is to demonstrate how easy it is to integrate 3rd-party L&Fs in Matlab. In the demo I use the current latest available JTattoo library (1.6.7), but you can always download the latest version and replace the JTattoo.jar file. JTattoo contains a large set of different L&Fs that can be used independently (screenshots). The nice thing about L&Fs is that since all Matlab GUI is based on Java Swing, the new L&Fs automatically affect Matlab controls just like native Java ones.
The demo can be downloaded from the Matlab File Exchange. After downloading, unzip it into any folder on your Matlab path and run JTattooDemo.m.
A Matlab figure is displayed with two panels, one containing Matlab uicontrols (within a simple uiflowcontainer) and the other containing Java components.
Two main menus are available: System enables selecting the standard Swing L&Fs that are installed on your system (this varies a bit between platforms and Matlab releases); JTattoo enables selecting one of the JTattoo L&Fs. Once the user selects any of the L&F menu items, the entire figure is updated. This is done by calling
javax.swing.SwingUtilities.updateComponentTreeUI()
on the figure’s Java Frame‘s content pane. Both the Matlab and the Java controls within the figure are automatically updated by this Swing function to reflect the newly-selected L&F. Care is taken to update the L&F on the EDT, to prevent racing-condition issues.It should be noted that the demo resets the L&F after updating the figure, otherwise any new figure or window would open using the newly-selected L&F. This is done in the updateInterface function as follows:
function updateInterface( lookandfeel ) % Preserve the original L&F, before updating originalLnF = javax.swing.UIManager.getLookAndFeel; % Update the L&F in the demo figure as requested ... (all the existing code within the function) % Restore the original L&F for any new figure/window javax.swing.UIManager.setLookAndFeel(originalLnF); end % updateInterface |
Note that after changing the L&Fs several times, some L&F properties night get “mixed-up” causing odd-looking L&Fs. The simplest solution in this case is to restart Matlab…
Readers in Israel are invited to attend a free training seminar that I will present on advanced Matlab topics in Herzliya, on Thursday April 4, 2013. The seminar is free, but requires registration. Additional details here. I will speak in Hebrew, but the presentation will be in English and I will be happy to answer questions in English.
Neat! That updateInterface snippet looks like a prime use case for an onCleanup function, to restore the originalLnF no matter what, such as if any of the code errors or the user Ctrl-C’s 🙂
@Eric, indeed so. However, I usually prefer not to use onCleanup since it is only available in R2008a onward. Instead, I tend to use simple try-catch blocks, which are backward compatible all the way back to Matlab 6. As recently shown in the EigenVector blog, there are still plenty of users out there who use non-recent Matlab releases. So in general, unless there is a very good reason otherwise, I try to make my code as backward-compatible as possible. Luckily, most of the newish Matlab constructs have equivalent backward-compatible constructs so I am able to do so without much effort.
Nice write up. I’m studying something similar here at Fort Lewis College. It’s always helpful to
learn new stuff from fellow writers and collect expertise
from new sources. I’d like to incorporate some of this material
on my own blog (if you don’t mind). And of course, I’ll put up a backlink to your site at undocumentedmatlab.com on my own page. Kudos for posting.
Very interesting, thanks! Just a small suggestion: To make you code cross-platform, in the file JTattooDemo.m you could change the line:
to:
because in GNU/Linux the file separator is “/” and not “\” 🙂
@Digna – I normally use ‘/folder/file’ format for compatibility (this works on all platforms), it was simply an oversight in this case… – thanks for pointing it out
Yair, Thank you so much for this. I just want to how to use the Jtattoo LnF’s UIcontrols in making a new GUI in matlab. I have only experience making GUI in matlab by using matlab GUIDE. Is it possible that I can change the design of the UIcontrols in matlab GUIDE? How can I do it?
Thanks.
@Kilojoules – of course you can – read here: http://undocumentedmatlab.com/blog/modifying-matlab-look-and-feel/
Somewhere I have a fundamental misunderstanding of how to access third-party .jar files. This *seems* straightforward enough to me, but it doesn’t appear to work:
I expect to see two buttons, each with a different look and feel. However, I’ve tried every iteration of accessing the jtattoo LaF’s and can’t seem to set them. The .jar shows up when I type ‘javaclasspath’, so I think it should be accessible. This same error occurs regardless of which LaF I try to access from the JTattoo library, as well as any other 3rd-party library. However, if I use ‘newLFName2’ above, things work nicely.
So, I suspect I’m not understanding how to access the 3rd-party classes correctly. Any advice for a beginner?
@HAC – JTattoo LNFs cannot be used like simple LNFs. Take a look at the source code of the utility to see how they can be used.
Ah, that explains it. Thanks, Yair. Perhaps other LNFs (weblaf, PgsLookAndFeel, etc.) are the same way as well.
[…] like to welcome repeat guest blogger Karthik Ponudurai, who has previously written here about the JTattoo look-and-feel, and about integration of Java controls’ events with Matlab callbacks. Today, Karthik […]
Hi Yair,
Thanks for the post. You can also do a lot to customize the appearance of each LaF by using the public ‘setTheme’ method, which accepts a java.util.Properties object (the full set of customizable properties can be found Here ). This works well the very first time you set a particular LaF for a figure in the current MATLAB session. However, it seems that if you change some of the properties, and then build another figure with that same LaF, only a subset of those changes are actually registered. For example, if I set the ‘menuSelectionBackgroundColor’ and ‘menuBackgroundColor’ properties for the GraphiteLookAndFeel, only the ‘menuSelectionBackgroundColor’ property change has any subsequent effect. So far, restarting MATLAB seems to be the only fix. I’m currently using 2013a on a mac OSX system. Any thoughts would be appreciated!
Cheers!
Hi Yair,
I’ve seen the demo but when I adapt it into my gui code gcbf is not recognize.
How can I resolve it ? Can you give me a simply implementation in gui