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

Hyperlink text labels

Posted By Yair Altman On October 21, 2015 | 5 Comments

It is often useful to include hyperlinked text labels in GUIs. Such labels provide single-click access to important functionality, improve branding, and are non-intrusive action controls having a lower visual impact than a full-blown button. There are several ways that we can display such hyperlinks in Matlab GUIs, and today I will show one of my favorites.
The basic idea is to create a Java text label control whose label displays an HTML link. The control is modified to change the mouse cursor when it hovers over the hyperlink, and a mouse-click callback is set to open the hyperlink target in the system browser:

hyperlink text label
hyperlink text label

% Create and display the text label
url = 'UndocumentedMatlab.com';
labelStr = ['More info: ' url ''];
jLabel = javaObjectEDT('javax.swing.JLabel', labelStr);
[hjLabel,hContainer] = javacomponent(jLabel, [10,10,250,20], gcf);
% Modify the mouse cursor when hovering on the label
hjLabel.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.HAND_CURSOR));
% Set the label's tooltip
hjLabel.setToolTipText(['Visit the ' url ' website']);
% Set the mouse-click callback
set(hjLabel, 'MouseClickedCallback', @(h,e)web(['http://' url], '-browser'))


Note the visual illusion here: we do not directly click the hyperlink (note that its href is empty), but rather the label control. The end-result is the same.
Also note that this technique could be used to easily display clickable icons/images, including animated and transparent GIFs, by simply setting the label’s HTML string to display the relevant image. I have already shown how to do this in another post [1]. Uses could range from clickable logo images to clickable help icons.
We could also use a flat (borderless) button. I have already posted related articles about button customizations in Matlab GUIs (here [2], here [3] and here [4]). In fact, I have already shown how we can use borderless buttons in Matlab axes [4] to display a non-obtrusive control for controlling plot properties. The code would be very similar to the above, except that we would use a JButton rather than a JLabel, and also need to setBorder([]) and similar button-specific modifications. Buttons have a bit more functionality and customizability than simple text labels; the appearance would be the same, but the extra customizability may be handy in very special cases, although not for most use-cases.
One of the benefits of using either JLabels or JButtons is that they enable multiple callbacks [5] (e.g., FocusGained,FocusLost) and properties (e.g., VerticalAlignment,ToolTipText) that the standard Matlab text uicontrol does not provide (not to mention their builtin ability to display HTML, which Matlab’s uicontrol text labels do not posses).

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


5 Comments (Open | Close)

5 Comments To "Hyperlink text labels"

#1 Comment By Yaroslav On October 21, 2015 @ 09:03

Hi,
Apparently, there is a typo in the 3rd line of the code; it should read:

...
jLabel = javaObjectEDT('javax.swing.JLabel', labelStr);
...

#2 Comment By Yair Altman On October 21, 2015 @ 09:16

Thanks Don, corrected

#3 Comment By oussama On August 27, 2016 @ 01:43

hi, but how to connect the code with the label ??

#4 Comment By Yair Altman On August 27, 2016 @ 22:51

@oussama – read the post carefully. I showed how you can attach Matlab code to the label’s MouseClickedCallback property. You can attach any Matlab callback function that accepts 2 input args (the label object handle and the eventData object).

#5 Comment By پروژه متلب On November 13, 2016 @ 00:08

Thanks for kind useful information.


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

URL to article: https://undocumentedmatlab.com/articles/hyperlink-text-labels

URLs in this post:

[1] another post: http://undocumentedmatlab.com/blog/displaying-animated-gifs

[2] here: http://undocumentedmatlab.com/blog/button-customization

[3] here: http://undocumentedmatlab.com/blog/customizing-matlab-labels

[4] here: http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties

[5] multiple callbacks: http://undocumentedmatlab.com/blog/uicontrol-callbacks

[6] Transparent labels : https://undocumentedmatlab.com/articles/transparent-labels

[7] Customizing Matlab labels : https://undocumentedmatlab.com/articles/customizing-matlab-labels

[8] Customizing axes tick labels : https://undocumentedmatlab.com/articles/customizing-axes-tick-labels

[9] Toolbar button labels : https://undocumentedmatlab.com/articles/toolbar-button-labels

[10] Syntax highlighted labels & panels : https://undocumentedmatlab.com/articles/syntax-highlighted-labels-panels

[11] Images in Matlab uicontrols & labels : https://undocumentedmatlab.com/articles/images-in-matlab-uicontrols-and-labels

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