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:
ButtonDownFcn help

Subject: ButtonDownFcn help

From: Matt Fig

Date: 29 Oct, 2007 17:27:16

Message: 1 of 11

Hello,

What exactly does this property do? I want to make an edit
box that has a default string in it when it first shows up.
 When the user clicks inside the edit box, I want the
default string to disappear, which I thought I could use the
ButtonDownFcn to accomplish. However, I cannot seem to make
this ButtonDownFcn do anything at all. What am I not
understanding. Here is one example.


edt1 = uicontrol('style','edit','position',[30 30 90 20],...
                 'string','String','ButtonDownFcn',...
                 'set(edt1,''string'','' '')');

I also tried a function name, like @clc for the
ButtonDownFcn, but that didn't work either.

Please tell me the correct way to use this ButtonDownFcn. I
know there may be another way to do what I want to do, but I
also want to understand what this ButtonDownFcn does! Thanks.

Subject: ButtonDownFcn help

From: Matt Fig

Date: 29 Oct, 2007 17:33:07

Message: 2 of 11

I just tried the right click on the mouse and it worked!
The left click is the one I want to have perform the same
function. Thanks.

Subject: ButtonDownFcn help

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 29 Oct, 2007 17:51:25

Message: 3 of 11

In article <fg555k$j2e$1@fred.mathworks.com>,
Matt Fig <spamanon@yahoo.com> wrote:

>edt1 = uicontrol('style','edit','position',[30 30 90 20],...
> 'string','String','ButtonDownFcn',...
> 'set(edt1,''string'','' '')');

edt1 is not defined at the time the uicontrol() is executed,
so your ButtonDownFcn cannot "capture" the value of edt1 to
refer to it. Instead you are using the string form of callbacks,
which I believe I have read is executed as if typed at the
command line (implying that it would only have access to the
global workspace.)

See if this works:

edt1 = uicontrol('style','edit','position',[30 30 90 20],...
                 'string','String', ...
                 'ButtonDownFcn', @(src,evt) set(src,'String', '') );
--
  "There are some ideas so wrong that only a very intelligent person
  could believe in them." -- George Orwell

Subject: ButtonDownFcn help

From: PB

Date: 29 Oct, 2007 17:53:14

Message: 4 of 11

Den Mon, 29 Oct 2007 17:27:16 +0000 skrev Matt Fig:

> Hello,
>
> What exactly does this property do? I want to make an edit box that has
> a default string in it when it first shows up.
> When the user clicks inside the edit box, I want the
> default string to disappear, which I thought I could use the
> ButtonDownFcn to accomplish. However, I cannot seem to make this
> ButtonDownFcn do anything at all. What am I not understanding. Here is
> one example.
>
>
> edt1 = uicontrol('style','edit','position',[30 30 90 20],...
> 'string','String','ButtonDownFcn',...
> 'set(edt1,''string'','' '')');
>
> I also tried a function name, like @clc for the ButtonDownFcn, but that
> didn't work either.
>
> Please tell me the correct way to use this ButtonDownFcn. I know there
> may be another way to do what I want to do, but I also want to
> understand what this ButtonDownFcn does! Thanks.

Matt, you are doing it right. It just doesn't behave the way you expect
it to. To execute the ButtonDownFcn, either click in a five-pixel border
around the uicontrol or right-click inside the control itself. ( I can't
find a doc-reference but I know this is the way it works.)

HTH

PB

Subject: ButtonDownFcn help

From: Matt Fig

Date: 29 Oct, 2007 17:55:24

Message: 5 of 11

OOps, upon a more careful reading of the properties,
ButtonDownFcn does what it is supposed to do. I would still
like to either have a left click clear the string or
initialize the string in a highlighted state so that once
the user starts typing the default string disappears and the
new one replaces it, if anyone knows how to do this.

Subject: ButtonDownFcn help

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 29 Oct, 2007 18:53:47

Message: 6 of 11

In article <fg56qc$f26$1@fred.mathworks.com>,
Matt Fig <spamanon@yahoo.com> wrote:
>OOps, upon a more careful reading of the properties,
>ButtonDownFcn does what it is supposed to do. I would still
>like to either have a left click clear the string or
>initialize the string in a highlighted state so that once
>the user starts typing the default string disappears and the
>new one replaces it, if anyone knows how to do this.

You will have to modify the Java properties for this.

--
  "Is there any thing whereof it may be said, See, this is new? It hath
  been already of old time, which was before us." -- Ecclesiastes

Subject: ButtonDownFcn help

From: Yair Altman

Date: 29 Oct, 2007 22:15:07

Message: 7 of 11

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fg5a7r$4he$1@canopus.cc.umanitoba.ca>...
> In article <fg56qc$f26$1@fred.mathworks.com>,
> Matt Fig <spamanon@yahoo.com> wrote:
> >OOps, upon a more careful reading of the properties,
> >ButtonDownFcn does what it is supposed to do. I would still
> >like to either have a left click clear the string or
> >initialize the string in a highlighted state so that once
> >the user starts typing the default string disappears and the
> >new one replaces it, if anyone knows how to do this.
>
> You will have to modify the Java properties for this.


