|
Hi Yair,
Thanks for your advice. I've had a go at using a simple state variable and this works fine, enabling me to determine where node selections derive from (javaCallbackExp5, below). I did find that when using this approach it was important to include a short pause after setting the new selection path to ensure that the selection change callback had time to execute before resetting the state.
I also had a quick play with the checkboxTreeModel you reccommended (com.mathworks.mwswing.checkboxtree.CheckBoxTree). However, I found that when using a selectionModel to highlight particular paths the nodes were not checked even though the paths appear in the selection model (javaCallbackExp6, below).
Thanks again for your help.
Best wishes,
John
function javaCallbackExp5
state = 0;
import javax.swing.tree.*;
import javax.swing.*;
import com.jidesoft.swing.CheckBoxTree.*
% define a simple tree
rootNode = DefaultMutableTreeNode( 'Objects' );
fNode = DefaultMutableTreeNode( 'window' );
rootNode.add( fNode );
sNode = DefaultMutableTreeNode( 'door' );
rootNode.add( sNode );
% define tree model
treeModel = DefaultTreeModel( rootNode );
% tree
tree = com.jidesoft.swing.CheckBoxTree( treeModel );
treeSelectionModel = tree.getCheckBoxTreeSelectionModel();
% create handle to java object callback properties
hTreeSelectionModel = handle( treeSelectionModel, 'CallbackProperties' );
% set appropriate callback
set( hTreeSelectionModel, 'ValueChangedCallback', {@selectionChanged_cb} );
pathNew = javax.swing.tree.TreePath( [ rootNode sNode ] );
hTreeSelectionModel.setSelectionPath( pathNew );
% place tree on figure
fh = figure( 'windowStyle', 'docked' );
jScrollPane = com.mathworks.mwswing.MJScrollPane(tree);
[jComp,hc ] = javacomponent(jScrollPane,[10,10,300,150],fh);
set(fh, 'userdata', 0 );
hButton = uicontrol( 'style', 'pushbutton',...
'units', 'normalized',...
'position', [ 0.5 0.3 0.3 0.1 ],...
'string', 'default selection',...
'callback', @pushbuttonCallback );
hButton3 = uicontrol( 'style', 'pushbutton',...
'units', 'normalized',...
'position', [ 0.5 0.1 0.3 0.1 ],...
'enable','off',...
'string', 'update...',...
'callback', @pushbutton3Callback );
pathNew = javax.swing.tree.TreePath( [ rootNode sNode ] );
function pushbuttonCallback( hObject, ev )
% restore default selection
state=1;
hTreeSelectionModel.setSelectionPath( pathNew );
pause(0.01);
state=0;
end
function pushbutton3Callback( hObject, ev )
% disable update button
set( hObject, 'enable', 'off' );
end
function selectionChanged_cb( varargin )
% if selection changed manually then enable update button
if state
set( hButton3, 'enable', 'off' );
else
set( hButton3, 'enable', 'on' );
end
end
end
function javaCallbackExp6
import com.mathworks.mwswing.checkboxtree.*
jRoot = DefaultCheckBoxNode('Root');
l1a = DefaultCheckBoxNode('Letters'); jRoot.add(l1a);
l1b = DefaultCheckBoxNode('Numbers'); jRoot.add(l1b);
l2a = DefaultCheckBoxNode('A'); l1a.add(l2a);
l2b = DefaultCheckBoxNode('b'); l1a.add(l2b);
l2c = DefaultCheckBoxNode('<html><b>?'); l1a.add(l2c);
l2d = DefaultCheckBoxNode('<html><i>?'); l1a.add(l2d);
l2e = DefaultCheckBoxNode('3.1415'); l1b.add(l2e);
fh = figure( 'windowStyle', 'docked' );
jTree = com.mathworks.mwswing.MJTree(jRoot);
jCheckBoxTree = CheckBoxTree(jTree.getModel);
jScrollPane = com.mathworks.mwswing.MJScrollPane(jCheckBoxTree);
[jComp,hc] = javacomponent(jScrollPane,[150,10,120,110],gcf);
% get selection model
treeSelectionModel = jCheckBoxTree.getSelectionModel();
% define a path
path = javax.swing.tree.TreePath( [ jRoot l1a ] )
% select path
treeSelectionModel.setSelectionPath( path );
% recover selected path
treeSelectionModel.getSelectionPath
end
|