Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Customizing combobox popups

September 24, 2014 8 Comments

Last week I explained how we can use display custom items in a standard Matlab combobox (popup/dropdown), using its underlying Java component. Today I will show how we can use this Java component for other nice customizations of the combobox’s popup:

  • Getting the underlying Java component
  • MaximumRowCount
  • PopupVisible
  • PopupWidthConstrained

Underlying Java component

The first step is to find the underlying Java component of the Matlab combobox (aka popup) uicontrol. This is done using my findjobj utility:

% Create the Matlab combobox
items = {'<html><font color="red">Hello</font></html>', 'world', ...
         '<html><font style="font-family:impact;color:green"><i>What a', ...
         '<html><font color="blue" face="Comic Sans MS">nice day!</font>'};
hCombobox = uicontrol('Style','popup', 'Position',[10,100,120,20], 'String',items);
% Find the uicontrol's underlying Java component
jCombobox = findjobj(hCombobox);

% Create the Matlab combobox items = {'<html><font color="red">Hello</font></html>', 'world', ... '<html><font style="font-family:impact;color:green"><i>What a', ... '<html><font color="blue" face="Comic Sans MS">nice day!</font>'}; hCombobox = uicontrol('Style','popup', 'Position',[10,100,120,20], 'String',items); % Find the uicontrol's underlying Java component jCombobox = findjobj(hCombobox);

For findjobj to work, the Matlab uicontrol needs to be visible – it will not have a Java component before it is rendered onscreen for the first time. If everything is successful, jCombobox should now be a reference to the underlying om.mathworks.hg.peer.ComboboxPeer$MLComboBox Java component, which is an extension of the standard Swing JComboBox, as can be seen using my checkClass utility:

>> jCombobox.checkClass
private com.mathworks.hg.peer.ComboboxPeer$MLComboBox (uiinspect)
Superclass: com.mathworks.mwswing.MJComboBox
Superclass: javax.swing.JComboBox
Methods in JComboBox missing in ComboboxPeer$MLComboBox:
   JComboBox()
   JComboBox(java.lang.Object[])
   JComboBox(java.util.Vector)
   JComboBox(javax.swing.ComboBoxModel)
Methods in ComboboxPeer$MLComboBox missing in JComboBox:
   ComboboxPeer$MLComboBox(com.mathworks.hg.peer.ComboboxPeer)
   isPopupWidthConstrained() : boolean
   isTipWhenTruncatedEnabled() : boolean
   processEvent(java.awt.AWTEvent)
   registerWithKeyBindingManager(com.mathworks.mwswing.binding.KeyBindingManager, java.lang.String)
   setConstrainPopupWidth(boolean)
   setEditorColumnCount(int)
   setTipWhenTruncatedEnabled(boolean)
Methods inherited & modified by ComboboxPeer$MLComboBox:
   getInsets() : java.awt.Insets
   setBackground(java.awt.Color)
   updateUI()
Interfaces in JComboBox missing in ComboboxPeer$MLComboBox:
   java.awt.ItemSelectable
   java.awt.event.ActionListener
   javax.accessibility.Accessible
   javax.swing.event.ListDataListener

>> jCombobox.checkClass private com.mathworks.hg.peer.ComboboxPeer$MLComboBox (uiinspect) Superclass: com.mathworks.mwswing.MJComboBox Superclass: javax.swing.JComboBox Methods in JComboBox missing in ComboboxPeer$MLComboBox: JComboBox() JComboBox(java.lang.Object[]) JComboBox(java.util.Vector) JComboBox(javax.swing.ComboBoxModel) Methods in ComboboxPeer$MLComboBox missing in JComboBox: ComboboxPeer$MLComboBox(com.mathworks.hg.peer.ComboboxPeer) isPopupWidthConstrained() : boolean isTipWhenTruncatedEnabled() : boolean processEvent(java.awt.AWTEvent) registerWithKeyBindingManager(com.mathworks.mwswing.binding.KeyBindingManager, java.lang.String) setConstrainPopupWidth(boolean) setEditorColumnCount(int) setTipWhenTruncatedEnabled(boolean) Methods inherited & modified by ComboboxPeer$MLComboBox: getInsets() : java.awt.Insets setBackground(java.awt.Color) updateUI() Interfaces in JComboBox missing in ComboboxPeer$MLComboBox: java.awt.ItemSelectable java.awt.event.ActionListener javax.accessibility.Accessible javax.swing.event.ListDataListener

We shall now use three properties of this object to customize the control’s popup:

