MATLAB Answers

How can I set the focus to a GUI element created with javacomponent?

4 views (last 30 days)
Josh G.
Josh G. on 3 Sep 2020
Answered: Josh G. on 16 Sep 2020
I need to use a javacomponent text field to take advantage of the FocusLostCallback property. However, the only way I know of to set focus to a GUI element is uicontrol, which doesn't work for a JavaWrapper object (2nd output of javacomponent), and I get an error when trying to use it on a Java object (1st output of javacomponent) or a handle to that object (obtained via handle(foo)), which says the argument has to be a valid Parent.
How can I set the focus to this text field?
Edit: Here's a minimal working example.
function fieldWindow
f = figure('Position',[400,400,400,400]);
uicontrol(f,'String','Generate Field in Focus',...
function generateField(hObject, eventData)
[jTestField,testField] = javacomponent('javax.swing.JTextField');
hTestField = handle(jTestField); % Get a proper Matlab handle to prevent memory leaks
hTestField.FocusLostCallback = @deleteTextBox;
testField.Parent = f;
testField.Position = [100,310,200,20];
hTestField.HorizontalAlignment = javax.swing.JLabel.LEFT;
hTestField.KeyPressedCallback = @deleteTextBox;
function deleteTextBox(hObject,eventData)
if isa(eventData,'sun.awt.CausedFocusEvent')
Josh G.
Josh G. on 3 Sep 2020
I rewrote the code so it could work in a single file and added it as a MWE in the original post. I'm trying variations of javaMethod('requestFocusInWindow',jTestField) within the generateField function but no luck so far.

Sign in to comment.

Accepted Answer

Josh G.
Josh G. on 16 Sep 2020
I finally had a chance to sit down and hammer away at this until I found a solution, so I'll post it here for anyone else who needs it. The fix is a simple two lines inserted before function deleteTextBox:
Alternatively, pause(0.001) can be substituted for drawnow if drawnow doesn't work. I've run into many issues where the latency in drawing a GUI element results in unexpected behavior, and this was yet another case of that.

More Answers (1)

Geoff Hayes
Geoff Hayes on 4 Sep 2020
Josh - which version of MATLAB do you have? Does your version support uihtml? If so, you could modify one of their examples to do something like the following. You might write some HTML code where you can add listeners for your controls. In particular, you might have a click listener for your button and a focus out listener for your text field
function setup(htmlComponent) {
document.getElementById("generateFieldInFocusButton").addEventListener("click", function(event) {
document.getElementById("textInputField").style.visibility = "visible";
document.getElementById("textInputField").addEventListener("focusout", function(event) {
document.getElementById("textInputField").style.visibility = "hidden";
htmlComponent.Data = document.getElementById("textInputField").value;
document.getElementById("textInputField").value = "";
We show and hide the text field depending upon whether we click the button or the text field loses focus respectively. We write data back to the MATLAB code with
htmlComponent.Data = document.getElementById("textInputField").value;
where the main MATLAB code is simply
fig = uifigure;
fig.Position = [500 500 490 180];
h = uihtml(fig);
h.Position = [20 20 450 130];
h.HTMLSource = fullfile(pwd,'generateFieldInFocus.html');
h.DataChangedFcn = @(src,event)disp(h.Data);
See the attached m-file for the html code (you will need to change the extension to html).




Community Treasure Hunt


Start Hunting!

Translated by