|
John Anderson wrote:
>
>
> I've just started playing around with UITree and thanks to Brad
> Phelan's tutorial have managed to implment most of the
> functionality
> I desired. However, one feature of the JTree java class I'd quite
> like to utilise is the ability to dynamically update the tree so
> that
> for nodes can be added to the current display. It wasn't clear to
> me
> from the UITree documentation how this might be achieved directly
> in
> matlab. I've had some success implementing the JTree Java class in
> matlab but as I'm not a Java programmer I'm finding it a little
> tricky to get the dynamic update working. Any advice on either
> using
> the dynamic update feature of JTree from matlab or how this might
> be
> achieved through the UITree class would be much appreciated,
> cheers,
> john
I've had a little more success using the TreeModel java class which
can be used in conjunction with the uitree matlab class. This
function allows you to dynamically add and delete nodes from a tree
and automatically updates the tree
function treeExperiment6
% derived from Brad Phelan's tree demo
% create a tree model based on UITreeNodes and insert into uitree.
% add and remove nodes from the treeModel and update the display
import javax.swing.tree.*;
% figure window
f = figure('WindowStyle', 'docked', 'Units', 'normalized' );
f = gcf;
b1 = uicontrol( 'string','add Node', ...
'units' , 'normalized', ...
'position', [0 0.5 0.5 0.5], ...
'callback', @b1_cb ...
);
b2 = uicontrol( 'string','remove Node', ...
'units' , 'normalized', ...
'position', [0.5 0.5 0.5 0.5], ...
'callback', @b2_cb ...
);
% create top node
rootNode = UITreeNode('dummy', 'Root Node', [], 0);
treeModel = DefaultTreeModel( rootNode );
tree = uitree;
tree.setModel( treeModel );
set(tree, 'Units', 'normalized',...
'position', [0 0 1 0.5]);
set( tree, 'NodeSelectedCallback', @selected_cb );
tree.setSelectedNode( rootNode );
function selected_cb( tree, ev )
nodes = tree.getSelectedNodes;
node = nodes(1);
path = node2path(node);
path
end
function path = node2path(node)
path = node.getPath;
for i=1:length(path);
p{i} = char(path(i).getName);
end
if length(p) > 1
path = fullfile(p{:});
else
path = p{1};
end
end
% add node
function b1_cb( h, env )
nodes = tree.getSelectedNodes;
node = nodes(1);
parent = node;
childNode = UITreeNode('dummy', 'Child Node', [], 0);
treeModel.insertNodeInto(childNode,
parent,parent.getChildCount());
% expand to show added child
tree.setSelectedNode( childNode );
% insure additional nodes are added to parent
tree.setSelectedNode( parent );
end
% remove node
function b2_cb( h, env )
nodes = tree.getSelectedNodes;
node = nodes(1);
if ~node.isRoot
nP = node.getPreviousSibling;
nN = node.getNextSibling;
if ~isempty( nN )
tree.setSelectedNode( nN );
elseif ~isempty( nP )
tree.setSelectedNode( nP );
else
tree.setSelectedNode( node.getParent );
end
node
treeModel.removeNodeFromParent( node );
end
end
end
|