简体   繁体   English

Java更新列表模型字符串

[英]Java updating listmodel string

I have a JList that uses a list model to add strings of text to the JList . 我有一个JList ,它使用列表模型将文本字符串添加到JList I am having trouble figuring out how to update a selected listModel . 我在弄清楚如何更新选定的listModel时遇到麻烦。 I am able to select the listModel String (I have verified that what ever I select returns correctly) but I am unable to figure out how to update the listModel string I select. 我能够选择listModel字符串(我已经确认选择的所有内容listModel正确返回),但是我无法弄清楚如何更新我选择的listModel字符串。 Any help with this would be greatly appreciated. 任何帮助,将不胜感激。 Is there a updateElement(variable) or something I could use to accomplish this? 是否有一个updateElement(variable)或我可以用来完成此操作的东西?

    String string1 = "hello";
    String string2 = "goodbye";
        String myItem = jlst.getSelectedValue();
    // myItem is the string returned
        listModel.addElement(string1 + string2);
// adds a new element is there anyway to update myItem so string1 and string 2 become apart of the myItem string ?

Make sure you are using a DefaultListModel . 确保您使用的是DefaultListModel

/* Create model */        
DefaultListModel<String> dlm = new DefaultListModel<>();

/* Add elements */ 
dlm.addElement("test");
dlm.addElement("test2");

/* JList to use the model */ 
JList<String> list = new JList<>(dlm);

/* Update an element */
dlm.set(1, "test3");

I am having trouble figuring out how to update a selected listmodel. 我在弄清楚如何更新选定的列表模型时遇到了麻烦。

Read the section from the Swing tutorial on How to Use Lists . 阅读Swing教程中有关如何使用列表的部分 It has a working example that shows you how to dynamically add/remove elements from the DefaultListModel based on a user interaction with the GUI. 它有一个有效的示例,向您展示如何基于与GUI的用户交互从DefaultListModel动态添加/删除元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM