Contribution: TreeViewTypeAheadSelector


I created a simple class that allows you to select items in a TreeView by typing the name. It keeps track of your keypresses and selects the first Item in the TreeView that matches your input. It works more or less the same as in Finder or Explorer.


#ifndef _TreeViewTypeAheadSelector_
#define _TreeViewTypeAheadSelector_

#include "../headers.h"

class TreeViewTypeAheadSelector : public KeyListener
	void setTreeView(TreeView* treeView_);
    bool keyPressed (const KeyPress& key, Component* originatingComponent);
	String search;
	uint32_t lastKeyTime;	
	TreeView* treeView;


#include "TreeViewTypeAheadSelector.h"

	: treeView(0),



void TreeViewTypeAheadSelector::setTreeView(TreeView* treeView_)
	if (treeView != 0)
	treeView = treeView_;
	if (treeView != 0)

bool TreeViewTypeAheadSelector::keyPressed (const KeyPress& key, Component* originatingComponent)
	//let treeview handle keypress first, this will handle navigation with arrow keys etc.
	if (treeView->keyPressed(key))
		search = String::empty;
		return true;
	//if the treeview didn't consume the keypress we will use it
	uint32_t now = Time::getMillisecondCounter();
	if (now - lastKeyTime > 1250) //clear search string when no key has been pressed for 1.25 seconds
		search = String::empty;
	search += key.getTextCharacter();
	lastKeyTime = now;
	//try to find the first item that matches the search string
	for (int i=0; i<treeView->getNumRowsInTree(); ++i) 
		TreeViewItem* item = treeView->getItemOnRow(i);
		if (item != 0) 
			if (item->getUniqueName().startsWithIgnoreCase(search))
				item->setSelected(true, true);
	return true;



Does anyone ever created some code that allows you to select multiple files/items in a TreeView with the keyboard.
So using Shift and Arrow up and down to select multiple items, shouldn’t be too hard i guess and it would be a nice addition to the TreeView code.


Oh, right on, I’m going to drop this into my code right away (if you don’t mind?) - I can basically use it “as is”!

I’ve been applying gentle pressure on Jules to set up something like CPAN is for Perl, call it JPAN perhaps (Juce Programming Archive Network) and if this existed, your code should be there.


Yes you are free to use it. Having some kind of proper code snippet archive would indeed be nice.


Very little pressure required, it’s a great idea! Just need to finish a couple of 3rd party contracts I’m working on, and then that’ll be my next task.


Oh, I knew you liked the idea but :wink: I just wanted to twist your arm a little.