Delete selected item from JList
Asked Answered
U

3

16

Can anyone tell me a short way to delete the selected items from my JList?

I searched on google and here, but I found very many ways. Which way should I use?

Unwrap answered 22/2, 2012 at 20:46 Comment(6)
Get the JList's model (which will likely be a DefaultListModel object) and delete the selected item from the model via its removeElement(Object obj) method.Stook
See the standard Java Swing tutorial on this topicNewborn
@JoopEggen: I'm not the down-voter if that's what you're asking, but I disagree with you in that I think that there is one simple answer. You get the selected object from the JList (not the index), and if not null you call the model's one remove method that takes an object: removeElement(obj).Stook
@JoopEggen: but I almost down-voted not due to the "simplicity" of the question or lack thereof, but due to the original poster's not showing any of his code attempts to solve this. How can we guess what he's doing wrong if he doesn't show us anything?Stook
@HovercraftFullOfEels: agree, but if one cannot jump from JList+ListModel to AbstractListModel and then DefaultListModel, that's an understandable blocker.Pernod
you can't get shorter than 1) check if something is selected and if so grab it 2) remove that selected item from the list - these are the absolute minimal number of steps, in whatever frameworkEdgington
P
33

As @Andreas_D said, the data centered, more abstract ListModel is the solution. This can be a DefaultListModel. You should explicitly set the model in the JList. So (thanks to comment by @kleopatra):

DefaultListModel model = (DefaultListModel) jlist.getModel();
int selectedIndex = jlist.getSelectedIndex();
if (selectedIndex != -1) {
    model.remove(selectedIndex);
}

There are several remove... methods in DefaultListModel. By the way, this is a good question, as there is no immediate solution in the API (ListModel).

Pernod answered 22/2, 2012 at 21:3 Comment(3)
I would not get the index but rather the selected item itself, and then call the model's removeElement method that takes an Object parameter.Stook
agreed: ListModel is the most minimal of all view-collection interfaces (neither mutable nor modifiable) - but that's not the OPs problem: s/he states to have read the tutorial chapter (which shows the same sample as you do here - minus the check for being selected) Beware: the default model of a JList is not a DefaultListModel, you have to explicitly set it.Edgington
@HovercraftFullOfEels Why would you use the selected item instead of the index? The selected item might appear multiple times in the JList, so removing it via removeElement would be wrong.Hierology
B
3

The JList component is backed by a list model. So the only recommended way to remove an item from the list view is to delete it from the model (and refresh the view).

Bunche answered 22/2, 2012 at 20:53 Comment(2)
If he's using a DefaultListModel (and in all likelihood he is), then there's no need to refresh the view since this should be done automatically.Stook
This was a more or less general answer to a general question.Bunche
F
0

Once you delete the element from the model it will also be removed from the list. You can refer this JList article for more information. As the list is backed by a model if you do any operation on the model it will also reflect on the list. you just need to refresh the view.

Fidellia answered 7/2, 2019 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.