Drop Down Menu Width

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

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
Bruno on 21 Apr 2013
Edited: Image Analyst on 22 Apr 2013

Sign in to comment.

Answers (2)

Walter Roberson
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
Bruno on 21 Apr 2013
I don't want the menu width to change everytime the user hovers over a different item, I just want it to change to a width that allows display of the entire option. The menu options change as the user uses the program, so I can set a fixed width right from the beginning the is long enough.
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
Bruno on 22 Apr 2013
Walter, I'm sorry, you're still clearly misunderstanding my desire if you think this is a bad idea, so let me rephrase.
I'd like a dropdown menu that functions essentially as the menu toolbar on any program. You click "File" and options drop down. The width of the box containing these options is NOT the same width as the word "File" on the menu toolbar otherwise you wouldn't be able to make out what all the options are. The dropdown portion of the menu automatically sizes itself to accommodate the entire text of the each selectable item. At the same time this does NOT resize the width taken up by "File" on the menu toolbar because as you justly point out that could wreak havoc on the UI.
This is the type of autoresizing I'm wishing to implement. To resize just the drop down portion, i.e. that which is outlined in red on the image I uploaded, and NOT the GUI item itself, i.e. that which is outlined in blue on the image I uploaded.
Moving the drop down menu to the toolbar to gain this type of functionality is not an option.
Thank you, B
Walter Roberson
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
Bruno on 22 Apr 2013
The menu is a Pop-Up Menu, I guess I've been calling it a drop down. Sorry about that. The entire GUI is created in GUIDE. The pop-up menu has a fixed width of 25 characters, but some of the options are too wide and the dropdown (I guess pop-up) portion does not autoresize. Though from your response it seems as if it should be. Is there a bit I have to flip in the items properties list?
Bruno
Bruno on 22 Apr 2013
Walter, I created a popup uicontrol item in code and it appears to autosize as you stated it would. Apparently the style of the popups created in GUIDE are "popupmenu" and not "popup". I tried to change the style with set, but it wouldn't allow me which is understandable.
Does anyone know how to change the "popupmenu" created with GUIDE into a "popup"?
Thanks, B
Toby
Toby on 27 Nov 2013
Edited: Toby on 27 Nov 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?

Sign in to comment.

Jan
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

Asked:

on 21 Apr 2013

Edited:

on 27 Nov 2013

Community Treasure Hunt

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

Start Hunting!