Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Matlab-将鼠标悬停在字符串上,在列表框中显示长字符串的后缘_String_Matlab_User Interface_Listbox_Uicontrol - Fatal编程技术网

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

String Matlab-将鼠标悬停在字符串上,在列表框中显示长字符串的后缘,string,matlab,user-interface,listbox,uicontrol,String,Matlab,User Interface,Listbox,Uicontrol,我有一个Matlab列表框,其中一些字符串非常长。我不想仅仅因为这几个长字符串就让listbox太宽。 是否只需将鼠标悬停在这些字符串上而不使用滚动窗格,就可以在我的列表框中显示这些长字符串的后缘?也许,您可以设置列表框的TooltipString属性。这是将光标悬停在某个对象上时显示的内容。这将不是一个好的或用户友好的,但总比没有好 %Create a listbox myListbox = uicontrol('Style','listbox'); set(myListbox,'Toolti

我有一个Matlab列表框,其中一些字符串非常长。我不想仅仅因为这几个长字符串就让listbox太宽。
是否只需将鼠标悬停在这些字符串上而不使用滚动窗格,就可以在我的列表框中显示这些长字符串的后缘?

也许,您可以设置列表框的
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对象交互,可能有一些聪明的方法。

请参阅Jan使用Java对象的答案。工作得很好

% 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
%准备Matlab列表框uicontrol
hFig=数字;
listItems={‘苹果’、‘橘子’、‘香蕉’、‘柠檬’、‘樱桃’、‘梨’、‘瓜’;
hListbox=uicontrol(hFig,‘样式’、‘列表框’、‘位置’、[20,20,60,60]、‘字符串’、列表项);
%获取列表框的底层Java控件
jScrollPane=findjobj(hListbox);
%我们得到了scrollpane容器-得到它实际包含的listbox控件
jListbox=jScrollPane.getViewport.getComponent(0);
%转换为可回调的引用句柄
jListbox=句柄(jListbox,'CallbackProperties');
%设置鼠标移动事件回调
set(jListbox,‘MouseMovedCallback’,{@MouseMovedCallback,hListbox});
%鼠标移动回调
函数mouseMovedCallback(jListbox、jEventData、hListbox)
%获取当前悬停的列表项
mousePos=java.awt.Point(jEventData.getX,jEventData.getY);
hoverIndex=jListbox.locationToIndex(鼠标点)+1;
listValues=get(hListbox,'string');
hoverValue=listValues{hoverIndex};
%根据悬停的项目修改工具提示
msgStr=sprintf('item#%d:%s',hoverIndex,hoverValue);
设置(hListbox,“工具提示”,msgStr);
结束%mouseMovedCallback