【问题标题】:Automatic scroll down on Matlab text window (uicontrol)在 Matlab 文本窗口(uicontrol)上自动向下滚动
【发布时间】:2019-07-30 07:32:08
【问题描述】:

我想使用 Matlab 连续读取文件并在专用窗口中显示它。所以我使用uicontrol 命令。它运作良好,但我想在每次更新内容时直接进入内容的末尾。有什么解决办法吗?

MWE:

figHandle = figure('units','pixels',...
                'position',[40 40 240 940],...
                'menubar','none',...
                'resize','off',...
                'numbertitle','off',...
                'name','window custom')
txHandle = uicontrol('style','edit',...
                'units','pix',...
                'position',[10 60 220 830],...
                'backgroundcolor','w',...
                'HorizontalAlign','left',...
                'min',0,'max',10,...
                'enable','inactive');
txt=repmat('t|',1,100000);
set(txHandle,'string',cat(1,get(txHandle,'string'),{txt}));

【问题讨论】:

    标签: matlab user-interface matlab-figure


    【解决方案1】:

    没有纯 MATLAB 方法可以做到这一点,但完全有可能,使用 undocummented 方法,操纵底层 java 组件。

    首先需要的是来自 Matlab 中心的实用程序 findjobj。您需要下载此函数并在您的 MATLAB 路径中访问它。此函数将检索 MATLAB 文本框下的 java 对象的句柄。

    一旦您可以访问文本框的 java 方法,将caret 移动到文本末尾很简单,您只需调用其中一个组件方法:setCaretPosition(positionIndex)

    在 MATLAB 路径中添加函数 findjobj 后,只需在示例代码之后添加以下代码:

    % Get the handle of the jave edit box
    jtxtBox = findjobj(txHandle) ;
    % Get the handle of the jave "panel" component
    jTxtPane = jtxtBox.getComponent(0).getComponent(0) ;
    % move the caret to the end of the text
    jTxtPane.setCaretPosition( numel(txt) );
    

    瞧 :-)

    【讨论】:

    • 非常感谢。另一种解决方案是使用jTxtPane.getDocument.getLength 代替numel(txt),但需要在之前添加pause(0.1)
    猜你喜欢
    • 2018-08-02
    • 2012-05-06
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多