File Exchange

image thumbnail

Add Icons to GUI menus

version 1.4 (22.7 KB) by

Adds icons to the main figure menu of a given GUI

3.33333
3 Ratings

6 Downloads

Updated

View License

Given the handle of a GUI, this function attaches the icons stored in the "UserData" of each menu handle to the figure. As a result, the given GUI is enriched by properly implemented icons, added to the uimenu on the java level.

For the icons, filenames on the path, full qualified file names or java ImageIcon could be given.

Details can be found in the example.

USAGE:

% Attach icon data to the menu handles UserData

set(handles.Menu_File, 'UserData', '') % Also topevel handle could get the icon
set(handles.Menu_File_New, 'UserData', 'File_New.png')
set(handles.Menu_File_Load, 'UserData', 'File_Load.png')
set(handles.Menu_File_Save, 'UserData', 'File_Save.png') % get file on path
set(handles.Menu_File_Exit, 'UserData', 'File_Exit.png')

% Set the icons
Figure_Menu_Add_Icons(handles.Main)

-------
Aknowledgment: Some of the ideas used here are based on the great work of Yair Altman (http://undocumentedmatlab.com/) as presented in http://undocumentedmatlab.com/blog/customizing-menu-items-part-2/ and http://undocumentedmatlab.com/blog/customizing-menu-items-part-3/.

Comments and Ratings (4)

Prem Kumar

Roger GALLOU

Hi Alexander,
Thanks for this work .
I would like to make some changes to the code for its compatibility with R2014b.

a) - I have to change top_level_menu_handle by (which is available for < R2014b) :
 top_level_menu_handles = findobj(GUI_handle, {'Type', 'uimenu', 'Parent', GUI_handle});

b) - Then I give my version about majors changes in function Loop_SubMenues (can certainly be improved):
==========================================
function Loop_SubMenues(JMenu, Menu_Children)
% recursively runs through the menu entries and adds the icon, if exists

if isempty(Menu_Children)
    return
end

%% take care of seperators
% these change the positions
separator_state = get(Menu_Children, {'Separator'});
separator_state(strcmp(separator_state, 'off')) = {0};
separator_state(strcmp(separator_state, 'on')) = {1};

% sort position in ascending order

children_info = {Menu_Children, cell2mat(get(Menu_Children, 'Position')), cell2mat(separator_state)};
[~, iperm] = sort(children_info{2}, 1);
children_info{1} = children_info{1}(iperm);
children_info{2} = children_info{2}(iperm);
children_info{3} = children_info{3}(iperm);

children_info{3} = cumsum(children_info{3});

for num_entry=1:length(Menu_Children)
% pos_entry_java = children_info(num_entry, 2) + children_info(num_entry, 3) - 1; % java indexing
    pos_entry_java = children_info{2}(num_entry) + children_info{3}(num_entry) - 1; % java indexing
    
    try % Pour éviter :

        AddIcon(javaObjectEDT(JMenu.getItem(pos_entry_java)), children_info{1}(num_entry))
        
        % Recurse into submenues
        Loop_SubMenues(javaObjectEDT(JMenu.getItem(pos_entry_java)), get(children_info{1}(num_entry), 'Children'))
    catch
    end
end

Thierry Dalon

Idea is good. But implementation still contains some bugs. (Example gcf is used instead of input hfig)

Dongwon

Updates

1.4

Shortcoming removed as mentioned by Thierry

1.3

Comment added

1.1

Added aknowledgement

MATLAB Release
MATLAB 7.13 (R2011b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video