GUI listbox

I try to get selected value(string) from listbox in MATLAB.
Is this code OK?? Here is my code:
mousePos = java.awt.Point(jEventData.getX, jEventData.getY);
clickedIndex = jListbox.locationToIndex(mousePos) + 1;
listValues = get(hListbox,'string');
clickedValue = listValues{clickedIndex};
msgbox(clickedValue);
How to use JAVA in matlab we have in java jList.getSelectedIndex();

2 个评论

Is there any reason why you are not using the matlab commands set and get?
Aldin
Aldin 2011-12-19
Java is easier than MATLAB in JAVA we have method:
jList.getSelectedIndex(); jList.getSelectedValeu();
but in MATLAB we have not

请先登录,再进行评论。

 采纳的回答

Walter Roberson
Walter Roberson 2011-12-19
strings = get(hObject, 'string');
curval = get(hObject, 'value');
if iscell(strings)
curstring = strings{curval};
else
curstring = strings(curval, :); %char array
end
Whether the string parameter is a cell array of strings, or a char array, depends on exactly how the listbox was initialized. If it was initialized to a single string that did not have any '|' characters, or initialized to a char array, then a char array will be the result, but if it was initialized to a cell array of strings, or to a single string that had on or more '|' characters, then a cell array will be the result.

5 个评论

Aldin
Aldin 2011-12-19
Can somebody tell if there a function such as in JAVA jList.getSelectedValue() or JList.getSelectedIndex(). Because i'm beginner in MATLAB. I'm JAVA programmer.
There is no single command to get the value - Walter has given you the lines you need to get the current string.
You can put these in your own sub function to have a one-liner
getSelectedIndex = @(obj) get(obj, 'Value');
getSelectedValue = @(obj) subsref(get(obj, 'String'), struct('Type','{}', 'subs', get(obj, 'Value')));
Aldin
Aldin 2011-12-19
Is this possible with uitable????
Yes, see my response to your newer question on this topic.

请先登录,再进行评论。

更多回答(1 个)

Sean de Wolski
Sean de Wolski 2011-12-19
No reason to access java directly. Use get() with the handle to the listbox to recieve the value property. E.g:
figure; %new fig
h = uicontrol('units','norm','style','listbox',...
'string',{'hello';'world';'eww monday'},'position',[.2 .2 .4 .4]); %a listbox
Select 'world'.
Now at the command line:
get(h,'value')
Addendum:
So:
figure;
names = {'Aldin';'Sean';'John'};
h = uicontrol('units','norm','style','listbox',...
'string',names,'position',[.2 .2 .4 .4]);
h(2) = uicontrol('style','push','callback',@(src,evt)disp(names{get(h(1),'value')}));
Select my name then push the button

6 个评论

Aldin
Aldin 2011-12-19
If i have a listbox with name:
Example:
Aldin
Sean
Jhon
and i want to click on Sean (mark wtih blue color) and than click
a button. and when i click button i must to get your name.
Aldin
Aldin 2011-12-19
Yes but what is if i don't know your index "2". Always when i click on the listbox i must know the name but what if i will to get the index of your name in this example it is "2"
simple. I did it in both examples above. How did I extract the name from the list of names? What was the result of the first example when you selected an item and then got it's value as I did?
Aldin
Aldin 2011-12-19
Sean, please Can you make for me a gui application with listbox in MATLAB and when i selected any name on listbox to get the value or index of the name. You can send me your GUI application on aldin_ha@live.com. Please answer if you will
Aldin
Aldin 2011-12-19
It's confused for me
Aldin
Aldin 2011-12-19
I'm beginner in MATLAB

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Call Java from MATLAB 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by