简体   繁体   English

JList 添加和删除项目 (Netbeans)

[英]JList adding and removing items (Netbeans)

I'm trying to add and remove items from my jList (jList1), but It doesn't work.我正在尝试从我的 jList (jList1) 添加和删除项目,但它不起作用。 I've searched on stackoverflow for other people with the same problem, but when their problem is solved, I keep getting errors.我已经在 stackoverflow 上搜索了其他有同样问题的人,但是当他们的问题得到解决时,我不断收到错误消息。 So this is how I declared the jList:所以这就是我声明 jList 的方式:

jList1.setModel(new javax.swing.AbstractListModel() {
        String [] strings = lijstItems;
        public int getSize() {
            return strings.length;
        }
        public Object getElementAt (int i) {
            return strings[i];
        }
    });

So now I made these buttons to add and remove items from the list:所以现在我制作了这些按钮来添加和删除列表中的项目:

private void addHostActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:

    DefaultListModel model = (DefaultListModel) jList1.getModel();
    model.add(2, "item");
    // THIS DOES NOT WORK...

}

And

private void deleteHostActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:

}

I've tried so many things, but they don't work!我尝试了很多东西,但它们不起作用! Can anyone help me please?有人可以帮我吗?

Thanks!谢谢!

You set the model of the list to an AbstractListModel .您将列表的模型设置为AbstractListModel You can't cast the model to a DefaultListModel .不能将模型转换为DefaultListModel Trying to do so will give you a ClassCastException So set the model to a DefaultListModel instead.尝试这样做会给你一个ClassCastException所以将模型设置为DefaultListModel

jList1.setModel(new DefaultListModel());

And you probably want to use DefaultListModel#addElement(element) instead of adding the element to same index every time, with add(2, element)而且您可能希望使用DefaultListModel#addElement(element)而不是每次都将元素添加到相同的索引中,使用add(2, element)

this is how I declared the jList:这就是我声明 jList 的方式:

Why are you creating a custom ListModel?为什么要创建自定义 ListModel? Just use the DefaultListModel.只需使用 DefaultListModel。 There is no need for you to create a custom model to simply store String data.您无需创建自定义模型来简单地存储字符串数据。

Then you can read the section from the Swing tutorial on How to Use Lists for a working example that does exactly what you want by using the "Hire" and "Fire" buttons.然后,您可以阅读 Swing 教程中关于如何使用列表的部分,作为一个工作示例,该示例通过使用“雇用”和“触发”按钮完全满足您的要求。

In order to keep the existing items in the JList and add new ones, I had to get the model ListModel and then add the items to DefaultListModel as the ListModel does not have an addElement method.为了在 JList 中保留现有项目并添加新项目,我必须获取模型 ListModel,然后将项目添加到 DefaultListModel,因为 ListModel 没有 addElement 方法。 This is how I implemented:我是这样实现的:

   DefaultListModel<String> model = new  DefaultListModel<String>();
   ListModel model2 = jList.getModel();
   for (int i=0; i< model2.getSize();i++){
        model.addElement(model2.getElementAt(i).toString());              
        }
   model.addElement("new element");
   jList.setModel(model);

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

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