Multiple JComboboxes for one List - java

I'm currently facing a problem while having multiple Comboboxes for one List of Strings.
I have 1-5 Comboboxes depending on the selection of another Combobox.
When the User selects "3" then Combobox 4 and 5 become hidden.
At the moment I added the StringLists content to every Combobox at the start of the programm, allowing to select the same element on two different comboboxes.
My goal is to have the Comboboxes remove items that are selected by other visible Comboboxes already / add them back to every other visible Combobox when another Combobox changes the selected item.
Here is a quick example how it should look like in the end:
List has {"Cheese","Bread", "Butter}
The standard amount of comboboxes is 2
Combobox1 has "Cheese" selected and "Butter" as another option
Combobox2 has "Bread" selected and also "Butter" as another option
The user changes selected Item of Combobox2 to "Butter"
Combobox1 has "Cheese" selected and "Bread" as another option
Combobox2 has "Butter" selected and also "Bread" as another option
The user selects 3
Combox1 has "Cheese" selected
Combox2 has "Butter" selected
Combox3 has "Bread" selected
I really dont know how I could do this without having to use multiple switch cases to remove and add the item for every possible outcome. I have looked for other posts on similar problems and found 2 or 3 but they didn't really apply for the visible/invisible comboboxes problem.
Every kind of help is appriciated :)

Related

Editing Combobox Items Java

How can we make the items we choose in the combobox unusable?
I have 3 items in my combobox, when I select one of these items and press the ok button, it is processed. In the next process, I want to see this item in the combobox but make it unselectable. I would appreciate your help.
As it can be seen, we have 3 items and every time I press the OK button, I want to be able to see these 3 items but not be able to select the item we selected before.And by pressing the Ok button 3 times, we should not be able to select any of them again.

Creating a Jcombobox with multi select functionality in swing

I want to create a multi select list with Jcombobox displaying object of my custom class. Initially i thought i will write a renderer and and paint a jcheckbox inside it so that user will be able to select multiple items. I also looked on internet for such type of implementations. My problem is when user selects multiple checkbox, jcombobox shows only the last selected checkbox item value as its label. I wanted in this way
Checkbox item1, Checkbox item2
Or item1, item2 can also suffice my requirement.
Also when i ask for selected items i should get both selected items.
Please help me with this.
Thanks in advance.

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

Java ItemListener for JComboBox

I have a JComboBox and an ItemListener which can detect when there is an item state change. I want it to listen for whether the FIRST item in the list is selected.
It is a list of usernames, with the first item always being by default. However I need to know if the user selects the item in case there is a username called that gets added.
So basically I need a way to detect when the first item is selected, like where index = 1 or something. Thank you
I believe you're looking for JComboBox#getSelectedIndex
You could also use JComboBox#getSelectedItem
You could use ItemEvent#getItem which will return the selected item, but you'd need the combo box to determine the index of the item...

how to make when selecting items from 2 comboboxes dyanamically link to 3 rd combobox in java swings netbeans?

I am creating two comboboxes and inserting some elements like suzuki,Honda,activa into first combo and Renault,Indica,Benz into second combo box, and also i am creating 3rd combo which has nothing to insert. my question is when I will select an item in first for example if I select suzuki in first combo and Renault in second, both of these suzuki and Renault should appear in 3rd combobox, and if I change activa in first and Indica in second then 3rd combo should display activa and Indica. like this 3rd combo box should dyanamically get the selected items from first and second.
Plz help me,
Thanks and Regards,
-Chandrashekara Y D.
Create an ActionListener and register it at the first two ComboBox instances by calling addActionListener().
In your action listener, update the model of the third ComboBox. Initially set a DefaultComboBoxModel. In the action listener call getModel() and add/remove whatever you want. Or just create a new DefaultComboBoxModel every time and set it.
But should 3rd combo box remember values from first choice?
For example you have choosen suzuki in 1st, renault in 2nd, there are suzuki and renault in 3rd.
Now you change selection of 1st to activa and 2nd to indica. Should 3rd one be cleared and now contain activa and indica only? Or all 4 values (suzuki, renault, avtiva, indica)?
I guess 1st and 2nd combo are binded, and value to the 3rd should be added ONLY, when position in 1st AND 2nd combo are selected?
Easiest way is to create a button with handler, so where you click it, it will take data from 1st and 2nd combo and add it to 3rd (cleared or not).
If you want to operate only on selections, this one would be more complex. I guess you should introduce variables (boolean) to hold current state of each combo box. So you change boolean state of each boolean (cb1, cb2) to true when selected. In each cb selection handler you check if both boolean values are true, add proper values to 3rd cb and set boolean values back to false.
Not sure if it solves your problem, there are many possible scenarios to cover. I would stand for button - simple and more straightforward.

Categories