Java Swing Updating JList - java

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.

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");

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?

Jlist updating content using action listener

I have a Jframe that contains of two panel.
In one panel named "panelA" , user select a category by selecting an item from JCombobox and then click on "update button". Then I have another panel named "panelB" that contains a Jlist and it showes a list of existing items in that selected category from "panelA".
I have a model class that does the logic part of the application using observer pattern. I send the changes using action listener from panelA after clicking the button to the model class and model class does the work and prepares a list that contains filtered data. Then I need to some how get filtered data show on Jlist content on panelB .
Now My Problem is I don't know which method of the Jlist class updates the content the Jlist. Or it is better to say I don't know which method in Jlist class changes the datalist in Jlist.
I just need a clue.
Sorry if my question is not professional, I am very new at java and programing.
Thanks
If you want a variable-size JList, you should initialize it with JList(ListModel), using a list model that allows adding new elements to the list (like DefaultListModel).
Then you can add new elements like this:
DefaultListModel model = (DefaultListModel)list.getModel();
model.addElement(element);
Use generics if you are using Java 7:
DefaultListModel<MyClass> model = (DefaultListModel<MyClass>)list.getModel();
model.addElement(element);
Where MyClass is the class of the list elements.

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

Categories