Drop Down Menu Width
Show older comments
The width of the dropdown menus in Matlab GUIs appear to me to be fixed to their designated width. Is there a way to have the width of the drop down portion autosize to display the entire text of all selectable items without impacting the width of the item within the GUI window? See attached image for clarification.
Note: image currently under Mathworks review. Will be uploaded shortly.
3 Comments
Image Analyst
on 21 Apr 2013
Activate your GUI. Type alt-printscreen to copy it into the clipboard. Go to http://snag.gy and type control-V. Then tell us the URL it gives you.
Bruno
on 21 Apr 2013
Edited: Image Analyst
on 22 Apr 2013
Answers (2)
Walter Roberson
on 21 Apr 2013
0 votes
There is no way to do that, unless it is at the Java level.
What I used to do is calculate the required maximum width, based on the menu contents, and size according to that. That left blank space if the currently chosen menu item was not the widest item, but that is not necessarily a bad thing as users get disconcerted if the controls resize every time you choose a new item. For example, should items (such as other controls) further to the right be shifted left when the user selects a shorter menu item ?
7 Comments
Bruno
on 21 Apr 2013
Walter Roberson
on 22 Apr 2013
None of the uicontrol() autoresize: they take the position/size information you give them and do the best they can. Which is really all they should do, as they could mess up GUI designs a fair bit if they automatically resized.
Imagine, for example, that you have a uipanel that has had two uicontrol listbox placed side by side, and the uipanel not much wider than the two controls have been sized at. Now set the left control to have a long string in one of the entries. If the left uicontrol resizes it would have to overlap (potentially totally overlaying) the right control, unless the right control was also automatically moved right to make room for the auto-resized left control -- potentially pushing the right control out of the panel completely.
Remember, if an entry is too wide to fit the current control size, you have the option of setting the fontsize smaller so that everything fits.
Behavior that resizes controls needs to be handled within the context of GUI-designer-specified behavior for groups of items. For example if a control is going to be more than a certain width, the designer might want a text area beside it to instead be placed underneath the control. It needs a set of layout rules in order to be done automatically -- and MATLAB does not offer that sort of layout rule processing. uicontrols are the primitive elements that a programmer could choose to implement layout rules around.
I wrote my own resizing routines that were able to probe current contents of a uicontrol and figure out the required size and set the Position to just hold that information -- but I also built layout rule information into my resize routines. Unfortunately the code did not get released before the project was killed, but I may re-express it some day.
Bruno
on 22 Apr 2013
Walter Roberson
on 22 Apr 2013
Edited: Walter Roberson
on 22 Apr 2013
Ah, I realize now you are not talking about uicontrol(). Could you show a sample of how you are creating your menu ?
(incidentally, uicontrol popup does automatically use the necessary width.)
Bruno
on 22 Apr 2013
Bruno
on 22 Apr 2013
Matlab R2011b doesn't seem to differentiate between uicontrol Style 'popup' and 'popupmenu'. Also, I don't seem to be able to set a preference via the 'setConstrainPopupWidth' method of the underlying com.mathworks.hg.peer.ComboboxPeer$MLComboBox object. It defaults to 'off'; but with any attempt to change it, it remains set as 'off'. Though it looks like an unconstrained width used to be the 'default' feature (see Yair's book - Undocumented Secrets of MATLAB - Java Programming) But just as was the case for Bruno, the width of any dropdown menus in Matlab GUIs appear to me to be fixed to their designated width (as set by the popup uicontrol). Don't know if this is a result of the underlying class changing from a MJComboBox to MLComboBox? Seems like this should be a fairly straight-forward attribute to set though... Anybody know if this is definitely a version issue, or have any further ideas for a work-around?
Jan
on 22 Apr 2013
0 votes
I have emulated a drop-down menu with a very small button and a UICONTEXTMENU, which is made visible in the button's callback. This works fluently, but the code is much less attractive than a uicontrol('Style', 'popup').
Categories
Find more on App Building 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!