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

rayan alex
Ranch Hand

Joined: Nov 25, 2012
Posts: 33

1

posted Feb 01, 2013 10:28:17

0

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

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

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41065

29

posted Feb 02, 2013 09:21:53

0

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?

rayan alex
Ranch Hand

Joined: Nov 25, 2012
Posts: 33

1

posted Feb 02, 2013 09:41:44

0

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.

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2585

55

posted Feb 04, 2013 07:15:07

0

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.