MaximumRowCount

The MaximumRowCount numeric property (default=20) sets the maximal number of drop-down items to display in the visible portion of the popup, before requiring a scrollbar. This basically controls the popup’s height:

% Get the current MaximumRowCount value
numItems = get(jCombobox, 'MaximumRowCount');
numItems = jCombobox.MaximumRowCount;     % equivalent - access the property directly
numItems = jCombobox.getMaximumRowCount;  % equivalent - use Java's accessor method (best way)
% Set the MaximumRowCount value
set(jCombobox,'MaximumRowCount',3);
jCombobox.MaximumRowCount = 3;      % equivalent - access the property directly
jCombobox.setMaximumRowCount(3);    % equivalent - use Java's accessor method (best way)

% Get the current MaximumRowCount value numItems = get(jCombobox, 'MaximumRowCount'); numItems = jCombobox.MaximumRowCount; % equivalent - access the property directly numItems = jCombobox.getMaximumRowCount; % equivalent - use Java's accessor method (best way) % Set the MaximumRowCount value set(jCombobox,'MaximumRowCount',3); jCombobox.MaximumRowCount = 3; % equivalent - access the property directly jCombobox.setMaximumRowCount(3); % equivalent - use Java's accessor method (best way)

MaximumRowCount=20 (default)

MaximumRowCount=20
(default)

MaximumRowCount=3

MaximumRowCount=3
 

MaximumRowCount=2

MaximumRowCount=2
 


Note that MaximumRowCount is a Matlab extension to the standard Swing JComboBox, so if we use a JComboBox directly in our code (using javacomponent) we will not have this feature. Creating the ComboboxPeer$MLComboBox component instead is possible, but is beyond the scope of this article, because MathWorks chose for this class not to have JComboBox‘s standard constructors but rather only a constructor that accepts a com.mathworks.hg.peer.ComboboxPeer object.

PopupVisible

The PopupVisible property (default=false) is a boolean flag which controls whether the popup window is currently (or should be) displayed. If this property is updated, then the focus is automatically transferred to the popup window for easy item selection using the keyboard (up/down/enter keys). There are also equivalent convenience methods showPopup()/hidePopup():

% Is the popup currently shown?
isShown = get(jCombobox, 'PopupVisible');
isShown = jCombobox.PopupVisible;     % equivalent - access the property directly
isShown = jCombobox.isPopupVisible;   % equivalent - use Java's accessor method (best way)
% Display the popup
set(jCombobox,'PopupVisible',true);   % NOT 'on' - this is a Java property, not a Matlab HG one!
jCombobox.PopupVisible = true;        % equivalent - access the property directly
jCombobox.setPopupVisible(true);      % equivalent - use Java's accessor method (best way)
jCombobox.showPopup();                % equivalent - use Java's direct method
% Hide the popup
set(jCombobox,'PopupVisible',false);  % NOT 'off' - this is a Java property, not a Matlab HG one!
jCombobox.PopupVisible = false;       % equivalent - access the property directly
jCombobox.setPopupVisible(false);     % equivalent - use Java's accessor method (best way)
jCombobox.hidePopup();                % equivalent - use Java's direct method

% Is the popup currently shown? isShown = get(jCombobox, 'PopupVisible'); isShown = jCombobox.PopupVisible; % equivalent - access the property directly isShown = jCombobox.isPopupVisible; % equivalent - use Java's accessor method (best way) % Display the popup set(jCombobox,'PopupVisible',true); % NOT 'on' - this is a Java property, not a Matlab HG one! jCombobox.PopupVisible = true; % equivalent - access the property directly jCombobox.setPopupVisible(true); % equivalent - use Java's accessor method (best way) jCombobox.showPopup(); % equivalent - use Java's direct method % Hide the popup set(jCombobox,'PopupVisible',false); % NOT 'off' - this is a Java property, not a Matlab HG one! jCombobox.PopupVisible = false; % equivalent - access the property directly jCombobox.setPopupVisible(false); % equivalent - use Java's accessor method (best way) jCombobox.hidePopup(); % equivalent - use Java's direct method

Note that PopUpVisible is not a Matlab extension – it exists in the original Swing JComboBox. Unfortunately, it was not included in the list of properties that are exposed to the user by the high-level Matlab uicontrol, so we need to use the underlying Java component.
On a Windows platform the PopupVisible property is toggled, thereby showing/hiding the popup window, whenever the user clicks <Alt-Up> or <Alt-Down> when the combo-box has focus.

PopupWidthConstrained

