- Pressing escape will close the dropdown window without the need to move the mouse. To evoke the simulated closing function, the mouse needs to move by at least 1 pixel after pressing escape.
- The simulated closing function is evoked when the mouse moves outside of the dropdown object even if the dropdown window is still opened. Re-entering the window does nothing since the app already executing the closing function. There are alternative behaviors you could arrange such as using an underlying uipanel as a pointer object or by assigning a EnterFcn to detect when the mouse re-enters the dropdown menu.
- Pointer manager is supported with AppDesigner starting with Matlab R2021a.
Appdesigner get dropdown closing workaround
5 views (last 30 days)
Show older comments
Im trying to find a workaround for getting a dropdownclosing event.
i want to highlight certain curves in my plot when i open the dropdown menu. currently i do this by changing the color via a dropdownopening callback.
Is there a way to reset the change when i close the dropdown menu? so far i can only reset the change by opening another dropdown menu, but this is not optimal.
0 Comments
Accepted Answer
Adam Danz
on 4 Dec 2021
Edited: Adam Danz
on 4 Dec 2021
If you're closing the dropdown menu by selecting an option, you should include the reset actions in the DropDownValueChanged callback function. However, if a selection is not made, that function will not be evoked in which case this demo may be helpful.
How to simulate a DropDownClosing function
There may be an undocumented event that could be used by a listener to detect when the menu is closed but I don't know of such an event at the moment.
Instead, this demo shows how to use the mouse position to detect when the mouse leaves the dropdown menu area which triggers a callback function that you can define. This uses a pointer manager which became available in AppDesigner in Matlab R2021a (see announcement).
Steps
properties (Access = private)
pointMngr % pointer manager, set in startup and elsewhere within the app
end
2. Add a startup function to your app (how to add a startup function). The startup function will set up the pointer manager default settings.
% Code that executes after component creation
function startupFcn(app)
% Set up default pointer manager behavior which does nothing for now.
app.pointMngr.enterFcn = [];
app.pointMngr.exitFcn = [];
app.pointMngr.traverseFcn = [];
iptSetPointerBehavior(app.DropDown, app.pointMngr) % assign to dropdown obj
% Enable pointer manager for app
iptPointerManager(app.UIFigure,'enable');
end
3. Add a private function that defines "closing" actions (how to add a private function). This function will be evoked when the mouse leaves the dropdown menu area or when a selection is made in the dropdown menu. Importantly, the last two lines reset the exit behavior so that this function isn't triggered every time the mouse travels across the dropdown button without interacting with it. The exit function will be defined again by the DropDownOpeningFcn (step 4). The rest of this block merely indicates that the function was evoked.
function cursorEnterLeavePanelFcn(app, ~)
% Evoked by the pointer manager when the mouse leaves the
% dropdown menu area.
% Indicate exit by updating text in the axes
cla(app.UIAxes)
text(app.UIAxes, 0.1, 0.7, ['CLOSED', newline, datestr(now())],...
'FontSize', 18)
% Return pointer manager to default behavior
app.pointMngr.exitFcn = [];
iptSetPointerBehavior(app.DropDown, app.pointMngr)
end
4. Assign dropdown opening function. In addition to whatever actions this callback function already contains in your code, set the exitFcn for the pointer manager (first two lines below). The rest of this block merely changes the text within the app axes to indicate that the function was evoked.
% Drop down opening function: DropDown
function DropDownOpening(app, event)
% set exit function that is evoked when the mouse leaves the dropdown object.
app.pointMngr.exitFcn = @(~,~) cursorEnterLeavePanelFcn(app, app.DropDown);
iptSetPointerBehavior(app.DropDown, app.pointMngr)
% Indicate dropdown opened in the axes
cla(app.UIAxes)
text(app.UIAxes, 0.1, 0.7, ['OPENED', newline, datestr(now())],...
'FontSize', 18, 'Color', 'r')
end
5. Assign value changed function to dropdown. The menu will close when a selection is made so this evokes the closing actions instead of relying on mouse movement.
% Value changed function: DropDown
function DropDownValueChanged(app, event)
cursorEnterLeavePanelFcn(app, [])
end
Limitations
2 Comments
Adam Danz
on 5 Dec 2021
That sounds interesting. Could you share the URL where you learned about this or share the event name? If it only executes after the dropdown menu is clicked, you could just use the DropDownValueChanged function.
More Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!