<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>
	Comments on: Fixing a Java focus problem	</title>
	<atom:link href="https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fixing-a-java-focus-problem</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Fri, 10 Aug 2018 14:34:02 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.3</generator>
	<item>
		<title>
		By: Joe Burgel		</title>
		<link>https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-434988</link>

		<dc:creator><![CDATA[Joe Burgel]]></dc:creator>
		<pubDate>Fri, 10 Aug 2018 14:34:02 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1933#comment-434988</guid>

					<description><![CDATA[Hi Yair, Once again, saving my life! Thanks so much for what you do for all these years.]]></description>
			<content:encoded><![CDATA[<p>Hi Yair, Once again, saving my life! Thanks so much for what you do for all these years.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Temu		</title>
		<link>https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-416178</link>

		<dc:creator><![CDATA[Temu]]></dc:creator>
		<pubDate>Wed, 08 Nov 2017 13:40:05 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1933#comment-416178</guid>

					<description><![CDATA[Hi Yair,

Thanks for sharing all these insights!  These are really helpful.

I am currently trying to implement a focus-follows-mouse mechanism in matlab, since versions from (at least) 2015a onwards no longer support this behaviour.  I have sommething that sometimes works, but not always, and I am at a loss here.  I am hoping you have some ideas.  I have tried the following:
&lt;pre lang=&quot;matlab&quot;&gt;
function focus()
    mde = com.mathworks.mde.desk.MLDesktop.getInstance;
    cw = mde.getClient(&#039;Command Window&#039;);
    xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
    h_cw = handle(xCmdWndView,&#039;CallbackProperties&#039;);

    function callMe(varargin)
        desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
        desktop.getMainFrame.setFocusable(true);       % This one doesn&#039;t always work
        desktop.getMainFrame.requestFocusInWindow();   % This one doesn&#039;t always work
    end

    set(h_cw, &#039;MouseEnteredCallback&#039;, @callMe);
end

&lt;/pre&gt;

This often works, but when I, e.g., click the windows desktop first (at an empty space), only the icon in my taskbar becomes blue, but the matlab desktop does not focus.  When I come from a different window, it seems to work most of the times.

I hope you can help me with this,

Temu]]></description>
			<content:encoded><![CDATA[<p>Hi Yair,</p>
<p>Thanks for sharing all these insights!  These are really helpful.</p>
<p>I am currently trying to implement a focus-follows-mouse mechanism in matlab, since versions from (at least) 2015a onwards no longer support this behaviour.  I have sommething that sometimes works, but not always, and I am at a loss here.  I am hoping you have some ideas.  I have tried the following:</p>
<pre lang="matlab">
function focus()
    mde = com.mathworks.mde.desk.MLDesktop.getInstance;
    cw = mde.getClient('Command Window');
    xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
    h_cw = handle(xCmdWndView,'CallbackProperties');

    function callMe(varargin)
        desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
        desktop.getMainFrame.setFocusable(true);       % This one doesn't always work
        desktop.getMainFrame.requestFocusInWindow();   % This one doesn't always work
    end

    set(h_cw, 'MouseEnteredCallback', @callMe);
end

</pre>
<p>This often works, but when I, e.g., click the windows desktop first (at an empty space), only the icon in my taskbar becomes blue, but the matlab desktop does not focus.  When I come from a different window, it seems to work most of the times.</p>
<p>I hope you can help me with this,</p>
<p>Temu</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-383266</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sun, 17 Jul 2016 09:25:33 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1933#comment-383266</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-382353&quot;&gt;Dave Bryers&lt;/a&gt;.

For an editable combobox, you need to set the focusing customizations on the internal textfield sub-component rather than on the parent JComboBox. i.e., instead of:
&lt;pre lang=&quot;matlab&quot;&gt;
h3.putClientProperty(&#039;TabCycleParticipant&#039;, true);
h3.setFocusable(true)
&lt;/pre&gt;
do this:
&lt;pre lang=&quot;matlab&quot;&gt;
jEditor = h3.getEditor.getEditorComponent;  % or: h3.getComponent(2)
jEditor.putClientProperty(&#039;TabCycleParticipant&#039;, true);
jEditor.setFocusable(true)
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-382353">Dave Bryers</a>.</p>
<p>For an editable combobox, you need to set the focusing customizations on the internal textfield sub-component rather than on the parent JComboBox. i.e., instead of:</p>
<pre lang="matlab">
h3.putClientProperty('TabCycleParticipant', true);
h3.setFocusable(true)
</pre>
<p>do this:</p>
<pre lang="matlab">
jEditor = h3.getEditor.getEditorComponent;  % or: h3.getComponent(2)
jEditor.putClientProperty('TabCycleParticipant', true);
jEditor.setFocusable(true)
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Dave Bryers		</title>
		<link>https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-382353</link>

		<dc:creator><![CDATA[Dave Bryers]]></dc:creator>
		<pubDate>Tue, 05 Jul 2016 15:48:44 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1933#comment-382353</guid>

					<description><![CDATA[This all works for me except when I make a JComboBox editable.  At this point the tab traversal link seems to be broken, whilst you can tab to the combo box, tabbing out takes you back to the start.
&lt;pre lang=&quot;matlab&quot;&gt;
% Open figure window
hFig = figure;
% Create two stadard text boxes
h1 = uicontrol(hFig,&#039;style&#039;,&#039;edit&#039;,&#039;position&#039;,  [10 150 150 20],&#039;String&#039;,&#039;One&#039;);
h2 = uicontrol(hFig,&#039;style&#039;,&#039;edit&#039;,&#039;position&#039;,  [10 120 150 20],&#039;String&#039;,&#039;Two&#039;);
% Create an editable combo box
model = javax.swing.DefaultComboBoxModel({&#039;a&#039;,&#039;b&#039;,&#039;c&#039;});
h3 = javacomponent({&#039;javax.swing.JComboBox&#039;,model}, [10 90 150 20],hFig);
% Get it to tab here
h3.setFocusable(true)
h3.putClientProperty(&#039;TabCycleParticipant&#039;, true);
% Turn it into an editable box
h3.setEditable(true);
% Add two more standard text boxes
h4 = uicontrol(hFig,&#039;style&#039;,&#039;edit&#039;,&#039;position&#039;,  [10 60 150 20],&#039;String&#039;,&#039;Four&#039;);
h5 = uicontrol(hFig,&#039;style&#039;,&#039;edit&#039;,&#039;position&#039;,  [10 30 150 20],&#039;String&#039;,&#039;Five&#039;);
&lt;/pre&gt;
It is only with the &lt;code&gt;h3.setEditable(true)&lt;/code&gt; that the tab order is disturbed.
Anyone have any thoughts?]]></description>
			<content:encoded><![CDATA[<p>This all works for me except when I make a JComboBox editable.  At this point the tab traversal link seems to be broken, whilst you can tab to the combo box, tabbing out takes you back to the start.</p>
<pre lang="matlab">
% Open figure window
hFig = figure;
% Create two stadard text boxes
h1 = uicontrol(hFig,'style','edit','position',  [10 150 150 20],'String','One');
h2 = uicontrol(hFig,'style','edit','position',  [10 120 150 20],'String','Two');
% Create an editable combo box
model = javax.swing.DefaultComboBoxModel({'a','b','c'});
h3 = javacomponent({'javax.swing.JComboBox',model}, [10 90 150 20],hFig);
% Get it to tab here
h3.setFocusable(true)
h3.putClientProperty('TabCycleParticipant', true);
% Turn it into an editable box
h3.setEditable(true);
% Add two more standard text boxes
h4 = uicontrol(hFig,'style','edit','position',  [10 60 150 20],'String','Four');
h5 = uicontrol(hFig,'style','edit','position',  [10 30 150 20],'String','Five');
</pre>
<p>It is only with the <code>h3.setEditable(true)</code> that the tab order is disturbed.<br />
Anyone have any thoughts?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Sam Roberts		</title>
		<link>https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-367209</link>

		<dc:creator><![CDATA[Sam Roberts]]></dc:creator>
		<pubDate>Mon, 11 Jan 2016 16:25:28 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1933#comment-367209</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-356223&quot;&gt;Dirk Engel&lt;/a&gt;.

Thanks Dirk, I was pulling my hair out trying to figure out why setFocusable wasn&#039;t doing it. FYI, I can confirm that this works in 14b, 15a and 15b.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-356223">Dirk Engel</a>.</p>
<p>Thanks Dirk, I was pulling my hair out trying to figure out why setFocusable wasn&#8217;t doing it. FYI, I can confirm that this works in 14b, 15a and 15b.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-356239</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Mon, 31 Aug 2015 18:29:12 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1933#comment-356239</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-356223&quot;&gt;Dirk Engel&lt;/a&gt;.

@Dirk - thanks. I assume this originated from Matlab&#039;s change of its internal Java version, from 6 to 7.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-356223">Dirk Engel</a>.</p>
<p>@Dirk &#8211; thanks. I assume this originated from Matlab&#8217;s change of its internal Java version, from 6 to 7.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Dirk Engel		</title>
		<link>https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-356223</link>

		<dc:creator><![CDATA[Dirk Engel]]></dc:creator>
		<pubDate>Mon, 31 Aug 2015 09:27:55 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1933#comment-356223</guid>

					<description><![CDATA[I just found an easy way to make it work again in R2015b (probably also works since R2014b). In addition to set components focusable one needs to add a specific client property:

&lt;pre lang=&quot;matlab&quot;&gt;
h3.setFocusable(true);
h3.putClientProperty(&#039;TabCycleParticipant&#039;, true); % only components with this property will be accepted in the focus cycle
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>I just found an easy way to make it work again in R2015b (probably also works since R2014b). In addition to set components focusable one needs to add a specific client property:</p>
<pre lang="matlab">
h3.setFocusable(true);
h3.putClientProperty('TabCycleParticipant', true); % only components with this property will be accepted in the focus cycle
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Richard		</title>
		<link>https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-347123</link>

		<dc:creator><![CDATA[Richard]]></dc:creator>
		<pubDate>Wed, 25 Mar 2015 19:41:11 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1933#comment-347123</guid>

					<description><![CDATA[Running R2015a, I have what may be a related issue, but I&#039;m not sure. Simply put java callbacks appear to be suppressed when changing the stacking order of components in a uipanel.
I have an edit box, created with standard Matlab and adding a java callback for loss of focus:
&lt;pre lang=&quot;matlab&quot;&gt;
function ths = UIedit(oParent,oSkin,sDefault)
    ths.hCtrl = uicontrol(&#039;Style&#039;,&#039;edit&#039;,...
                          &#039;Parent&#039;,handle(oParent),...
                          &#039;String&#039;,ths.sDefault,...
                          &#039;Enable&#039;,&#039;off&#039;,...
                          &#039;ButtonDownFcn&#039;,@(hCtrl,oEventData)buttonDown(ths),...
                          &#039;Callback&#039;,@(hCtrl,oEventData)callback(ths,oEventData),...
                          &#039;KeyPressFcn&#039;,@(hCtrl,oEventData)keyPress(ths,oEventData),...
                          &#039;KeyReleaseFcn&#039;,@(hCtrl,oEventData)keyRelease(ths,oEventData));
    ths.jCtrl = findjobj(ths.hCtrl);
    set(ths.jCtrl,&#039;FocusLostCallback&#039;,@(src,evnt)focusLost(ths,src,evnt));
end % class constructor
&lt;/pre&gt;

If the edit box loses focus, the focusLost method is triggered and stuff happens. And it works just fine. Except...
I also have a listbox that is a regular Matlab uicontrol:
&lt;pre lang=&quot;matlab&quot;&gt;
function ths = UIlistbox(oParent,oSkin)
    ths.hCtrl = uicontrol(&#039;Style&#039;,&#039;listbox&#039;,...
                          &#039;Parent&#039;,handle(oParent),...
                          &#039;Max&#039;,2,...
                          &#039;Visible&#039;,&#039;off&#039;,...
                          &#039;Callback&#039;,@(hCtrl,oEventData)callback(ths,oEventData));
end % class constructor
&lt;/pre&gt;

The listbox is initially hidden because it serves as an auto-completion list for the edit box. You know the concept - you start typing in the edit box and the auto-completion listbox appears with suggestions. And when I press enter, or click on a listbox item, or press escape, stuff happens and the editbox is cleared and the listbox is emptied and re-hidden.
Now, the reason for the java callback is if I click away from the editbox while typing - I don&#039;t want the regular callback to fire and capture what I entered, I just want to clear and reset. As it happens this is not a very good solution at all, but it is illustrative of the problem.

Now, here&#039;s the problem. There are other components that belong to the uipanel parent- 5 children in total, with the order of creation such that the listbox is item #3 in the hPanel.Children array. Visually, there is another uicontrol with position coordinates that are on top of the listbox, such that I have to restack the children, like so:
&lt;pre lang=&quot;matlab&quot;&gt;
function moveToFront(ths)
    hCtrl = handle(ths);
    uistack(hCtrl,&#039;top&#039;);
end
&lt;/pre&gt;

Before restacking the java callback triggers just fine. After restacking, the java callback no longer fires. And I have no idea why. Any idea how to fix this?]]></description>
			<content:encoded><![CDATA[<p>Running R2015a, I have what may be a related issue, but I&#8217;m not sure. Simply put java callbacks appear to be suppressed when changing the stacking order of components in a uipanel.<br />
I have an edit box, created with standard Matlab and adding a java callback for loss of focus:</p>
<pre lang="matlab">
function ths = UIedit(oParent,oSkin,sDefault)
    ths.hCtrl = uicontrol('Style','edit',...
                          'Parent',handle(oParent),...
                          'String',ths.sDefault,...
                          'Enable','off',...
                          'ButtonDownFcn',@(hCtrl,oEventData)buttonDown(ths),...
                          'Callback',@(hCtrl,oEventData)callback(ths,oEventData),...
                          'KeyPressFcn',@(hCtrl,oEventData)keyPress(ths,oEventData),...
                          'KeyReleaseFcn',@(hCtrl,oEventData)keyRelease(ths,oEventData));
    ths.jCtrl = findjobj(ths.hCtrl);
    set(ths.jCtrl,'FocusLostCallback',@(src,evnt)focusLost(ths,src,evnt));
end % class constructor
</pre>
<p>If the edit box loses focus, the focusLost method is triggered and stuff happens. And it works just fine. Except&#8230;<br />
I also have a listbox that is a regular Matlab uicontrol:</p>
<pre lang="matlab">
function ths = UIlistbox(oParent,oSkin)
    ths.hCtrl = uicontrol('Style','listbox',...
                          'Parent',handle(oParent),...
                          'Max',2,...
                          'Visible','off',...
                          'Callback',@(hCtrl,oEventData)callback(ths,oEventData));
end % class constructor
</pre>
<p>The listbox is initially hidden because it serves as an auto-completion list for the edit box. You know the concept &#8211; you start typing in the edit box and the auto-completion listbox appears with suggestions. And when I press enter, or click on a listbox item, or press escape, stuff happens and the editbox is cleared and the listbox is emptied and re-hidden.<br />
Now, the reason for the java callback is if I click away from the editbox while typing &#8211; I don&#8217;t want the regular callback to fire and capture what I entered, I just want to clear and reset. As it happens this is not a very good solution at all, but it is illustrative of the problem.</p>
<p>Now, here&#8217;s the problem. There are other components that belong to the uipanel parent- 5 children in total, with the order of creation such that the listbox is item #3 in the hPanel.Children array. Visually, there is another uicontrol with position coordinates that are on top of the listbox, such that I have to restack the children, like so:</p>
<pre lang="matlab">
function moveToFront(ths)
    hCtrl = handle(ths);
    uistack(hCtrl,'top');
end
</pre>
<p>Before restacking the java callback triggers just fine. After restacking, the java callback no longer fires. And I have no idea why. Any idea how to fix this?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Oleg		</title>
		<link>https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-344867</link>

		<dc:creator><![CDATA[Oleg]]></dc:creator>
		<pubDate>Tue, 27 Jan 2015 17:26:24 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1933#comment-344867</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-333547&quot;&gt;Chris Rodgers&lt;/a&gt;.

Support confirmed that in R2014b we are out of luck and it is not high up in the priority list.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-333547">Chris Rodgers</a>.</p>
<p>Support confirmed that in R2014b we are out of luck and it is not high up in the priority list.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Chris Rodgers		</title>
		<link>https://undocumentedmatlab.com/articles/fixing-a-java-focus-problem#comment-333547</link>

		<dc:creator><![CDATA[Chris Rodgers]]></dc:creator>
		<pubDate>Wed, 08 Oct 2014 02:04:09 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1933#comment-333547</guid>

					<description><![CDATA[Dear Yair,

This seems to have broken in R2014b. Calling setFocusable doesn&#039;t seem to have any effect now.

For example:
&lt;pre lang=&#039;matlab&#039;&gt;
figure(101)
clf

% Add a text uicontrol to label the slider.
txt = uicontrol(&#039;Style&#039;,&#039;edit&#039;, &#039;Position&#039;,[400 45 120 20], &#039;String&#039;,&#039;Vertical Exaggeration&#039;);

jButton = javax.swing.JButton(&#039;Click me!&#039;);
jbh = handle(jButton,&#039;CallbackProperties&#039;);
set(jbh, &#039;ActionPerformedCallback&#039;, @(varargin) disp(&#039;CLICKED!&#039;))
[hjButton,hContainer] = javacomponent(jButton, [10,10,80,20], gcf);

jButton.setFocusable(true);
jButton.requestFocus();

uicontrol(&#039;Style&#039;,&#039;text&#039;, &#039;Position&#039;,[100 245 240 60],...
    &#039;String&#039;,&#039;Press TAB - java button should get focus...&#039;,&#039;FontSize&#039;,16);
&lt;/pre&gt;

If you run this in R2014a, then by pressing TAB and ENTER one can make  the &quot;CLICKED&quot; message appear in the command  window.

But in R2014b, pressing TAB will never give focus to the java button.

Do you have any ideas how to work around this?

Thanks,

Chris.]]></description>
			<content:encoded><![CDATA[<p>Dear Yair,</p>
<p>This seems to have broken in R2014b. Calling setFocusable doesn&#8217;t seem to have any effect now.</p>
<p>For example:</p>
<pre lang='matlab'>
figure(101)
clf

% Add a text uicontrol to label the slider.
txt = uicontrol('Style','edit', 'Position',[400 45 120 20], 'String','Vertical Exaggeration');

jButton = javax.swing.JButton('Click me!');
jbh = handle(jButton,'CallbackProperties');
set(jbh, 'ActionPerformedCallback', @(varargin) disp('CLICKED!'))
[hjButton,hContainer] = javacomponent(jButton, [10,10,80,20], gcf);

jButton.setFocusable(true);
jButton.requestFocus();

uicontrol('Style','text', 'Position',[100 245 240 60],...
    'String','Press TAB - java button should get focus...','FontSize',16);
</pre>
<p>If you run this in R2014a, then by pressing TAB and ENTER one can make  the &#8220;CLICKED&#8221; message appear in the command  window.</p>
<p>But in R2014b, pressing TAB will never give focus to the java button.</p>
<p>Do you have any ideas how to work around this?</p>
<p>Thanks,</p>
<p>Chris.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