The PopupWidthConstrained property (default=false) is a boolean flag which is another Matlab extension to the standard Swing JComboBox. It is apparently used to constrain the width of the drop-down list to the width of the text field. MathWorks took the trouble to add this feature because Swing JComboBox‘s width is constrained, causing a difficulty in distinguishing between popup values when the control is relatively narrow; Matlab’s MJComboBox‘s default unconstrained behavior is much more user-friendly:

PopupWidthConstrained=false (default)

PopupWidthConstrained=false
(default)

PopupWidthConstrained=true

PopupWidthConstrained=true
 


Note that the PopupWidthConstrained property’s read accessor methods is the expected isPopupWidthConstrained(), thereby also enabling the expected Matlab-standard format of get(‘PopupWidthConstrained’). However, the property update accessor method is not the expected setPopupWidthConstrained(flag) but rather a non-standard setConstrainPopupWidth(flag). For this reason, it is impossible to set this property using set(‘PopupWidthConstrained’,…), but only via the Java setConstrainPopupWidth() accessor method:

>> set(jCombobox,'PopupWidthConstrained',true)
??? Changing the 'PopupWidthConstrained' property of javahandle_withcallbacks.com.mathworks.hg.peer.ComboboxPeer$MLComboBox is not allowed.
 
>> jCombobox.setPopupWidthConstrained(true)
??? No appropriate method or public field setPopupWidthConstrained for class javahandle_withcallbacks.com.mathworks.hg.peer.ComboboxPeer$MLComboBox.
 
>> jCombobox.setConstrainPopupWidth(true)  % this is ok

For additional customizations of Matlab comboboxes, refer to section 6.7 of my Matlab-Java programming book.

R2014b

We’re all eagerly awaiting the much-anticipated R2014b release. As you all know, this is an important release with major functionality and engine improvements. It is therefore not surprising that the release date (which should normally have been September 1) is somewhat delayed. MathWorkers are hard at work fixing problems in the pre-release beta. This delay was actually anticipated, as can be seen from the pre-release expiry date.
We should all be patient and let MathWorks fix these issues without pressure. This release could be a real home-run and MathWorks should do all it can to ensure that it works as transparently and as backward-compatible as possible so that it indeed becomes a home run rather than an outfield foul ball. Let’s not have a repeat of R2010b, which required two separate service-pack updates. I urge MathWorks to take their time – better safe than sorry. I urge everyone else to be patient – it’s worth the wait.
When 14b is finally out, I will be here with a planned series of articles explaining how we can make good use of all its new goodies. Start drooling…
Happy Jewish New Year everybody!

Related posts:

  1. Customizing listbox/combobox items – Matlab listboxes can be customized using custom Java cell-renderers. ...
  2. Customizing listbox & editbox scrollbars – Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....
  3. Customizing figure toolbar background – Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. ...
  4. Customizing menu items part 3 – Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder. ...
  5. Customizing menu items part 2 – Matlab menu items can be customized in a variety of useful ways using their underlying Java object. ...
  6. Customizing editboxes – Matlab's editbox can be customized in many useful manners...
