How can I set the focus to a GUI element created with javacomponent?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Geoff Hayes
le 3 Sep 2020
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.
Réponse acceptée
Plus de réponses (1)
Geoff Hayes
le 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").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 commentaires
Voir également
Catégories
En savoir plus sur Environment and Settings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!