Rebuild PopUp-menu options when the user clicks to open

10 ビュー (過去 30 日間)
Thomas Watson
Thomas Watson 2018 年 8 月 10 日
コメント済み: Jan 2018 年 8 月 14 日
I've tried searching through most of the popup menu-related questions and didn't find anything similar. Essentially, I'd like to be able to dynamically populate a popup menu every time the user clicks it.
My current approach involves setting the menu's Enable property to ' inactive' so that it appears enabled but still calls my ButtonDownFcn callback. The user clicks the inactive menu, my callback reloads/adds options, and then sets the menu's Enable property to ' on'.
The problem is that the user has to click the menu twice to actually get it to open due to changing it's Enable property.
Is there any way to force the menu to open within my ButtonDownFcn callback so that it behaves normally for the user?
  4 件のコメント
Thomas Watson
Thomas Watson 2018 年 8 月 13 日
I only know of one standard callback for the popup menu, and that's the 'Callback' property. It only gets called when the user has selected an item in the menu, not before. Please correct me if there are other standard callbacks I am missing.
To reiterate what I'm looking for, I want to be able to reload the popup menu's options when the user clicks on it, just before the menu actually opens. The whole point is so I can dynamically add new options that might have become available since the last time the user opened the menu.
If you need a concrete example, I am essentially creating popup options based on some Matlab files that are present within a directory. It's possible to drop more files in the directory while the program is running, so I have to update the menu options without knowing when an update should actually occur. The most intuitive time to update would be just before the user tries to open the menu, thus the whole ButtonDownFcn callback.
The problem described in my original post is that the user has to click once to run the ButtonDownFcn, which updates the menu, and then again to actually open it. The preferred solution would involve clicking the menu only once.
Jan
Jan 2018 年 8 月 14 日
I do not understand, what "when the user clicks on it, just before the menu actually opens" exactly means. But I assume, you find the wanted callback in the Java level: https://undocumentedmatlab.com/blog/uicontrol-callbacks

サインインしてコメントする。

回答 (1 件)

Adam Danz
Adam Danz 2018 年 8 月 13 日
編集済み: Adam Danz 2018 年 8 月 13 日
Here are some ideas for solution
1) Create a small "refresh" button near the menu; the user can click it to refresh the list of files.
2) Create a timer function that continually checks the directory in the background every 'x' seconds and updates the menu whenever there is an update in the directory.
3) Any time the user interacts with the popup menu, it checks for changes in the directory and updates the list (after the user already clicked - which is weird).
If any of those solutions are viable and you need help getting started, I'd be glad to help.

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by