Java ItemListener for JComboBox - java

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

Related

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

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.

String Tokenizer.nextElement for JComboBox

I'm using
int TxtAge = Integer.parseInt(tfAge.getText().trim());
to get value from my textfield and search it in database.
Then, I'm using Integer age = Integer.parseInt(stringTokenizer.nextElement().toString()); to go to next attributes in my database.
I have no problem using those codes for textfield but when I'm using the JComboBox the result won't display. How to use the StringTokenizer.nextElement() for JComboBox? Is is the same with TextField?
String sex=(String) stringTokenizer.nextElement();
I tried this code but still failed :(
You seem to have left out the relevant portions of your code, i.e. how you are handling setting/getting items in the JComboBox. Whether you read these values from a database, a file or have them hardcoded is irrelevant to the question.
Since you do ask whether it is the same as with a JTextField, I can at least answer this; it is not the same. The question indicates that you're quite new to Swing. You would probably benefit from working through a basic Swing tutorial, just to get a grip on how to work with these basic GUI elements. For JComboBox, check out Oracles own How to Use Combo Boxes.
Anyways, when working with JComboBox, you will need to first populate it with the values that users can choose from and set the currently selected value. Retrieving the currently selected value is just a simple method call.
Further, you have the possibility of making a combobox editable. This means that the user can edit the text in the combo box to something that was not pre-populated. By default, this option is turned off.
I'll provide some examples.
Initialize:
JComboBox sexComboBox = new JComboBox();
sexComboBox.addItem("Not selected");
sexComboBox.addItem("Male");
sexComboBox.addItem("Female");
sexComboBox.addItem("Do not want to disclose");
By default, the first item you added is selected. To select another one, you need to add one of the following lines:
sexComboBox.setSelectedIndex(1); // zero-based index, "Male" is selected item
sexComboBox.setSelectedItem("Female"); // sets the selected item to "Female"
To enable user to edit the contents to something that was not pre-defined, just add the line:
sexComboBox.setEditable(true);
To retrieve the currently selected value:
String selectedItem = (String) sexComboBox.getSelectedItem();

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