关于focus问题,java日历上点击日期时没有active文本框,因为active组件在这次是java日历。
要知道哪个文本框是活动的最后,您只需要跟踪它。一种方法是向编辑框添加回调,它将使用最新活动文本框的句柄更新一个变量(存储在appdata)。
有了这个,日历的回调将只检索日期,然后将其放在 last active 文本框中。
注意:如果文本框'enable' 属性为'off' 或'inactive',则文本框的ButtonDownFcn 事件只会在左和 右键单击时触发。 (如果是'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
查看实际操作:
编辑
没有纯 Matlab 方法可以让您的 Matlab 编辑框完全可编辑一个响应(触发事件)对任何鼠标按钮的单击。
您可以通过使用 java 对象底层的文本框来获得此功能。这个 java 对象暴露了很多事件,你可以选择你需要的。
问题:
要获取底层 java 对象的句柄,您需要使用 Yair Altman 提供的全能 findjobj 实用程序。您可以从此处的文件交换处下载最新版本:findjobj
一旦你将它保存在你的 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,因为回调动作是直接完成的。