JTattoo look-and-feel demo

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.

Demo of using different Look-and-Feels in Matlab GUI

Demo of using different Look-and-Feels in Matlab GUI

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…

Matlab open training day (Israel) - click for details

Matlab open training day (Israel) - click for details

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.

Related posts:

  1. Modifying Matlab’s Look-and-Feel Matlab's entire Look-and-Feel (PLAF, or L&F) can be modified at the control or application level - this article shows how...
  2. Real-time trading system demo A real-time Matlab-based end-to-end trading system demo is presented ...
  3. Additional uicontrol tooltip hacks Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....
  4. Inactive Control Tooltips & Event Chaining Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....
  5. Blurred Matlab figure window Matlab figure windows can be blurred using a semi-transparent overlaid window - this article explains how...
  6. The javacomponent function Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations...

Categories: Guest bloggers, GUI, Java, Medium risk of breaking in future versions, UI controls

Tags: , , , ,

Bookmark and SharePrint Print

10 Responses to JTattoo look-and-feel demo

  1. Eric says:

    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 :)

    • Yair Altman says:

      @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.

  2. mormonism says:

    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.

  3. Digna says:

    Very interesting, thanks! Just a small suggestion: To make you code cross-platform, in the file JTattooDemo.m you could change the line:

    javaclasspath( {folder, [folder '\JTattoo.jar']} );

    to:

    javaclasspath( {folder, [folder filesep 'JTattoo.jar']} );

    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

  4. kilojoules says:

    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.

  5. HAC says:

    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:

    hFig = figure;
    hc = uiflowcontainer('v0', 'Units', 'norm', 'Position', [0.1 0.1 0.8 0.8]);
    h1 = uicontrol('String', 'One', 'Parent', hc);
    javaaddpath ./JTattoo-1.6.10.jar;
    m = javax.swing.UIManager();
    oldLF = m.getLookAndFeel;
    newLFName1 = 'com.jtattoo.plaf.aero.AeroLookAndFeel'; % This one fails!
    newLFName2 = 'com.jgoodies.looks.plastic.Plastic3DLookAndFeel'; % This one works!
    m.setLookAndFeel(newLFName1);
    [j2, h2] = javacomponent(javax.swing.JButton('Two')); 
    set(h2, 'Parent', hc);
    m.setLookAndFeel(oldLF);

    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?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

<pre lang="matlab">
a = magic(3);
sum(a)
</pre>