Matlab - 通过将鼠标悬停在字符串上来显示列表框中长字符串的后缘

Matlab - Display trailing edge of a long strings in a listbox by hovering the mouse over the string

我有一个 Matlab 列表框,其中有些字符串很长。我不想仅仅因为这几个长字符串而使列表框太宽。 是否可以通过简单地将鼠标悬停在这些字符串上而不使用滚动窗格来在我的列表框中显示这些长字符串的后缘?

或许,您可以设置列表框的 TooltipString 属性。这是将光标悬停在某个对象上时显示的内容。它不会很好或用户友好,但总比没有好。

%Create a listbox
myListbox = uicontrol('Style','listbox');
set(myListbox,'TooltipString','','Callback',@listboxCB);

%Callback function called each time the listbox value is changed
%It should also be called whenever the 'String' property is updated
function listboxCB(obj,evt)
    %Get the value 
    v=get(obj,'Value');
    if isempty(v)
        set(myListbox,'TooltipString','');
        return;
    end
    %Get the string corresponding to that line
    str = get(obj,'String');
    str = str{v(1)};         %Show the first one (if 'multiselect' = 'on')
    set(myListbox,'TooltipString',str);
end

直接与底层 Java 对象交互可能有一些聪明的方法。

使用 Java 个对象查看 Jan 的回答。效果很好。

% Prepare the Matlab listbox uicontrol
hFig = figure;
listItems =   {'apple','orange','banana','lemon','cherry','pear','melon'};
hListbox = uicontrol(hFig, 'style','listbox', 'pos',[20,20,60,60],  'string',listItems);

% Get the listbox's underlying Java control
jScrollPane = findjobj(hListbox);

% We got the scrollpane container - get its actual contained listbox control
jListbox = jScrollPane.getViewport.getComponent(0);

% Convert to a callback-able reference handle
jListbox = handle(jListbox, 'CallbackProperties');
% Set the mouse-movement event callback
set(jListbox, 'MouseMovedCallback', {@mouseMovedCallback,hListbox});

% Mouse-movement callback
function mouseMovedCallback(jListbox, jEventData, hListbox)
% Get the currently-hovered list-item
mousePos = java.awt.Point(jEventData.getX, jEventData.getY);
hoverIndex = jListbox.locationToIndex(mousePos) + 1;
listValues = get(hListbox,'string');
hoverValue = listValues{hoverIndex};

% Modify the tooltip based on the hovered item
msgStr = sprintf('<html>item #%d: <b>%s</b></html>', hoverIndex, hoverValue);
set(hListbox, 'Tooltip',msgStr);
end  % mouseMovedCallback

https://www.mathworks.com/matlabcentral/answers/436048-display-trailing-edge-of-a-long-strings-of-a-listbox-by-hovering-the-mouse-over-the-string#answer_352806