16 Jan 2018

Scroll to the last line of text edit in Matlab

收藏到CSDN网摘
Matlab's edit UIControl provide some flexibilities for displaying information, while setting the Max-Min>1 it supports multiple line. However, the caret is alwasy set to 0 position in the first line which is very annoy while you would like to show dynamic updated information to the user. To implement this capability, the underlying Java class has to be digged out. Yair M. Altman's findjobj.m function works perfectly to obtain those kind of information. Then, all we need to do is providing the Matlab UIControl's handle and set caret to the document length.

The code is like:
if isvalid(hEdit)
   hScrollPane = findjobj(hEdit);
   hText = hScrollPane.getComponent(0).getComponent(0);
   hText.setCaretPosition(hText.getDocument.getLength);
end
Note: by giving the Matlab edit UIControl's handle, the findjobj() will return it's base class in java style, which is a scrollable panel (javahandle_withcallbacks.com.mathworks.hg.peer.utils.UIScrollPane). The first call to 'getComponent(0)' returns the panel's internal view port (javax.swing.JViewport). Then, the second call to 'getComponent(0)' on view port returns the multiline text edit (com.mathworks.hg.peer.EditTextPeer$hgTextEditMultiline). As long as the handle of the java class has been obtained, the caret position can be set to the last position to force the edit showing the last line.

No comments :

Post a Comment