java 如何从 JList 中的组件获取文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/966625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:37:11  来源:igfitidea点击:

How can I get the text from a component in a JList?

javastringswingjlist

提问by soldier.moth

I have a JListand I am wanting to get the text of an entry of that list at a specific index. Could someone inform me how to do this or should I restructure my code to getValuesinstead of getIndices?

我有一个JList,我想在特定索引处获取该列表条目的文本。有人可以告诉我如何做到这一点,或者我应该将我的代码重构为getValues而不是getIndices

回答by Pierre

JList dataList=(...)

 for(int i = 0; i < dataList.getModel().getSize(); i++) {
     System.out.println(dataList.getModel().getElementAt(i));
 }

回答by soldier.moth

Object[] temp = jList1.getSelectedValues();
temp[i] = the object you want.

回答by Dave

DefaultListModel list = new DefaultListModel();
JList jl = new JList(list);

int i = 21;
Object = element;
String = yourElement;

element = jl.getModel().getElementAt(i);
yourElement = element.toString;

回答by SandroMarques

String nick = jListNicknames.getModel().getElementAt(index).toString();
System.out.println(nick);