# \[ListBox\] and its custom \[Component\]'s mouse event

**URL:** <https://forum.juce.com/t/listbox-and-its-custom-component-s-mouse-event/21846>\
**Category:** General JUCE discussion\
**Created:** [April 16, 2017, 9:42am UTC](https://forum.juce.com/t/listbox-and-its-custom-component-s-mouse-event/21846 "2017-04-16T09:42:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![uuich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/uuich/32/19970_2.png) [@uuich](https://forum.juce.com/u/uuich)\
**Post date:** [April 16, 2017, 9:42am UTC](https://forum.juce.com/t/listbox-and-its-custom-component-s-mouse-event/21846/1 "2017-04-16T09:42:41Z")

</div>

Hi and Hello

I came to the point when im in need of a list that contains set of **editable** [Label] components.

[ListBox] was a natural choice here as i need to utilize its features. Unfortunatelly i cannot due to the nature of how [MouseEvent]'s are consumed.

Im rather aware of [MouseListener] approach but that doesnt seem to make the job ether.

I desire of a [ListBox]'es events to be **active** (receivable) with possibility of **editing** [Label] components that it contains as well.

I wonder if this is achievable in any non over convoluted way.  
My first doubt was that the ListBox::mouseUp() method receives wrong coordinates, but its just assumpltion.

**Does anyone tried similar scenario ?**

* * *

```
class List : public Component, public ListBoxModel {
public:
	List() {
		box.setModel(this);
		box.setColour(ListBox::backgroundColourId, Colour::greyLevel(0.2f));
		box.setRowHeight(30);
		addAndMakeVisible(box);
	}

	void resized() override {
		box.setBounds(getLocalBounds());
	}

	void paintListBoxItem(int rowNumber, Graphics &g, int width, int height, bool rowIsSelected) override {
		if(!isPositiveAndBelow(rowNumber, names.size())) return;
		g.fillAll(Colour::greyLevel(rowIsSelected ? 0.15f : 0.05f));
	}

	int getNumRows() override {
		return names.size();
	}

	Component* refreshComponentForRow(int row, bool selected, Component* component) override {
		auto *item = static_cast<Item*>(component);
		if(row < names.size()) {
			if(!item)
				item = new Item(box, names);
			item->update(row, names[row]);
		} else {
			delete component;
			item = nullptr;
		}
		return item;
	}

	void addItem(const String &name = String("new item")) {
		names.add(name);
		box.updateContent();
	}

	void listBoxItemClicked(int row, const MouseEvent &) { // NOT CALLED !!!!
		DBG("selected row : " + String(row));
	}

private:
	ListBox box;
	StringArray names;
}

```

* * *

```
class MainComponent : public Component {
public:
	MainComponent() {
		addAndMakeVisible(list);

		for(int i = 0; i < 7; i++)
			list.addItem("item" + String(i));

		setSize(600, 400);
	}

	void paint(Graphics& g) override {
		g.fillAll(Colours::whitesmoke);
	}

	void resized() override {
		list.setBounds(0, 0, 110, getHeight());
	}

private:
	List list;

	JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MainComponent)
};
```

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [April 16, 2017, 9:59am UTC](https://forum.juce.com/t/listbox-and-its-custom-component-s-mouse-event/21846/2 "2017-04-16T09:59:24Z")

</div>

Check out the TableListBox in the JUCE Demo project

Rail

---

<div class="post-metadata">

**Author:** ![uuich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/uuich/32/19970_2.png) [@uuich](https://forum.juce.com/u/uuich)\
**Post date:** [April 16, 2017, 10:17am UTC](https://forum.juce.com/t/listbox-and-its-custom-component-s-mouse-event/21846/3 "2017-04-16T10:17:44Z")

</div>

Ah, of course, my bad I’ve ignored that example … thank you Rail !

* * *

```
void mouseDown (const MouseEvent& event) override {
    // single click on the label should simply select the row
    owner.table.selectRowsBasedOnModifierKeys (row, event.mods, false);
    Label::mouseDown (event);
}
```

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [August 14, 2018, 10:51am UTC](https://forum.juce.com/t/listbox-and-its-custom-component-s-mouse-event/21846/4 "2018-08-14T10:51:19Z")

</div>

Could this tiny example please be added to the refreshComponent\* documentation for ListBox and TableListBox please!
