如何获取活动对象MATLAB GUI的句柄 [英] How to get handle to active object MATLAB GUI
问题描述
我正在尝试使用MATLAB GUI创建日历.
我有两个Edit Text
对象-edittext1
和edittext2
.
Im trying to create a calendar using MATLAB GUI.
I have two Edit Text
objects - edittext1
and edittext2
.
我要这样做:
我将光标放在edittext1
上,然后在日历上选择日期,然后将其插入edittext1
的文本字段中.
与edittext2
相同:如果我将光标置于edittext2
中并选择将其放入edittext2
编辑文本中的日期.
I want to do this one:
I put cursor at edittext1
then select date at calendar and it pits into text field of edittext1
.
And the same for edittext2
: if I put cursor into edittext2
and select date it puts into edittext2
Edit Text.
我知道我可以通过这种方式对日历使用回调.
I know I can use callback for calendar this way.
问题:
如何将Callback函数处理程序放入主动编辑文本对象? 如何获取当前光标所在的对象的句柄?
How can I put into Callback function handler to ACTIVE edit text object? How to get handle to object where cursor is now?
推荐答案
关于 focus 问题,当您单击Java上的日期时,没有 active 文本框日历,因为此时 active 组件是Java日历.
About the focus question, there is no active text box when you click a date on the java calendar, because the active component at this time is the java calendar.
要知道哪个文本框处于活动状态, last ,您只需要跟踪它即可.一种方法是在编辑框中添加一个回调,该回调将使用最新活动文本框的句柄更新变量(存储在appdata
中).
To know which text box was active last, you simply need to keep track of it. One way is to add a callback to the edit box, which will update a variable (stored in the appdata
) with the handle of the latest active text box.
为此,日历的回调将仅检索日期,然后将其放置在上次活动文本框中.
Armed with that, the callback of the calendar will just retrieve the date, then place it in the last active text box.
注意:仅当文本框'enable'
属性为'off'
或'inactive'
时,文本框的ButtonDownFcn
事件才会在左侧和上单击. (如果它是'on'
,则仅检测到右键单击).这就是为什么我将文本框声明为inactive
的原因.那不会阻止您以编程方式更新文本,因此我认为这不是问题.
Note: The ButtonDownFcn
event of the text box will only fire on left and right click if the text box 'enable'
property is 'off'
or 'inactive'
. (if it is 'on'
, then only the right click is detected). That is why I declared the text boxes as inactive
. That does not prevent you to update the text programmatically so I didn't think it was a problem.
testcalendar.m
的代码:
function testcalendar
handles.f = figure;
commonEditProperties = {'Style', 'edit', 'String', '', ...
'Units', 'Normalized', ...
'Enable','inactive' , ...
'callback',@EditBoxFcn , ...
'ButtonDownFcn',@EditBoxFcn } ;
handles.ledit = uicontrol( commonEditProperties{:}, 'Position', [0.1 0.1 0.3 0.1], 'Tag','ledit' );
handles.redit = uicontrol( commonEditProperties{:}, 'Position', [0.5 0.1 0.3 0.1], 'Tag','redit' );
% preallocate a variable to hold the active text box handle
setappdata(handles.f,'activeTextBox',[]) ;
com.mathworks.mwswing.MJUtilities.initJIDE;
% Put calendar to my figure
handles.jPanel = com.jidesoft.combobox.DateChooserPanel;
[handles.hPanel,handles.hContainer] = javacomponent(handles.jPanel,[100,100,200,200], handles.f);
juiFunHandle = handle(handles.jPanel, 'CallbackProperties');
set(juiFunHandle, 'MousePressedCallback', ...
@(src, evnt)CellSelectionCallback(src, evnt, handles));
set(juiFunHandle, 'KeyPressedCallback', ...
@(src, evnt)CellSelectionCallback(src, evnt, handles));
% store gui handles in application data
guidata(handles.f , handles)
end
function EditBoxFcn(hobj,~)
handles = guidata(hobj) ;
ActiveTextBox = get(hobj,'Tag') ;
setappdata( handles.f , 'activeTextBox', handles.(ActiveTextBox) ) ;
end
function CellSelectionCallback(~, ~, handles)
% retrieve the handle of the active text box
ActiveTextBox = getappdata(handles.f,'activeTextBox') ;
% assign a default active text box if none was selected before
if isempty(ActiveTextBox) ; ActiveTextBox = handles.ledit ; end
numRetry = 10 ;
for k=1:numRetry
pause(0.1)
dateString = char( javaMethodEDT('getSelectedDate', handles.jPanel) ) ;
if ~isempty(dateString) ; break ; end
end
set(ActiveTextBox , 'String' , dateString ) ;
end
查看实际效果:
See it in action:
没有一种纯粹的Matlab方法可以使您的Matlab编辑框完全可编辑 an (单击事件)对任何鼠标按钮的响应(触发事件).
您可以通过使用底层Java对象的文本框来获得此功能.这个Java对象公开了很多事件,您可以选择所需的事件.
There is no pure Matlab way to have your Matlab edit box fully editable an reacting (firing an event) to a single click of any mouse button.
You can get this functionality by using the text box underlying java object. This java object exposes a lot of events and you can just pick the one you need.
收获:
要获取基础Java对象的句柄,您需要使用 Yair Altman 的全能findjobj
实用程序.您可以从以下文件交换下载最新版本: findjobj
The catch:
To get the handle of the underlying java object, you need to use the almighty findjobj
utility by Yair Altman. You can download the latest version from the file exchange here: findjobj
一旦将其保存在Matlab路径中,只需将上面示例中定义的定义编辑框的第一行代码替换为:
Once you have that saved in your Matlab path, just replace the few first line of code defining the edit boxes given in the example above by:
commonEditProperties = {'Style', 'edit', 'String', '', 'Units', 'Normalized', 'Enable','on' } ;
handles.ledit = uicontrol( commonEditProperties{:}, 'Position', [0.1 0.1 0.3 0.1] );
handles.redit = uicontrol( commonEditProperties{:}, 'Position', [0.5 0.1 0.3 0.1] );
% preallocate a variable to hold the active text box handle
setappdata(handles.f,'activeTextBox',[]) ;
% Find the java underlying object for the text boxes
ledit = findjobj(handles.ledit) ;
redit = findjobj(handles.redit) ;
% assign a callback to the java object (which CAN detect single click)
set(ledit,'MouseClickedCallback',@(h,e) setappdata( handles.f , 'activeTextBox', handles.ledit ) ) ;
set(redit,'MouseClickedCallback',@(h,e) setappdata( handles.f , 'activeTextBox', handles.redit ) ) ;
您可以完全注释或删除子功能EditBoxFcn
,因为直接执行了回调操作.
And you can completely comment or remove the sub-function EditBoxFcn
as the callback action is done directly.
这篇关于如何获取活动对象MATLAB GUI的句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!