- Undocumented Matlab - https://undocumentedmatlab.com/blog_old -
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:
% Create and display the text label url = 'UndocumentedMatlab.com'; labelStr = ['<html>More info: <a href="">' url '</a></html>']; 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 [3]. 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 [4], here [5] and here [6]). In fact, I have already shown how we can use borderless buttons in Matlab axes [6] 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 [7] (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
Article printed from Undocumented Matlab: https://undocumentedmatlab.com/blog_old
URL to article: https://undocumentedmatlab.com/blog_old/hyperlink-text-labels
URLs in this post:
[1] Image: https://undocumentedmatlab.com/feed/
[2] email feed: https://undocumentedmatlab.com/subscribe_email.html
[3] another post: https://undocumentedmatlab.com/blog/displaying-animated-gifs
[4] here: https://undocumentedmatlab.com/blog/button-customization
[5] here: https://undocumentedmatlab.com/blog/customizing-matlab-labels
[6] here: https://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties
[7] multiple callbacks: https://undocumentedmatlab.com/blog/uicontrol-callbacks
[8] Inactive Control Tooltips & Event Chaining : https://undocumentedmatlab.com/blog_old/inactive-control-tooltips-event-chaining
[9] Tab panels – uitab and relatives : https://undocumentedmatlab.com/blog_old/tab-panels-uitab-and-relatives
[10] uitree : https://undocumentedmatlab.com/blog_old/uitree
[11] uiinspect : https://undocumentedmatlab.com/blog_old/uiinspect
[12] Customizing editboxes : https://undocumentedmatlab.com/blog_old/customizing-editboxes
[13] Transparent labels : https://undocumentedmatlab.com/blog_old/transparent-labels
Click here to print.
Copyright © Yair Altman - Undocumented Matlab. All rights reserved.
5 Comments To "Hyperlink text labels"
#1 Comment By Yaroslav On October 21, 2015 @ 9:03 am
Hi,
Apparently, there is a typo in the 3rd line of the code; it should read:
#2 Comment By Yair Altman On October 21, 2015 @ 9:16 am
Thanks Don, corrected
#3 Comment By oussama On August 27, 2016 @ 1:43 am
hi, but how to connect the code with the label ??
#4 Comment By Yair Altman On August 27, 2016 @ 10:51 pm
@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 @ 12:08 am
Thanks for kind useful information.