Observablelist and arraylist, different behavior - java

I'm working with a javafx listview in muliple selection mode, specifically a method which deletes selected items in the list.
the method with a observablelist
list.getItems()
.removeAll(
list.getSelectionModel()
.getSelectedItems());
the method with an array list
list.getItems()
.removeAll(new ArrayList<Object>(
list.getSelectionModel()
.getSelectedItems()));
Why would these implementations produce different results?
The arraylist implementation removes only selected items.
The observablelist implementation removes everything after the first selected item, whether it's selected or not.
Could this be a bug in the javafx removeAll method?

The ObservableList is backed by the actual model, and as such changes along with the listview while the entries are removed. By first copying the selected elements to an ArrayList, you eliminate the problem: the created ArrayList does not depend on the listview, and as such does not change during the removeAll operation.

list.getItems().removeAll(
new ArrayList<Object>(list.getSelectionModel().getSelectedItems()));
This is creating an ArrayList<Object> whose Object it contains is another type of list structure.
So in your first case the list.getSelectionModel().getSelectedItems() gets a list of items and the .removeAll looks through that list to see which items to remove.
In the second case the new ArrayList<Object>(list.getSelectionModel().getSelectedItems()) is an ArrayList of a list - the .removeAll looks for that list within the list that calls it.

Related

How can I add more than one element to a JList?

Apparently, the .setListData() method only allows an user to add one element at a time to a JList. I want to give the user the ability to add more than one element to the JList at one time.
Also .setListData() only adds more elements to the JList. Is there a way to have the newly added elements replace the existing elements in a JList?
From the documentation for JList:
public void setListData(E[] listData)
Constructs a read-only ListModel from an array of items, and calls setModel with this model.
Note the words "array of items". This means you can add more than one element at a time.

Recyclerview: Hiding Items without removing them from the ArrayList

I've been searching for that for quite a while and haven't found a proper answer:
Is there a way to remove an Item from a RecyclerView without removing it from the underlying ArrayList?
I want to add a functionality, to temporarily hide Items from the List, when a certain condition is true. I still need those items back later, but managing 2 separate ArrayList seems overly complicated.
If I set the Visibility of the Item inside the Adapter to gone, I have a visible gap where the item was, so that is not a solution.
Is there any way by which I can avoid managing 2 separate ArrayLists?
I think you'll have to actually remove from the data model. Otherwise, you'll either end up with blank rows, as you say, or when you scroll, the "hidden" recycled elements will get jumbled up.
Just because you remove from the the Arraylist doesn't mean you can't add another list to (temporarily) store the items that were removed.
For example, define this in the adapter
private List<Item> removed = new ArrayList<>();
public void remove(int position) {
removed.add(items.get(position));
items.remove(position);
notifyDataSetChanged();
}
If you need better functionality about which items you have removed, a Hashmap may be better.
you have an ArrayList of object type
You have a model/pojo class of Object type
while iterating JsonData from JsonResponse just pass boolean value true/false in object type Model class. The data you want to show make it true else false, later you can set the 'false value to true' to your Model class.
Hope this will work.
In my case, I was using getApplicationContext() instead of getContext or this

Object to ArrayList to JList and back again (A few questions)

Program Outline:
I plan to make a simple Java program that will load Vehicle objects (Vehicle being the superclass, EnginedVehicle and GoodsVehicle being the subclasses) from an XML file into an ArrayList which will then be displayed on a JList. The user will be able to show/hide the different Vehicle types using check boxes, add a new vehicle type or press the selected item in the JList and edit or delete it. The program will then put the Objects back into the ArrayList where it can be then saved back to the XML file.
Question: So, I am completely fine with the loading of the XML file into the ArrayList and putting that object onto the JList but the thing that is hurting my head is thinking about how I am going to:
What is the best way of getting the object back from the JList ready for it to be modified or deleted and put back into the ArrayList?
How would I show/hide the different types of Vehicles in the JList using the check boxes?
I understand this may seem a lot but, this is my first post and I am new to the community and I have fairly good knowledge of Java and OOP programming but I have just finished writing a fairly big website and going back to Java is a headache.
Since your ArrayList should be equal in size (item count) to your JList, your JList will have the index you're interested based on selection. Regardless if you want to modify or delete the item, store what index it was at and remove the item from the JList (You should be using a DefaultListModel). Use this index value to get the object from your ArrayList. If you're modifying, modify your object as needed, you shouldn't have to remove the object from the ArrayList for modifications, and place it back into your DefaultListModel. If it's a delete, then just remove the object from your ArrayList using the index value you stored.
As far as displaying (show/hide), clear your DefaultListModel (which will clear your JList), iterate through your ArrayList and add the items to the DefaultListModel that match your checkbox selection criteria.
EDIT:
I didn't take into consideration of possibly modifying/deleting items when items are hidden. For this, may want your objects to have a field that stores what index they are at in the ArrayList. This way when you do your filter, I would copy the items from your "Master" ArrayList into a sub list that you can populate your DefaultListModel. Then you apply the same logic to this sublist when selecting an item from you JList, then take your changes from your sublist and apply them to the "Master" ArrayList.
Keep in mind that when you remove an item, you'll have to reassign all items index location from that point on down.
I'm sure there is probably a cleaner way of doing this, but this is what first comes to mind for me.
I don't know if I'm horribly mistaken, but why change to a JList at all? Do you use your JList as a parameter to visualize the information in it? If yes, why dont you use your ArrayList instead? Then the checkboxes only change the visibility ot the Items of the List. So you dont have to care about indices, because they stay the same. And new entries, can be made as well... maybe im wrong but i guess you got kind of a GUI for the user to browse/alter/add new vehicles?

GlazedLists and JTable with multiple sources

I have 3 ArrayLists and a JTable.
I would like to be able to add/edit/remove elements from the selected (from one of the 3) ArrayList by selecting the elements in the JTable and for example clicking on a button.
I have this code for using only one ArrayList:
/* ... */
EventList<MyClass> eventList = GlazedLists.eventList(myFirstArrayList);
SortedList<MyClass> sortedList = new SortedList<MyClass>(eventList, null);
filterList = new FilterList<MyClass>(sortedList);
EventTableModel<MyClass> listModel = new EventTableModel<MyClass>(filterList,
myTableFormat);
table.setModel(listModel);
TableComparatorChooser.install(table, sortedList,
AbstractTableComparatorChooser.MULTIPLE_COLUMN_MOUSE);
selectionModel = new EventSelectionModel<MyClass>(filterList);
table.setSelectionModel(selectionModel);
/* ... */
How could I change the source of the EventList or the FilterList to the 2. or the 3. ArrayList so if I edit the EventList it will modify the source ArrayList too?
As far as I know, Glazed Lists will not handle propagating changes in your event lists back to your underlying ArrayLists. In fact, in your example your event list and array list are not linked in any way -- the event list just contains all the same items as the array list. See the javadoc for the GlazedLists.eventList static helper here
What you probably want to do is install a listener on your event list and propagate any changes to your array list. Also make sure that you manipulate the event list in response to GUI deletion events. If you modify the filter list the events won't propagate backwards to the event list.

Java Swing Updating JList

I'd like to know if there is any way that I can update a Jlist after the user adds or removes an item to it and after a user sorts it. Is there any way that I can write a standardized method to update the display based on the order of items in an array or vector, and when the user remove or adds an object from the array that the JList is based on?
Thank you.
Updates should be made to the ListModel, not the Array that was used to create the model.
However, if you want to refresh the list with completely new items or change the order of the items then you create a new DefaultListModel and use the setModel(...) method of the JList to update the view.

Categories