Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Popup menu in the Toolbar?

Subject: Popup menu in the Toolbar?

From: jason

Date: 10 Dec, 2007 19:18:39

Message: 1 of 6

Does anyone know how to put a popup menu in a gui toolbar,
similar to the "Current Directory" popup menu on the
Matlab Desktop? currently I can only find how to add
pushbuttons and toggle buttons to it.

Thanks for any help
Jason

Subject: Popup menu in the Toolbar?

From: Matt Fig

Date: 10 Dec, 2007 20:21:12

Message: 2 of 6

"jason " <mcmains.1@osu.edu> wrote in message
<fjk3ef$sdg$1@fred.mathworks.com>...
> Does anyone know how to put a popup menu in a gui toolbar,
> similar to the "Current Directory" popup menu on the
> Matlab Desktop? currently I can only find how to add
> pushbuttons and toggle buttons to it.
>
> Thanks for any help
> Jason


I don't see in the documentation that this can be done
directly. However, you could do something quick and dirty
like this (watch for wrapping):

fg = figure('units','pixels','position',[400 500 300 300]);
posfg = get(gcf,'position');
posfrm = [1 (posfg(4)-20) posfg(3)-1 20];
fr =
uicontrol('style','frame','units','pixels','position',posfrm);
pum = uicontrol('style','popupmenu','position',posfrm-[-1 -1
200 0],...
                'string',{'here','there','everywhere'});

Subject: Popup menu in the Toolbar?

From: Yair Altman

Date: 10 Dec, 2007 22:30:34

Message: 3 of 6

"jason " <mcmains.1@osu.edu> wrote in message
<fjk3ef$sdg$1@fred.mathworks.com>...
> Does anyone know how to put a popup menu in a gui toolbar,
> similar to the "Current Directory" popup menu on the
> Matlab Desktop? currently I can only find how to add
> pushbuttons and toggle buttons to it.
>
> Thanks for any help
> Jason


As Matt said, modifying the toolbar is undocumented and
unsupported. You can easily add pushbuttons and
toggle-buttons using the following syntax:

htb = findall(gcf,'tag','FigureToolBar');
if ~isempty(htb)
   
uitoggletool('tooltip','toggle','seperator','on','parent',htb,'cdata',iconData);
    uipushtool(...); % similarly
end

However, trying the same with a uicontrol or javacomponent
is blocked. So, the workaround is to directly add a Java
dropdown
(javax.swing.JComboBox({'here','there','everywhere'})
object) to the underlying Java implementation of the
toolbar. This is slightly more difficult and requires some
Java knowledge. Use my FindJObj submission on the File
Exchange to get started:

jtb = findjobj(gcf,'-nomenu','class','mjtoolbar');
if ~isempty(jtb)
    jtb(1).addSeparator;
    jcb=javax.swing.JComboBox({'here','there','everywhere'}));
    jtb(1).add(jcb);
    jtb(1).repaint;
    jtb(1).revalidate;
end

Of course, in all events you need to set the action
callback: For pushbuttons/toggle-buttons this is via their
ClickedCallback property; for JComboBox this is via its
ActionPerformedCallback property.

Yair Altman
http://ymasoftware.com

Subject: Popup menu in the Toolbar?

From: Yair Altman

Date: 18 Dec, 2007 22:35:24

Message: 4 of 6

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message
<fjkema$16s$1@fred.mathworks.com>...
> "jason " <mcmains.1@osu.edu> wrote in message
> <fjk3ef$sdg$1@fred.mathworks.com>...
> > Does anyone know how to put a popup menu in a gui toolbar,
> > similar to the "Current Directory" popup menu on the
> > Matlab Desktop? currently I can only find how to add
> > pushbuttons and toggle buttons to it.
> >
> > Thanks for any help
> > Jason
>
>
> As Matt said, modifying the toolbar is undocumented and
> unsupported. You can easily add pushbuttons and
> toggle-buttons using the following syntax:
>
> htb = findall(gcf,'tag','FigureToolBar');
> if ~isempty(htb)
>
>
uitoggletool('tooltip','toggle','seperator','on','parent',htb,'cdata',iconData);
> uipushtool(...); % similarly
> end
>
> However, trying the same with a uicontrol or javacomponent
> is blocked. So, the workaround is to directly add a Java
> dropdown
> (javax.swing.JComboBox({'here','there','everywhere'})
> object) to the underlying Java implementation of the
> toolbar. This is slightly more difficult and requires some
> Java knowledge. Use my FindJObj submission on the File
> Exchange to get started:
>
> jtb = findjobj(gcf,'-nomenu','class','mjtoolbar');
> if ~isempty(jtb)
> jtb(1).addSeparator;
> jcb=javax.swing.JComboBox({'here','there','everywhere'}));
> jtb(1).add(jcb);
> jtb(1).repaint;
> jtb(1).revalidate;
> end
>
> Of course, in all events you need to set the action
> callback: For pushbuttons/toggle-buttons this is via their
> ClickedCallback property; for JComboBox this is via its
> ActionPerformedCallback property.
>
> Yair Altman
> http://ymasoftware.com


I just found out that you don't need to use findjobj to find
the underlying Java toolbar jtb:

jtb = get(get(htb,'JavaContainer'),'ComponentPeer');

Yair

Subject: Popup menu in the Toolbar?

From: jason

Date: 25 Jul, 2008 19:23:02

Message: 5 of 6

Your solution worked great Yair, its a little more bulky
to program than I'm used to, but it makes the figure looks
exactly how I wanted it. Thanks!

Jason

Subject: Popup menu in the Toolbar?

From: NK University

Date: 15 Dec, 2011 03:04:08

Message: 6 of 6

It is a very quick way to creat popup menu using this java method, while how to control the size of the obj? e.g. the size? Thank you!


"Yair Altman" wrote in message <fjkema$16s$1@fred.mathworks.com>...
> "jason " <mcmains.1@osu.edu> wrote in message
> <fjk3ef$sdg$1@fred.mathworks.com>...
> > Does anyone know how to put a popup menu in a gui toolbar,
> > similar to the "Current Directory" popup menu on the
> > Matlab Desktop? currently I can only find how to add
> > pushbuttons and toggle buttons to it.
> >
> > Thanks for any help
> > Jason
>
>
> As Matt said, modifying the toolbar is undocumented and
> unsupported. You can easily add pushbuttons and
> toggle-buttons using the following syntax:
>
> htb = findall(gcf,'tag','FigureToolBar');
> if ~isempty(htb)
>
> uitoggletool('tooltip','toggle','seperator','on','parent',htb,'cdata',iconData);
> uipushtool(...); % similarly
> end
>
> However, trying the same with a uicontrol or javacomponent
> is blocked. So, the workaround is to directly add a Java
> dropdown
> (javax.swing.JComboBox({'here','there','everywhere'})
> object) to the underlying Java implementation of the
> toolbar. This is slightly more difficult and requires some
> Java knowledge. Use my FindJObj submission on the File
> Exchange to get started:
>
> jtb = findjobj(gcf,'-nomenu','class','mjtoolbar');
> if ~isempty(jtb)
> jtb(1).addSeparator;
> jcb=javax.swing.JComboBox({'here','there','everywhere'}));
> jtb(1).add(jcb);
> jtb(1).repaint;
> jtb(1).revalidate;
> end
>
> Of course, in all events you need to set the action
> callback: For pushbuttons/toggle-buttons this is via their
> ClickedCallback property; for JComboBox this is via its
> ActionPerformedCallback property.
>
> Yair Altman
> http://ymasoftware.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us