Disable item from JList

How can we disable an item from JList?

[45 byte] By [student@sunDNa] at [2007-11-27 11:35:17]
# 1

you need to get the DefaultListModel from your JList and remove the item at index.

DefaultListModel dlm = (DefaulListMOdel) jList.getModel();

dlm.remove(index);

bye

Message was edited by:

RPalluk

RPalluka at 2007-7-29 17:01:57 > top of Java-index,Desktop,Core GUI APIs...
# 2

it is not working..

Exception in thread "main" java.lang.ClassCastException: javax.swing.JList$1 can

not be cast to javax.swing.DefaultListModel

at visualjava.project.MainFile.initTools(MainFile.java:110)

at visualjava.project.MainFile.<init>(MainFile.java:36)

at visualjava.project.MainFile.main(MainFile.java:131)

Press any key to continue . . .

student@sunDNa at 2007-7-29 17:01:57 > top of Java-index,Desktop,Core GUI APIs...
# 3

Why did you repost?

You got the same answer you got in your other posting which is simply to remove the item form the JList.

If you don't know how to do this then read the JLIst API and follow the link on "How to Use LIsts" for a working example.

camickra at 2007-7-29 17:01:57 > top of Java-index,Desktop,Core GUI APIs...
# 4

i have used this in a jTable and work but the jlist seems different.

You can initalize the jList with a DefaultListModel and use it to modify the data, from the error seems that isn't possible to cast a ListModel to DefaultListModel but i don't know

why, ListModel is an interface of DefaultListModel.

see this example.

http://jug.org.ua/wiki/display/JavaAlmanac/Listening+for+Changes+to+the+Items+in+a+JList+Component

sorry for my bad english!

Roberto

RPalluka at 2007-7-29 17:01:57 > top of Java-index,Desktop,Core GUI APIs...