Creating a Jcombobox with multi select functionality in swing - java

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.

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

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

Jcombobox with Jcheckboxes

how can I create (with JAVA) Combobox that contains Checkboxes for multiple selection and display the selected items in the Combobox like this picture:
click to see the pic
and thnx for advance.
EDIT:
I found this API (JAPURA API) and it's great but when I select multiple things I want to display the selected items instead of "* multiple items *".
Here's the link to achieve your goal:
JComboBox Providing a Custom Renderer
I would suggest you create a JButton and style it in the form of a Combobox if you want that.
Then, at the onclick function (actionperformed), you create a JPanel and make it visible under the button. In the panel, you can put whatever you want, so you just put checkboxes there.
I know this is kind of a workaround of your problem, but it should be easy for you to do so, and the actual user does not see a difference at all.
Hope I could help you.
Cheers,
Lucky

Working with radio button in java

I've two jRadioButtons in Java application. Let's say Male and Female. Initially one is selected. While selecting another, the previous should be unselected and vice-versa. How to make it work? Also how can it be used to store in database?
You can give same name to all the radio buttons, from which you want to select only one. That way, you will be able to select just one button out of all. This you can do by creating a ButtonGroup.
Now, if you want to add the selected item to the database, just get the value corresponding to the name of Button Group, you will get the value of selected button.
See documentation and tutorial
What you need to use is a ButtonGroup component. Have a look at: How to Use the ButtonGroup Component

Categories