[英]How to get all values from Jlist?
I have to find the longest word for each value from Jlist, not only a selected one.我必须从 Jlist 中找到每个值的最长单词,而不仅仅是一个选定的单词。 How do I get all values, without selecting anything?
如何在不选择任何内容的情况下获取所有值?
I have the list defined:我定义了列表:
list = new JList<String>(model);
btnNewButton_1.addActionListener(new ActionListener() { // Run
public void actionPerformed(ActionEvent e) {
String intxt = "", extxt = "";
intxt = list.getSelectedValue();
if (intxt == null) {
JOptionPane.showMessageDialog(null, "No chosen value", "Error",
JOptionPane.INFORMATION_MESSAGE);
} else {
...
You can use a for
loop in its ListModel
and call getElementAt(int index)
method:您可以在其
ListModel
中使用for
循环并调用getElementAt(int index)
方法:
JList<String> list = new JList<>();
for (int i = 0; i < list.getModel().getSize(); i++) {
String listElement = list.getModel().getElementAt(i);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.