GlazedLists and JTable with multiple sources - java

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.

Related

Java Swing - jComboBox not refreshing

I have an issue with my jComboBox not reflecting the changes in the model ...
In the model I have Box class that keeps an array list of Items. I have a combo box model defined this way :
myCombo.setModel(new javax.swing.DefaultComboBoxModel(box.items().toArray()));
(I use NetBeans 'design' mode). So as far as I understand, after setting the model this way, the combo box should reflect any changes in items list and at the start of the application, it indeed correctly shows the elements of the item list.
I also have a button Add to add a random instance of Item to the items list.
private void buttonAddActionPerformed(java.awt.event.ActionEvent evt) {
box.addRandomItem();
}
The method addRandomItem() simply adds some new instance of Item to the items list. So when I click the Add button, the new random item is correctly added to the items list (I can see it printed at the console) but at the GUI level, the list in combo box is not being updated so I cannot see the newly added item in it.
You should add the new item manually to the model using addElement(E element) or insertElementAt(E element, int index).
Internally, the DefaultComboBoxModel makes a copy of the items you provide as a parameter in the constructor, so there isn't any way to know that the original array has changed.

Java Vector class not updating

So I'm working on a program for an intro level Java class, and am having an issue when using Vector to dynamically display data to a GUI. We're storing data in a linked list and using the vector to have a dynamic list showing the data members on the side of the GUI. We can add and remove data from the linked list just fine, and when we add to it, the vector automatically updates (we have a call to do that at the end of every successful add and remove). However, when we successfully remove a data member from the linked list, the data stays on the Jlist that the vector is being displayed on. We are required to use vector and cannot use a generic ArrayList.
Any help would be appreciated.
Assuming your JList is backed by a Vector (by using the JList(Vector<?> listData) constructor), the JList will copy the values out of the vector once and never read it again. This means changes to the vector will not be reflected in the JList.
To have your JList dynamically update you'll need to use a ListModel. A common approach is to create a JList with a DefaultListModel, then update the DefaultListModel accordingly. DefaultListModel will fire the appropriate ListDataEvents when you modify it.
DefaultListModel listModel = new DefaultListModel();
JList jlist = new JList(listModel);
// to update
listModel.addElement("foo");
listModel.addElement("bar");

Observablelist and arraylist, different behavior

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.

Java ZK ListBox listModel out of sync after sorting

hey guys i am new to ZK framework i have a listbox being sorted in the view later i pass the listBox to the controller and i need the items being selected by the user but in the model the items are syncronized with the sorting but in the getSelection array is not syncronized with the sort insted with the original data here is the code.
public void createPDFFromModel(Listbox list,String ref){
BindingListModelList model = (BindingListModelList)list.getModel();
for(int i=0;i<model.size();i++){
System.out.println((((ZamoraListitemAdapter)model.get(i)).getName()));
}
System.out.println("-------------------------------------------");
//Data Printed OK.
java.util.ArrayList<ZamoraListitemAdapter>selections = new java.util.ArrayList<ZamoraListitemAdapter>(model.getSelection());
for(int i=0;i<selections.size();i++){
ZamoraListitemAdapter clazz = (ZamoraListitemAdapter)selections.get(i);
System.out.println(clazz.getName());
//Out of sync with model and with sorting
}
my question is how i get the order of the items after the sort in getSelection model. i am using ZK 5.2.8
You could sort selections after you create it.
There are maybe other/better solutions, but you have
to write what you try to achieveif you need more help.
Reply to Comment
What I mean is
ArrayList<ZamoraListitemAdapter>selections =
new ArrayList<ZamoraListitemAdapter>(model.getSelection());
Collections.sort(selections);
So selections is a List of all your selected items and as long
as you compare the items the same way, they should be in the right order
In case you mean reorder and not sorting...
For me sorting means it is automatic and done by a algorithm.
Reordering instead means you, for example, drag the objects around.
If you mean reordering, and your Model hasn't a way to know if
a item is selected, you probably did something wrong.
Cos zk has got the two classes ListModelList and AbstractListModel
wich implement all methods needed for selection behavior and a custom
Model should, if the programmer wants a selectable Model, inherent
from one of them, cos it's the easiest way.
You should may also read this.
model.getSelection()
returns a Set (no order), instead use
model.getInnerList()
that returns ListModeList

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