JComboBox content disappear once I select one - java

I have a JCombobox whose content is populated this way:
List<MyClass> l = getList();
for(MyClass ll : l)
combo.addItem(ll.toString());
combo.setSelectedIndex(0);
Everything's ok till I click on combo in order to show item list: when I click selected item disappears (but list appears) when I select an item on list whole combobox disappears!!
Why?

Problem was related to another problem I had and this answer
Panel components disappear when I minimize frame
solved both!

For JComboBoxs (add, remove, manage) Items to use is the ComboBoxModel.
JComboBox and its Model is based on arrays Vector<> or Object[], since Java2.
All updates (JComboBox and its Model) must be done on Event Dispatch Thread.

Related

Move a list from a frame to another frame

In this code, I need to display a list of drugs formed by selected items from another 3 lists from another class and I want to show in my frame in this class I create a object 'c' from where I initialised my list.
I put selected items into "items" and then i move items in a java.awt.List mylist
but when I run code, don't show anything about my list.
If for example I make items = c.getList_drugs_critics() and I display it right, show the correct list, but if I want too show selected items from 3 lists don t run.

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.

How to manipulate JComboBox - Java. (MouseListener)

ComboBox cmbCategory, cmbType;
I have two ComboBoxes. The 2nd ComboBox depends on the first ComboBox.
For example.
If I chose "Food" on cmbCategory, the choices on cmbType would be "desert, appetizer..")
How will I do that without clicking a button. I mean when "Food" is chosen on cmbCategory, the choices for food on cmbType would showed up automatically without clicking a button. Because what I have come up to is that my cmbType is hidden and when a button is clicked, then that's the time it will be visible.
I believe this is about MouseListener or MouseClicked but I have no idea on how to do it.
You can add an ActionListener to the cmbCategory. On select call getSelectedItem() to get category.
Define a Map> the map should keep the list f items for each category. Fill the map (or you can define some logic to get list of types by selected category). Then just remove all the existing items in cmbType and add new list of types for the selected category.
See the ombobox related code snippets here

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 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