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:
JTextField and focus order problem

Subject: JTextField and focus order problem

From: Eddy Currents

Date: 19 Oct, 2010 15:34:04

Message: 1 of 3

I am developing a GUI in Matlab R2010a. For the most part, I am using Matlab's standard uicontrols, but there are situations where I need to use javax.swing.JTextField controls instead of Matlab's edit control. The problem I encounter is that the JTextFields (which are all children of uipanel) don't participate in the panel's focus order. If I place the caret in the JTextField, and hit the tab key, focus goes to the next standard Matlab control in the focus order, and not the next JTextField. Nor can I programatically set focus to a JTextField using its requestFocus() method.

Any ideas on how to do this? (The code snippet below shows how I instantiate the JTextField and configure it).

Thanks,
EC.

javaHandle = javaObjectEDT('javax.swing.JTextField');
[obj.TextboxDay h] = javacomponent(javaHandle);
set(h, 'Parent', obj.PanelDetails);
set(h,'Position', [110, 35, 35, 25]);
set(obj.TextboxDay, 'KeyTypedCallback', @(src,event)onTextboxDayKeyTyped(obj, src, event));
set(obj.TextboxDay, 'FocusGainedCallback', @(src,event)onTextboxDayFocusGained(obj, src, event));
set(obj.TextboxDay, 'FocusLostCallback', @(src,event)onTextboxDayFocusLost(obj, src, event));

Subject: JTextField and focus order problem

From: Yair Altman

Date: 20 Oct, 2010 18:47:03

Message: 2 of 3

"Eddy Currents" <eddy.currents@yahoo.com.delete.this> wrote in message <i9kdpc$b7p$1@fred.mathworks.com>...
> I am developing a GUI in Matlab R2010a. For the most part, I am using Matlab's standard uicontrols, but there are situations where I need to use javax.swing.JTextField controls instead of Matlab's edit control. The problem I encounter is that the JTextFields (which are all children of uipanel) don't participate in the panel's focus order. If I place the caret in the JTextField, and hit the tab key, focus goes to the next standard Matlab control in the focus order, and not the next JTextField. Nor can I programatically set focus to a JTextField using its requestFocus() method.
>
> Any ideas on how to do this? (The code snippet below shows how I instantiate the JTextField and configure it).
>
> Thanks,
> EC.
>
> javaHandle = javaObjectEDT('javax.swing.JTextField');
> [obj.TextboxDay h] = javacomponent(javaHandle);
> set(h, 'Parent', obj.PanelDetails);
> set(h,'Position', [110, 35, 35, 25]);
> set(obj.TextboxDay, 'KeyTypedCallback', @(src,event)onTextboxDayKeyTyped(obj, src, event));
> set(obj.TextboxDay, 'FocusGainedCallback', @(src,event)onTextboxDayFocusGained(obj, src, event));
> set(obj.TextboxDay, 'FocusLostCallback', @(src,event)onTextboxDayFocusLost(obj, src, event));


Tricky problem, simple solution: http://UndocumentedMatlab.com/blog/fixing-a-java-focus-problem/

Yair Altman
http://UndocumentedMatlab.com

Subject: JTextField and focus order problem

From: Eddy Currents

Date: 21 Oct, 2010 14:49:04

Message: 3 of 3

>
> Tricky problem, simple solution: http://UndocumentedMatlab.com/blog/fixing-a-java-focus-problem/
>
> Yair Altman
> http://UndocumentedMatlab.com

Thanks Yair - that works a treat. In my attempts to solve this problem, I did try the setFocusable method, along with a bunch of other JTextField focus-related methods, but since I didn't really know what I was looking for, I guess I didn't realise the solution was under my nose - your blog post is as clear as a bell, and Undocumented Matlab is a great resource!

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