我有一个Matlab列表框,上面的一些字符串很长。我不想仅仅因为这几个长字符串而使列表框太宽。 无论如何,只需将鼠标悬停在这些长字符串上而不使用滚动窗格,就可以在我的列表框中显示这些长字符串的尾部?
答案 0 :(得分:0)
也许,您可以设置列表框的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对象进行交互可能有一些聪明的方法。
答案 1 :(得分:-1)
请参阅使用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