public void removeSelectionInterval(int index0,
int index1)
Sets the selection to be the set difference of the specified interval and the current selection. Both the index0 and index1 indices are removed. It's not necessary for index0 to be less than index1. This is a convenience method that just delegates to the selectionModel. The DefaultListSelectionModel implementation will do nothing if either index0 or index1 are -1. If index0 or index1 are less than -1, IndexOutOfBoundsException is thrown.Parameters:index0 - the first index to remove from the selection
index1 - the last index to remove from the selection
int index1)
Sets the selection to be the set difference of the specified interval and the current selection. Both the index0 and index1 indices are removed. It's not necessary for index0 to be less than index1. This is a convenience method that just delegates to the selectionModel. The DefaultListSelectionModel implementation will do nothing if either index0 or index1 are -1. If index0 or index1 are less than -1, IndexOutOfBoundsException is thrown.Parameters:index0 - the first index to remove from the selection
index1 - the last index to remove from the selection
Removes all the components from this container.------
这个是移components 不是内容
values.addElement("a");
values.removeelement("a");
JList j = new JList(values);
j.updateUI();
具体问题还是查一下api
注意static & instance
你那个是取消选中的吧?
((javax.swing.DefaultListModel) jList.getModel())
……………………………………这里改成你的ListModel,并要实现removeAllElements()的方法。当然,你可以改成另一个名字。
运行后怎么会在((javax.swing.DefaultListModel) jList.getModel()).removeAllElements();这句话抛出classcastexception?
那个*jList*就是你的List啊替成jl就可以至于classcastexception,你在初始化jl的时候要加一句:
jl.setModel(new javax.swing.DefaultListModel())
打出来看看,是不是你初始化以后又有类似
jl=new JList();
这种东西啊??
应该是System.out.print(jl.getModel());吧?
加在((javax.swing.DefaultListModel) jl.getModel()).removeAllElements();之前显示结果是javax.swing.JList$1@78920a
这能说明什么问题呢?
((javax.swing.DefaultListModel) jList.getModel()).removeAllElements();改成((javax.swing.AbstractListModel) jList.getModel()).removeAllElements();有ListModel的父类代替就可以不管你设的是什么了:)
xmvigour(微电--有狼的气质 没狼的勇气 真郁闷)是啊,是啊,偶新人,以后大哥罩偶啊,正在学习的说。
呵呵 我们互助:)swing我还得向你学习:)
本来defaultlistmodel的时候还没保错说没这个函数?
我的jlist中的元素来源于vector型的choices,jlist又是在一个jScrollPane1里,所以只要
choices.clear();
jScrollPane1.getViewport().setView(null);
jScrollPane1.getViewport().setBackground(Color.white);
就行了。给分喽!
faint啊,这个
寒一个你这不是解决问题的办法啊,把jList从界面删除了,看起来是那么回事,实际。。