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:
Java Component Focus Question.

Subject: Java Component Focus Question.

From: Jerry Muir

Date: 3 Dec, 2007 18:00:14

Message: 1 of 4

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.

Example:

function Focus_Issue()
 
screenSize = get(0,'ScreenSize');
gui = figure();
hiight = 200;
width = 300;
set(gui, 'Position', [screenSize(3)/2 screenSize(4)/2 width
hiight] );
set(gui, 'Tag', 'main_gui' );
set(gui, 'MenuBar', 'none' );
set(gui, 'NumberTitle','off');
set(gui, 'Name', 'Graphical User Interface' );
 
lowPos = [50 35 200 20];
middlePos = [50 80 200 20];
highPos = [50 125 200 20];
 
pwField1 = javax.swing.JPasswordField(20);
pwField2 = javax.swing.JPasswordField(20);
pwField3 = javax.swing.JPasswordField(20);
javacomponent(pwField1, lowPos);
javacomponent(pwField2, middlePos);
javacomponent(pwField3, highPos);
 
%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.


Any suggestions would be appreciated.

Subject: Java Component Focus Question.

From: Yair Altman

Date: 3 Dec, 2007 20:17:15

Message: 2 of 4

"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.

...works for me on ML 7.4 (R2007a)...

Try the following variation:

[hj,hc]=javacomponent(pwField1,lowPos);
hj.requestFocus;

Yair Altman
http://ymasoftware.com

Subject: Java Component Focus Question.

From: Jerry Muir

Date: 4 Dec, 2007 23:38:26

Message: 3 of 4

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?

Subject: Java Component Focus Question.

From: Yair Altman

Date: 4 Jan, 2008 13:31:22

Message: 4 of 4

"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.

Yair Altman

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