...and here's how (note: undocumented/unsupported by MathWorks):

1. download my FindJObj from the File Exchange:
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=14317

2. run the following:
edt1=uicontrol('style','edit',...); %no need to set
ButtonDownFcn
jedt1=findjobj(edt1,'nomenu'); %get underlying java object
cbStr='set(gcbo,''text'','''');set(gcbo,''MouseClickedCallback'',[])';
set(jedt1,'MouseClickedCallback',cbStr);

Yair Altman
http://ymasoftware.com

Subject: ButtonDownFcn help

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 29 Oct, 2007 22:26:50

Message: 8 of 11

In article <fg5m1b$a7g$1@fred.mathworks.com>,
Yair Altman <altmanyDEL@gmailDEL.comDEL> wrote:
>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
>message <fg5a7r$4he$1@canopus.cc.umanitoba.ca>...
>> In article <fg56qc$f26$1@fred.mathworks.com>,
>> Matt Fig <spamanon@yahoo.com> wrote:
>> >I would still
>> >like to either have a left click clear the string or
>> >initialize the string in a highlighted state so that once
>> >the user starts typing the default string disappears and the
>> >new one replaces it, if anyone knows how to do this.

>...and here's how (note: undocumented/unsupported by MathWorks):

>1. download my FindJObj from the File Exchange:
>http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=14317

>2. run the following:
>edt1=uicontrol('style','edit',...); %no need to set
>ButtonDownFcn
>jedt1=findjobj(edt1,'nomenu'); %get underlying java object
>cbStr='set(gcbo,''text'','''');set(gcbo,''MouseClickedCallback'',[])';
>set(jedt1,'MouseClickedCallback',cbStr);

Hmmm, that appears to me to clear the mouse click callback after
clearing the edit field. If so, then the callback would somehow
have to be reprimed after the field lost focus. Matt wasn't clear
on what he would like to have happen after the first time; my own
interpretation is that he wanted mouse left-click to clear the
string each time. Which leads to difficulty if the user wants to
use the mouse to reposition within the string for editting purposes.

Matt? What do you expect after the first firing?
--
   "Beware of bugs in the above code; I have only proved it correct,
   not tried it." -- Donald Knuth

Subject: ButtonDownFcn help

From: Yair Altman

Date: 29 Oct, 2007 23:45:57

Message: 9 of 11

> Hmmm, that appears to me to clear the mouse click callback
> after clearing the edit field.

exactly - on purpose

> If so, then the callback would somehow
> have to be reprimed after the field lost focus.

This can easily be done by setting the MouseClickedCallback
in FocusLostCallback (I forget the exact name but it should
be pretty close), and clearing it in FocusGainedCallback. Or
some similar variation, you get the drift.

> Matt wasn't clear on what he would like to have happen
> after the first time; my own interpretation is that he
> wanted mouse left-click to clear the string each time.
> Which leads to difficulty if the user wants to use the
> mouse to reposition within the string for editting purposes.
>
> Matt? What do you expect after the first firing?

In most non-Matlab implementations that I've seen, following
the first clearing the data was considered "tainted" and
never cleared again, even after losing focus. Of course,
this is a matter of usability and requirements, and these
are just my 2 cents. To clear the data each time, one can
use the Focus callback as stated above.

The above Java callbacks, along with some ~30 other standard
uicontrol callbacks are so powerful: The above example could
never be done with the standard Matlab callbacks, and yet is
so easy to achieve using the Java ones. It's such a pity
that MathWorks does not expose them in the regular Matlab
(HG) interface. Maybe someone will pick up the ball for
R2008a... Bill? anyone?

Yair Altman

Subject: ButtonDownFcn help

From: Matt Fig

Date: 30 Oct, 2007 01:10:31

Message: 10 of 11

The action I wish for is this: the first mouse click clears
the form of it's default string, any following mouse clicks
place the cursor where the pointer points.
I would also settle for the edit box to come up with the
default string highlighted so that as soon as the user
started typing, it would be overwritten anyway. That way
the user wouldn't have to click in the first place and I
wouldn't have to program in all the buttondownfcn
manipulations! Thanks guys.

Subject: ButtonDownFcn help

From: Jeremy

Date: 16 Feb, 2011 23:15:05

Message: 11 of 11

Hey Matt - Did you ever find a good solution to this problem? This is exactly what I'm trying to achieve now. Thanks!


"Matt Fig" wrote in message <fg60a7$mr9$1@fred.mathworks.com>...
> The action I wish for is this: the first mouse click clears
> the form of it's default string, any following mouse clicks
> place the cursor where the pointer points.
> I would also settle for the edit box to come up with the
> default string highlighted so that as soon as the user
> started typing, it would be overwritten anyway. That way
> the user wouldn't have to click in the first place and I
> wouldn't have to program in all the buttondownfcn
> manipulations! Thanks guys.

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