This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Remove from JList  RSS feed

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a List of names that I then put into a String[] and then put that list into a JList. The problem is when I display the JList everthing is fine except I can't remove a listing from the JList. I can remove it from the List just fine. But the JList doesn't update and I don't know how to remove a String at an index in a JList . I have tried JList.remove(int) but that doesn;t work. Anyone??
Thanks
Gregg
 
Luong Nguyen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think you should create MyList that is extended from JList. In the new class, you write a removeElement(int index) method as below:
public void removeIndex(int index) {
ListModel model = getModel();
int sz = model.getSize();
if (index < 0 | | sz <= index) {
return;
}
Object[] objs = new Object[sz - 1];
for (int i = 0; i < index; i++) {
objs[i] = model.getElementAt(i);
}

for (i = index + 1; i < sz; i++) {
objs[i] = model.getElementAt(i);
}
setListData(objs);
}
Regards.
 
Luong Nguyen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I almost forgot. There is an example about JList. Please refer to http://java.sun.com/products/jfc/tsc/tech_topics/jlist_1/jlist.html
Good luck.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tips. But I figured it out. You have to put your list into a ListModel and then add and remove from the ListModel.
Thanks for replying though.
Happy Coding
Gregg
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!