Editing Combobox Items Java - 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.

Related

Multiple JComboboxes for one List

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

Showing a counter dynamically for JList items highlighted

I have a program that I created for work. This program takes an uploaded file, reads it, and puts the data into a JList in the GUI. The GUI actually has two lists and the user is able to move items between the left and right list by highlighting them like usual with a JList and then hitting an arrow to move the items. The lists are multiple-interval selection.
One small addition I would like to add is some type of counter that shows the user how many items they have selected before they actually move them between lists. This would need to be dynamic so if the user holds control down and begins clicking the counter will continue to update the number of highlighted items.
As the lists are often quite large and a user might need to move an odd number of transactions between the lists (Think 300 transactions in left list and the user needs to move exactly 50) it would be beneficial to have this counter.
Can anyone think of how this could be done? I'm not sure how to add an action listener to just clicking on the items. Please also let me know if I need to elaborate any more.
Generally my question is can I create an action listener just for when a user clicks a item in a JList that updates a counter for the current selected indices? Also it would need to change when they no longer have selected an indice.
Register a ListSelectionListener with your JList.
The listener could simply query how many rows are selected and update the number in the panel to that. Perhaps use getSelectedValues().size().
http://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html#addListSelectionListener-javax.swing.event.ListSelectionListener-

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

How do you make the items in a JList toggle when you click them?

I currently have a JList and I need the items to be able to be toggled with a single click, similarly to how Ctrl-click works when it is set to MULTIPLE_INTERVAL for the selection mode.
Is it possible to make the items select and deselect as follows if there are items A, B and C without making the user require Ctrl-click?
-The user clicks A, A is selected
-The user clicks B, A and B are selected
-The user clicks A, B is selected
The easiest way I can think to do this is to add a MouseListener and capture the clicks and manually select/unselect items.

multiple instance of a JButton listener event in JLists

I have created a dialog with two JLists displayed and a button that takes the selected value from the second JList and does something (say it outputs the selected value). The list of values from the second JList is updated according to the selection of the value from the first JList.
When the dialog is displayed initially, the button is disabled. When a value is selected from the first and then from the second JList, the button is enabled an the required selection listener is added to it.
The problem is that every time the button is clicked the number of output messages is equal to the time a value of the second JList is selected. For example if I select a value from the second JList, then I change my mind and select another value, the click of the button will output the message two times.
Does anyone know a method to prevent such a thing?
Your ListSelectionListener should check for (e.getValueIsAdjusting() == false) otherwise you'll respond to all of user's selections and not just the final one.
Yes: don't cache the selections, just process the actual selection in your second list.
If possible, post the code that is executed once your button is pressed. I guess, you have some sort of collection (a list or queue) that stores all the selections you do on the dialog and when you press the button, each stored selection is processed.
This looks like an intended behaviour, because you usually don't code this by accident ;)
If it is intended and you just want to eliminate duplicates, consider using a Set instead of a list, as a Set will only contain unique values.
It does perfectly what it should do.
It fires two events,
1> Selection is removed from first item.
2> Selection is done to second item.
So as fbcocq said, you should check for getValueIsAdjusting(). Check this out, it'll help.
Are you adding an ActionListener to the button every time you enable it?

Categories