How to get handle to active object MATLAB GUI

不羁岁月 提交于 2019-12-12 05:36:46

问题


Im trying to create a calendar using MATLAB GUI. I have two Edit Text objects - edittext1 and 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.

Question:

How can I put into Callback function handler to ACTIVE edit text object? How to get handle to object where cursor is now?


回答1:


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.

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.

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.


Code for 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:


Edit

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.

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

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 ) ) ;

And you can completely comment or remove the sub-function EditBoxFcn as the callback action is done directly.



来源:https://stackoverflow.com/questions/40363256/how-to-get-handle-to-active-object-matlab-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!