Thread Subject: UITree dynamic update...

Subject: UITree dynamic update...

From: John Anderson

Date: 16 Sep, 2005 10:04:30

Message: 1 of 2

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

Subject: UITree dynamic update...

From: John Anderson

Date: 6 Oct, 2005 08:40:10

Message: 2 of 2

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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
gui Sprinceana 22 Aug, 2009 06:28:56
treeview Sprinceana 22 Aug, 2009 06:28:52
java Yair Altman 26 Feb, 2008 05:43:49
tree Yair Altman 26 Feb, 2008 05:43:49
undocumented Yair Altman 26 Feb, 2008 05:43:28
rssFeed for this Thread

Contact us at files@mathworks.com