简体   繁体   English

JList如何以简单的方式删除Item?

[英]JList how to remove an Item in simple way?

I know there are some questions related to this but they did not help me at all 我知道与此相关的一些问题,但它们根本没有帮助我

my code is simple I'm trying to delete the first item on my list 我的代码很简单,我正在尝试删除列表中的第一项

DefaultListModel model = (DefaultListModel) jList1.getModel();        
model.removeElementAt(0);

this gives me a ClassCastException as follows 这给了我一个ClassCastException如下

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: rfs.search$1 cannot be cast to javax.swing.DefaultListModel
at rfs.search.jTextField1KeyReleased(search.java:130)
at rfs.search.access$500(search.java:15)

'rfs' is my package name and the 'search.java' is java file that contains the jList1 “ rfs”是我的软件包名称,“ search.java”是包含jList1的Java文件

basically my code look like this (there are some netbeans auto generated code I did not include here) 基本上我的代码是这样的(有一些netbeans自动生成的代码我没有在这里包括)

package rfs;

import javax.swing.DefaultListModel;


public class search extends javax.swing.JFrame {
    public search() {
        initComponents();

}
private void jTextField1KeyReleased(java.awt.event.KeyEvent evt){                                        
    DefaultListModel dlm = (DefaultListModel) jList1.getModel();        
    dlm.removeElementAt(0);

}                                      

// Variables declaration - do not modify                     
private javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
// End of variables declaration                   

} }

If you initialize your JList using Vector , all changes to the Vector will be visible in the JList . 如果使用Vector初始化JList ,则对Vector所有更改将在JList

Code: 码:

Vector<String> vector = new Vector<>();
vector.add("a");
vector.add("b");
vector.add("c");
JList<String> jlist = new JList<>(vector);

jlist.setSelectedIndices(new int[] {0, 1, 2});
System.out.println(jlist.getSelectedValuesList());

vector.remove(0);

jlist.setSelectedIndices(new int[] {0, 1, 2});
System.out.println(jlist.getSelectedValuesList());

Output: 输出:

[a, b, c]
[b, c]

There are two ways but both are similar: Way 1 : Suppose your JList is jList1 now to use DefaultListModel in your jList1 you need to set jList1 model, follow the code to set model and add values in your jList1: 两种方法,但是两者相似: 方法1 :假设您的JList现在是jList1,要在jList1中使用DefaultListModel,您需要设置jList1模型,按照代码设置模型并在jList1中添加值:

jList1.setModel(new DefaultListModel());

DefaultListModel lm1=(DefaultListModel) jList1.getModel();

lm1.add(0, "A");
lm1.add(1, "B");
lm1.add(2, "C");
lm1.add(3, "D");
lm1.add(4, "E");

To remove first(0) item follow the code: 要删除first(0)项目,请遵循以下代码:

lm1.remove(0);

Way 2 : In NetBeans follow those steps: 1st in drag and drop area select your JList and right click and select "Customize Code..." then on left side change "default code" to "custom creation" and change the code(on right side) according to the following code, 方式2 :在NetBeans中,请执行以下步骤:首先在拖放区域中选择JList,然后右键单击并选择“自定义代码...”,然后在左侧将“默认代码”更改为“自定义创建”并更改代码(在右侧)根据以下代码,

jList2 = new javax.swing.JList();
jList2.setModel(new DefaultListModel());
jScrollPanel.setViewportView(jList2);

Here jList2 is variable name of JList you are using 这里jList2是您正在使用的JList的变量名

Now you can use DefaultListModel without any exception. 现在,您可以毫无例外地使用DefaultListModel。 You can add values to the list following way 您可以按照以下方式将值添加到列表中

DefaultListModel listModel=(DefaultListModel)jList2.getModel();
listModel.add(0,"A");
listModel.add(1,"B");
listModel.add(2,"C");

and to remove first(0) item just do listModel.remove(0); 并删除first(0)项,只需执行listModel.remove(0);

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

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