This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Put a print statement in your itemStateChanged method and print out the value returned by the ItemEvent's getStateChange() method. (You may want to look at the API docs for this as well).
Joined: Nov 25, 2012
Tony Docherty wrote:Put a print statement in your itemStateChanged method and print out the value returned by the ItemEvent's getStateChange() method. (You may want to look at the API docs for this as well).
Thank you for your help.
I printed values using this statment System.out.println(e.getStateChange());
When i select items on combo box, console prints 1,2,1,2,1,2 value pattern.
Please help to understand what's the error here. Im new to programming.
Have look through the ItemListener documentation, as Tony has suggested, and also have a look at the states, which you will probably find under ItemEvent. It looks as if two things might be happening every time you click an item.
Joined: Oct 13, 2005
Follow on post from OP which I thought was too similar to this thread to be a thread on its own:-
On the other thread, rayan alex wrote:Hi,
In the following program, Im trying to add some items to Jlist using JComboBox without any duplicates.
I managed to add items with Duplicates, As well as I managed to find duplicates using this condition (combo.getSelectedItem().equals(listModel.getElementAt(i)) )
But when i add else statement, program didnt wiork. Please help me to understand where is the error of this program,
Thank you in advance.
Copied and pasted from other thread.
By the way: Did you sort out the duplications you mentioned yesterday?
Joined: Nov 25, 2012
No I tried many times. I couldnt fix the error yet. Im new to programming.
All i want to do is, When i click on the items on combo box, those items should add to Jlist.
But if the item already in the Jlist, Program should pop up error message to avoid adding duplicate items.
Please help me to understand, whats the wrong with my code.
Joined: Aug 07, 2007
If you want to add an item when it is selected why are you doing things when the event is not a SELECTED event.
This is strange logic. If you want to react on a deselected event test for DESELECTED.
I suggest you get a pen and paper and, using your brain rather than a computer, 'run' the code in your itemStateChanged method writing down changes in values as you 'execute' each line. I would start by writing down a list of say 3 items that could be in the JComboBox and pretend you have clicked on item 1 then item 3, then item 2 and then item 1 again and for each click write down what is now in the JList.