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

조회 수: 2 (최근 30일)
Josh G.
Josh G. 2020년 9월 3일
답변: Josh G. 2020년 9월 16일
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',...
'Position',[100,100,200,200],...
'Callback',@generateField);
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')
delete(hTestField);
delete(testField);
end
end
end
end
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2020년 9월 3일
Josh - can you request focus on the javacomponent (JComponent) with perhaps requestFocusInWindow? WIthout seeing your code, it isn't all that clear if this method will be sufficient or helpful.
Josh G.
Josh G. 2020년 9월 3일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Josh G.
Josh G. 2020년 9월 16일
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:
drawnow;
javaMethod('requestFocusInWindow',hTestField);
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.

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2020년 9월 4일
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").focus();
});
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).

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by