Solutions for Matlab Simulink and RTW
Friday, 21 November 2008
 
  Home arrow Tutorials arrow Adding Popups To The Tree Control  
Home Consulting Tutorials Downloads Matlab Snippets Code Candy Contact Links
Adding Popups To The Tree Control Print E-mail
Written by Brad Phelan   
Wednesday, 13 April 2005

I recieved an email asking whether it was possible to demonstrate adding popup menus to one of the tree controls. So here it is.

Modify the two_viewers.m file so it looks like the below file. There is not a huge amount of change to be done. The trick is knowing the Java API's and how Matlab wraps up the listener methods as callbacks. Generally to find what Matlab callbacks are available for Java objects execute the command set on the object. For example.

>> t = uitree;

>> set ( t )

<snip>

NodeSelectedCallback: {}
NodeWillExpandCallback: {}
NodeExpandedCallback: {}
NodeWillCollapseCallback: {}
NodeCollapsedCallback: {}
NodeDroppedCallback:
{}

>> set ( t.Tree )

<snip>

 TreeCollapsedCallback: string -or- function handle -or- cell array
 TreeExpandedCallback: string -or- function handle -or- cell array
 ComponentHiddenCallback: string -or- function handle -or- cell array
 ComponentMovedCallback: string -or- function handle -or- cell array
 ComponentResizedCallback: string -or- function handle -or- cell array
 ComponentShownCallback: string -or- function handle -or- cell array
 MouseDraggedCallback: string -or- function handle -or- cell array
 MouseMovedCallback: string -or- function handle -or- cell array
 ValueChangedCallback: string -or- function handle -or- cell array
 PropertyChangeCallback: string -or- function handle -or- cell array
 FocusGainedCallback: string -or- function handle -or- cell array
 FocusLostCallback: string -or- function handle -or- cell array
 CaretPositionChangedCallback: string -or- function handle -or- cell array
 InputMethodTextChangedCallback: string -or- function handle -or- cell array
 KeyPressedCallback: string -or- function handle -or- cell array
 KeyReleasedCallback: string -or- function handle -or- cell array
 KeyTypedCallback: string -or- function handle -or- cell array
 MouseClickedCallback: string -or- function handle -or- cell array
 MouseEnteredCallback: string -or- function handle -or- cell array
 MouseExitedCallback: string -or- function handle -or- cell array
 MousePressedCallback: string -or- function handle -or- cell array
 MouseReleasedCallback: string -or- function handle -or- cell array
 HierarchyChangedCallback: string -or- function handle -or- cell array
 VetoableChangeCallback: string -or- function handle -or- cell array
 ComponentAddedCallback: string -or- function handle -or- cell array
 ComponentRemovedCallback: string -or- function handle -or- cell array
 AncestorMovedCallback: string -or- function handle -or- cell array
 AncestorAddedCallback: string -or- function handle -or- cell array
 AncestorRemovedCallback: string -or- function handle -or- cell array
 AncestorMovedCallback_: string -or- function handle -or- cell array
 AncestorResizedCallback: string -or- function handle -or- cell array
 MouseWheelMovedCallback: string -or- function handle -or- cell array
 TreeWillCollapseCallback: string -or- function handle -or- cell array
 TreeWillExpandCallback: string -or- function handle -or- cell array

Note that I'm also using anonymous function to create the callbacks for the JMenuItem objects. anonymous functions are just simple one line nested functions. For example

>> myfun = @(x)x+1

>> myfun(1)

ans =

2

Here is the new function. Have fun!

% TWO_VIEWERS
%
% Sets up two directory viewers side by side and two buttons.
% When pressed, the buttons display the selected files from
% the corresponding tree.
%
% Demonstrates how to use the directory_viewer tree
% control.
%
% Added Java Popups to one of the tree controls


function two_viewers
import javax.swing.*; % Set up figure figure('units', 'normalized'); b1 = uicontrol( 'string','Selections', ... 'units' , 'normalized', ... 'position', [0 0.5 0.5 0.5], ... 'callback', @b1_cb ... ); b2 = uicontrol( 'string','Selections', ... 'units', 'normalized', ... 'position', [0.5 0.5 0.5 0.5], ... 'callback', @b2_cb ... ); t1 = directory_viewer; set(t1, 'Units', 'normalized',... 'position', [0 0 0.5 0.5]); t2 = directory_viewer; set(t2, 'Units', 'normalized',... 'position', [0.5 0 0.5 0.5]); function b1_cb(h, env)
disp( t1.SelectedDirectories() )
end function b2_cb(h, env)
disp( t2.SelectedDirectories() )
end % Define callbacks for context menu items cb1 = @(src,ev)disp('x'); cb2 = @(src,ev)disp('y'); cb3 = @(src,ev)disp('z'); % Define the context menu items item1 = JMenuItem('button1'); item2 = JMenuItem('button2'); item3 = JMenuItem('button3'); set(item1, 'ActionPerformedCallback', cb1); set(item2, 'ActionPerformedCallback', cb1); set(item3, 'ActionPerformedCallback', cb1); popup = JPopupMenu; popup.add(item1); popup.add(item2); popup.add(item3); set(b2,'UIContextMenu', cmenu); % Get the tree from the tree model tree = t2.Tree; % Add a mouse pressed callback from the model set(tree, 'MousePressedCallback', @mouse_cb); % Mouse Pressed Handler function mouse_cb(h, ev)
if ev.getModifiers()== ev.META_MASK
popup.show(tree, ev.getX, ev.getY); popup.repaint; % If you want to get fancy here you % can use the JTree.getPathForLocation % method to find the actual node for the % point clicked and then generate a custom % popup based on the node. end end end
Last Updated ( Friday, 22 April 2005 )
< Prev   Next >
Sponsored by

Sole Central

Your one stop shop for Birkenstock shoes and sandles.