You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@netbeans.apache.org by Marco Rossi <ma...@markreds.it> on 2018/04/13 09:28:52 UTC

Programmatically select a node after being created

Hi there,
can someone tell me how to programmatically select a node in my beanTreeView (backed up by an explorerManager) after being created in my data model? I tried to do this in parent node, by adding a NodeListener like this:

	parentNode.addNodeListener(new NodeAdapter() {
            @Override
            public void childrenAdded(NodeMemberEvent ev) {
                if (ev.isAddEvent()) {
                    final Node node = ev.getNode();
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                LibraryViewerTopComponent.findInstance().getExplorerManager().setSelectedNodes(new Node[] { node });
                            } catch (PropertyVetoException ex) {
                                MessageBox.error(ex);
                            }
                        }
                    });
                }
            }
        });

When I create a new item in my data model, the ChildFactory refreshes asynchronously itself and creates a new node representing the data. Unfortunately this does’t work because the child node may be not exists yet in the explorer manager. Any suggestions?
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@netbeans.apache.org
For additional commands, e-mail: users-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


Re: Programmatically select a node after being created

Posted by Marco Rossi <ma...@markreds.it>.
Ok, I decided to subclass my beanTreeView.

public final class LibraryViewerTopComponent extends TopComponent implements ExplorerManager.Provider {
	private final ExplorerManager explorerManager = new ExplorerManager();
	private BeanTreeView beanTreeView;
	
	private final class LibraryTreeView extends BeanTreeView implements TreeModelListener {
        
        	@Override
        	protected NodeTreeModel createModel() {
            		NodeTreeModel model = super.createModel();
            		model.addTreeModelListener(this);
            		return model;
        	}

        	@Override
        	public void treeNodesInserted(TreeModelEvent e) {
            		Object[] children = e.getChildren();
            		if (children.length > 0) {
                		TreePath tp = e.getTreePath();
                		tree.setSelectionPath(tp.pathByAddingChild(children[0]));
            		}
        	}

        	@Override
        	public void treeNodesRemoved(TreeModelEvent e) {
            		// NOP
        	}
        
        	@Override
        	public void treeNodesChanged(TreeModelEvent e) {
            		// NOP
        	}

        	@Override
        	public void treeStructureChanged(TreeModelEvent e) {
            		// NOP
        	}
    	}
	
	public LibraryViewerTopComponent() {
		initComponents();
		// initialize data model
		…
		…
	}
	
	private void initComponents() {
        	beanTreeView = new LibraryTreeView();
        	setBackground(java.awt.Color.white);
        	setOpaque(true);
        	setLayout(new java.awt.BorderLayout());
        	add(beanTreeView, java.awt.BorderLayout.CENTER);
    	}
	
	@Override
    	public ExplorerManager getExplorerManager() {
    		return explorerManager;
	}
}

It works but I appreciate any other suggestions/comments.


> Il giorno 14 apr 2018, alle ore 08:56, Emilian Bold <em...@protonmail.ch> ha scritto:
> 
> I'm also curious about this.
> 
> It seems to me the API is somewhat incomplete, specifically in the async area. We have no Future to listen to and react. So, it's all fire-and-forget when often times you want to do something after the node has been created / shown.
> 
> In your case you control the beanTreeView so as a last resort I guess you could subclass the view and listen on the JTree directly (it's a protected field). Still... it's ugly.
> 
> Not really sure if there's some other way. If you do find it, please come back on the mailing list and let us know.
> 
> --emi
> 
> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> 
> On 13 April 2018 12:28 PM, Marco Rossi <ma...@markreds.it> wrote:
> 
>> Hi there,
>> 
>> can someone tell me how to programmatically select a node in my beanTreeView (backed up by an explorerManager) after being created in my data model? I tried to do this in parent node, by adding a NodeListener like this:
>> 
>> parentNode.addNodeListener(new NodeAdapter() {
>> 
>> @Override
>> 
>> public void childrenAdded(NodeMemberEvent ev) {
>> 
>> if (ev.isAddEvent()) {
>> 
>> final Node node = ev.getNode();
>> 
>> SwingUtilities.invokeLater(new Runnable() {
>> 
>> @Override
>> 
>> public void run() {
>> 
>> try {
>> 
>> LibraryViewerTopComponent.findInstance().getExplorerManager().setSelectedNodes(new Node[] { node });
>> 
>> } catch (PropertyVetoException ex) {
>> 
>> MessageBox.error(ex);
>> 
>> }
>> 
>> }
>> 
>> });
>> 
>> }
>> 
>> }
>> 
>> });
>> 
>> When I create a new item in my data model, the ChildFactory refreshes asynchronously itself and creates a new node representing the data. Unfortunately this does’t work because the child node may be not exists yet in the explorer manager. Any suggestions?


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@netbeans.apache.org
For additional commands, e-mail: users-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


Re: Programmatically select a node after being created

Posted by Emilian Bold <em...@protonmail.ch>.
I'm also curious about this.

It seems to me the API is somewhat incomplete, specifically in the async area. We have no Future to listen to and react. So, it's all fire-and-forget when often times you want to do something after the node has been created / shown.

In your case you control the beanTreeView so as a last resort I guess you could subclass the view and listen on the JTree directly (it's a protected field). Still... it's ugly.

Not really sure if there's some other way. If you do find it, please come back on the mailing list and let us know.

--emi

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

On 13 April 2018 12:28 PM, Marco Rossi <ma...@markreds.it> wrote:

> Hi there,
> 
> can someone tell me how to programmatically select a node in my beanTreeView (backed up by an explorerManager) after being created in my data model? I tried to do this in parent node, by adding a NodeListener like this:
> 
> parentNode.addNodeListener(new NodeAdapter() {
> 
> @Override
> 
> public void childrenAdded(NodeMemberEvent ev) {
> 
> if (ev.isAddEvent()) {
> 
> final Node node = ev.getNode();
> 
> SwingUtilities.invokeLater(new Runnable() {
> 
> @Override
> 
> public void run() {
> 
> try {
> 
> LibraryViewerTopComponent.findInstance().getExplorerManager().setSelectedNodes(new Node[] { node });
> 
> } catch (PropertyVetoException ex) {
> 
> MessageBox.error(ex);
> 
> }
> 
> }
> 
> });
> 
> }
> 
> }
> 
> });
> 
> When I create a new item in my data model, the ChildFactory refreshes asynchronously itself and creates a new node representing the data. Unfortunately this does’t work because the child node may be not exists yet in the explorer manager. Any suggestions?
> 
> 
> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> 
> To unsubscribe, e-mail: users-unsubscribe@netbeans.apache.org
> 
> For additional commands, e-mail: users-help@netbeans.apache.org
> 
> For further information about the NetBeans mailing lists, visit:
> 
> https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@netbeans.apache.org
For additional commands, e-mail: users-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists