Unwanted Keyboard focus on Component inside table


#1

Hey guys, a bit of a specific issue here, I hope you can help me.

I went and modified JUCE’s Table Demo to reproduce the issue (JUCE\examples\Demo\Source\Demos\WidgetsDemo.cpp). Only mofidication was to use TextEditor components for the “Artist” column, in addition to the existing editable labels and ComboBoxes.

My issue is that whenever a row is selected by clicking on the cell in the “ID” column, the TextEdit component in the “Artist” column gets the keyboard focus. I don’t want that.

My desired behaviour would be to only gain focus when the user clicks on or tabs onto the actual TextEdit, and only then. Funnily enough, if I select the row by clicking on any other column, the TextEdit does not get keyboard focus.

I know this sounds like a very minor problem, but due to other circumstances in my Plugin, this is causing a lot of headaches.

Any ideas?
Cheers.


#2

To give a bit more detailed info, I added the following nested class to TableDemoComponent:

class SongColumnCustomComponent : public Component
{
public:
	SongColumnCustomComponent(TableDemoComponent& td) : owner(td)
	{
		// just put a TextEditor inside this component
		addAndMakeVisible(m_textEditor);

		m_textEditor.onTextChange = [this] { owner.setText(columnId, row, m_textEditor.getText()); };
	}

	void resized() override
	{
		m_textEditor.setBoundsInset(BorderSize<int>(2));
	}

	// Our demo code will call this when we may need to update our contents
	void setRowAndColumn(int newRow, int newColumn)
	{
		row = newRow;
		columnId = newColumn;
		m_textEditor.setText(owner.getText(columnId, row), dontSendNotification);
	}

private:
	TableDemoComponent& owner;
	TextEditor m_textEditor;
	int row, columnId;
};

And then in refreshComponentForCell(…) I added:

	if (columnId == 2) // Artist name column.
	{
		SongColumnCustomComponent* textEditor = static_cast<SongColumnCustomComponent*> (existingComponentToUpdate);

		// If an existing component is being passed-in for updating, we'll re-use it, but
		// if not, we'll have to create one.
		if (textEditor == nullptr)
			textEditor = new SongColumnCustomComponent(*this);

		textEditor->setRowAndColumn(rowNumber, columnId);
		return textEditor;
	}

Other than that it’s identical to the original WidgetsDemo.cpp.