How can I set the focus to a GUI element created with javacomponent?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
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.
채택된 답변
추가 답변 (1개)
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).
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!