I have been searching for some time for a solution to focus
issues when using java components in a matlab GUI.
I can't seam to find a way to programmatically set the focus
on a given field. See below example.
%pwField1.requestFocus
% Does nothing, returns false.
%uicontrol(pwField1);
% returns following error:
% ??? Error using ==> uicontrol
% Arguments may only include figure, uicontainer, or
uicontrol handle and param/value pairs.
"Jerry Muir" <gerard_muir.nospam@lord.com> wrote in message
<fj1g7e$iq9$1@fred.mathworks.com>...
> Hello all,
>
> I have been searching for some time for a solution to focus
> issues when using java components in a matlab GUI.
> I can't seam to find a way to programmatically set the focus
> on a given field. See below example.
[snip]
> %pwField1.requestFocus
> % Does nothing, returns false.
Thanks for looking in to my question. I found the cause to
be a race condition. I looks like I am trying to call
methods on the java object before the class loader has
finished creating it. If I added a pause to my code as shown
below it works fine.
"Jerry Muir" <gerard_muir.nospam@lord.com> wrote in message
<fj4odi$7ke$1@fred.mathworks.com>...
> Hi Yair,
>
> Thanks for looking in to my question. I found the cause to
> be a race condition. I looks like I am trying to call
> methods on the java object before the class loader has
> finished creating it. If I added a pause to my code as
> shown below it works fine.
>
> ....
> javacomponent(pwField1,lowPos);
> javacomponent(pwField2, middlePos);
> javacomponent(pwField3, highPos);
>
> pause(0.1); % Insure the object has time to be created.
> pwField3.requestFocus;
>
> So this brings up a second question, Is there a way to
> wait on the class loader to finish before continuing the
> program execution?
Sorry for the belated response: The "semi-official" way to
solve this is to use awtinvoke() for all Java GUI
invocations, and awtcreate() for their creation. This places
the method invoked on the Java event thread queue, ensuring
it's executed sequentially. The syntax pwField3.requestFocus
apparently runs on the main Matlab thread, and so is not
guarantied to occur after the Java components finish loading
& rendering. However, awtinvoke is not trivial to use, and
also has several limitations. Note that while MathWorks
advises Java users to use awtinvoke & awtcreate instead of
the direct-invocation syntax for the above reason, they have
still not made these functions officially supported.
In practice, the pause() method, while ugly and unsafe, is
in fact the easiest and fastest solution to program. Call me
a bad programmer, but I rarely use awtcreate/awtinvoke
nowadays. Normally, only a few milliseconds pause is needed:
pause(0.005); heavy Java components (like a JTable full of
data etc.) may need more. Use try-catch to solve these cases.
Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central. Read the complete Disclaimer prior to use.