Jlist updating content using action listener - java

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.

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.

Populate a comboBox with one of the values of an Object from an ArrayList

I'm looking for a way to populate the JcomboBox with an ArrayList. I'd like the comboBox to display all of the professional career names that are inside each object element of the arrayList. Any tips on how to do this? or is it there a function that can instantly put on those strings into the comboBox?
This is the arrayList I've created:
ArrayList<Career> cList = new ArrayList<Career>();
This is what Career contains:
public class Career {
private String careerName;
private int careerTerm;
private String careerCode;
}
There is no method to add an ArrayList to a ComboBoxModel. You need to add each item in the ArrayList to the combo box in a loop.
You can add any Object to the model of the combo box.
Then you should then create a custom renderer to display a property of your custom Object.
See Combo Box With Custom Renderer for a general renderer to get you started.
You can also read the section from the Swing tutorial on Providing Custom Renderers for basic information about renderers.

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

jcombobox - check whether model content changed

I have created a class extending JComboBox. the model is set like this:
DefaultComboBoxModel<String> readoutModel = new DefaultComboBoxModel<String>(options.toArray(new String[options.size()]));
setModel(readoutModel);
The class implements a listener interface to listen for changes of another class (myModel). These changes might be not relevant at all for that combobox, it might contain selection changes and it might contain content changes for that combobox.
it's easy to change the selection like this:
#Override
public void modelChanged() {
...
setSelectedItem(myModel.getSelectedReadOut());
}
but what if the content of the combobox needs to be changed? shall I replace the combobox model? would I have to interate over the items and compare them with the items present in myModel? I could also remove all items from the combobox model and then add item by item from myModel? (which would also happen if just the selection changes...).
Three options to update your combo box when the underlying data is changed:
Exchange the model (replace with the new one)
Use a generic mutable model (such as DefaultComboBoxModel) and add/remove the data to reflect the changes
Create your own model implementation which is an Adapter to the actual data, and fire change events to reflect changes on the data.
The Adapter solution is quite easy to implement (ComboBoxModel, which is a ListModel), doesn't need to duplicate data and therefore does not need synchronization. Usually the best option, in my option.

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