有关focus问题,没有active单击 java 日历上的日期时的文本框,因为active此时的组件isjava 日历。
了解哪个文本框处于活动状态last,您只需要跟踪它即可。一种方法是向编辑框添加回调,这将更新变量(存储在appdata
) 与最新活动文本框的句柄。
有了这个,日历的回调将只检索日期,然后将其放入最后登录文本框。
注:ButtonDownFcn
文本框的事件只会在左侧触发and如果文本框右键单击'enable'
财产是'off'
or 'inactive'
。 (如果是'on'
,则仅检测到右键单击)。这就是为什么我将文本框声明为inactive
。这并不妨碍您以编程方式更新文本,所以我认为这不是问题。
代码为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
看看它的实际效果:
Edit
没有纯粹的 Matlab 方法可以让您的 Matlab 编辑框完全可编辑an对任何鼠标按钮的单击做出反应(触发事件)。
您可以通过使用底层 java 对象的文本框来获得此功能。这个 java 对象公开了很多事件,您可以选择您需要的事件。
要点:
想要获取底层java对象的句柄,需要使用万能的findjobj
效用由亚尔奥特曼。您可以从此处的文件交换下载最新版本:findjobj https://uk.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects
将其保存在 Matlab 路径中后,只需将上面示例中定义编辑框的第一行代码替换为:
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
因为回调操作是直接完成的。