GUI Java uicontrol Undocumented feature
Print Print
« Previous
Next »
8 Responses
  1. Aurélien September 24, 2014 at 06:10 Reply

    About 14b : it is definitely a major update. I already know that a lot of software here will not migrate into 14b. You were talking about 10b , for us the biggest gap was 11b to 12a (or any newer release) because TMW changed the behavior of matrix functions like setdiff, intersect, union, ismember … (fortunately we have the workaround LEGACY flag like -v6 !!)

    I love new features but I hate compatibility issues

    Maybe 14b will be alive in a few days before MATLAB Expo 2014 (2nd October @ Paris…and I will be there even if I am from south of France!!)

  2. oro77 September 25, 2014 at 00:44 Reply

    I agree concerning 14b, I prefer stability, functionability and compabilities support more than new functions.

    @Aurélien> More updates of your blog would be welcomed 🙂

  3. Andras June 1, 2015 at 05:58 Reply

    Dear Yair!

    I’ve run into a problem using your findjobj utility, I am trying to build a GUI with several popup menus. I build a popup menu using hComboBox=uicontrol(‘Style’,’popup’,…), but for some reason the findjobj(hComboBox) returns an empty handle. When I do the same in the command window it works perfectly (also when I call: findjobj(‘class’,’MLComboBox’). Is there a reason why is it acting like that when I call the findjobj in a function?

    And secondly, why is it that when I declare the same Position property to a Matlab popup menu, and an EditBox, the height of the 2 object differ?

    I am using R2014b version.

    Thanks, for your reply!

    • Yair Altman June 1, 2015 at 07:56 Reply

      @Andras – perhaps the control is not yet visible by the time findjobj is called. Try calling drawnow; pause(0.05); and ensure you are not setting Visible=’off’.

      Re the control height, this is how Matlab works internally; the height is automatic and cannot be controlled from Matlab. It is possible (but also not trivial) to do it in Java. You can search for this online.

    • Andras June 2, 2015 at 01:23 Reply

      Thanks, for the fast reply.
      Sadly neither of those work, I still get an empty handle after calling findjobj.

      I dont quite get your second answer, I thought the Position property sets [left bottom width height] the size of the object, but calling a simple JComboBox with javacomponent, or calling an MLComboBox with uicontrol, and then setting their position property to the exact same values, I still get 2 different height.

    • Andras June 2, 2015 at 01:31 Reply

      Alright, now I see, so the basic popup menu uicontrol height is not editable.

      • Yair Altman June 2, 2015 at 01:40

        Correct. Try keeping the uicontrol height at its standard value (20px) – maybe that will help findobj() to find it. You don’t care about the height value anyway because as you have seen it does not really affect the actual displayed control height.

      • Andras June 2, 2015 at 01:46

        I try to build my GUI using normalized units to set the position property (to avoid setting the ResizeFcn). My main concern is that there is always a label, and sometimes an editbox also next to the popup, and I need to keep them in the same line, and height, etc … , for the GUI to look OK.

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
ActiveX (6) AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
  • Nicholas (3 days 19 hours ago): Hi Yair, Thanks for the reply. I am on Windows 10. I also forgot to mention that this all works wonderfully out of the editor. It only fails once compiled. So, yes, I have tried a...
  • Nicholas (3 days 19 hours ago): Hi Yair, Thanks for the reply. I am on Windows 10. I also forgot to mention that this all works wonderfully out of the editor. It only fails once compiled. So, yes, I have tried a...
  • Yair Altman (4 days 2 hours ago): Nicholas – yes, I used it in a compiled Windows app using R2022b (no update). You didn’t specify the Matlab code location that threw the error so I can’t help...
  • Nicholas (4 days 22 hours ago): Hi Yair, Have you attempted your displayWebPage utility (or the LightweightHelpPanel in general) within a compiled application? It appears to fail in apps derived from both R2022b...
  • João Neves (8 days 3 hours ago): I am on matlab 2021a, this still works: url = struct(struct(struct(struct(hF ig).Controller).PlatformHost). CEF).URL; but the html document is empty. Is there still a way to do...
  • Yair Altman (11 days 2 hours ago): Perhaps the class() function could assist you. Or maybe just wrap different access methods in a try-catch so that if one method fails you could access the data using another...
  • Jeroen Boschma (11 days 4 hours ago): Never mind, the new UI components have an HTML panel available. Works for me…
  • Alexandre (11 days 5 hours ago): Hi, Is there a way to test if data dictionnatry entry are signal, simulink parameters, variables … I need to access their value, but the access method depends on the data...
  • Nicholas (11 days 19 hours ago): In case anyone is looking for more info on the toolbar: I ran into some problems creating a toolbar with the lightweight panel. Previously, the Browser Panel had an addToolbar...
  • Jeroen Boschma (15 days 2 hours ago): I do not seem to get the scrollbars (horizontal…) working in Matlab 2020b. Snippets of init-code (all based on Yair’s snippets on this site) handles.text_explorer...
  • Yair Altman (43 days 5 hours ago): m_map is a mapping tool, not even created by MathWorks and not part of the basic Matlab system. I have no idea why you think that the customizations to the builtin bar function...
  • chengji chen (43 days 11 hours ago): Hi, I have tried the method, but it didn’t work. I plot figure by m_map toolbox, the xticklabel will add to the yticklabel at the left-down corner, so I want to move down...
  • Yair Altman (51 days 4 hours ago): @Alexander – this is correct. Matlab stopped including sqlite4java in R2021b (it was still included in 21a). You can download the open-source sqlite4java project from...
  • Alexander Eder (57 days 0 hours ago): Unfortunately Matlab stopped shipping sqlite4java starting with R2021(b?)
  • K (63 days 10 hours ago): Is there a way to programmatically manage which figure gets placed where? Let’s say I have 5 figures docked, and I split it into 2 x 1, I want to place 3 specific figures on the...
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top