[英]Getting value of String in Jlist
How can I get the value of an item in this case String item that has been added to a JList? 在这种情况下,如何获取已添加到JList的字符串项的值? What I meanV 我的意思是V
String[] coinNames ={"Quarters","Dimes","Nickels","Pennies"};
JList coinList = new JList (coinNames);
coinList[0] == "Quarters" ???????
Since I clearly can't reference it like a normal array, how might I go about getting the string value of coinlist[0]? 由于我显然不能像普通数组一样引用它,我该如何获取coinlist [0]的字符串值?
This is a simple example to get just an index to your JList and display all your JList. 这是一个简单的示例,仅获取您的JList的索引并显示所有JList。
public static void main(String[] args) {
String[] coinNames ={"Quarters","Dimes","Nickels","Pennies"};
JList coinList = new JList (coinNames);
String myCoin = (String) coinList.getModel().getElementAt(0);
System.out.println(myCoin);
/* LIST AL YOUR COINNAMES */
System.out.println("\nCoinList\n");
for(int i =0; i < coinList.getModel().getSize(); i++){
System.out.println((String) coinList.getModel().getElementAt(i));
}
coinNames[0] = "My new Value"; // Edit your CoinNames at index 0
/* LIST AL YOUR NEW COINNAMES */
System.out.println("\nNew coinList edited\n");
for(int i =0; i < coinList.getModel().getSize(); i++){
System.out.println((String) coinList.getModel().getElementAt(i));
}
}
coinList.getModel().getElementAt(0);
pls read the manual: http://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html#getModel-- 请阅读手册: http : //docs.oracle.com/javase/8/docs/api/javax/swing/JList.html#getModel--
EDIT: or simply take a look at the example at the op of the amnual page: http://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html 编辑:或只是看一下年度页面上的示例: http ://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html
// Create a JList that displays strings from an array
String[] data = {"one", "two", "three", "four"};
JList<String> myList = new JList<String>(data);
// Create a JList that displays the superclasses of JList.class, by
// creating it with a Vector populated with this data
Vector<Class<?>> superClasses = new Vector<Class<?>>();
Class<JList> rootClass = javax.swing.JList.class;
for(Class<?> cls = rootClass; cls != null; cls = cls.getSuperclass()) {
superClasses.addElement(cls);
}
JList<Class<?>> myList = new JList<Class<?>>(superClasses);
// The automatically created model is stored in JList's "model"
// property, which you can retrieve
ListModel<Class<?>> model = myList.getModel();
for(int i = 0; i < model.getSize(); i++) {
System.out.println(model.getElementAt(i));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.