File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Problem with Adding items from JComboBox to JList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with Adding items from JComboBox to JList" Watch "Problem with Adding items from JComboBox to JList" New topic
Author

Problem with Adding items from JComboBox to JList

rayan alex
Ranch Hand

Joined: Nov 25, 2012
Posts: 33
    
    1
Hi,
In my program im trying to add items from combo box to Jlist. But when i click on the item on combo box,
it adds same item 2 times.
please help to fix this error.

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1939
    
  28
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
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.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
I shall move this to our GUIs forum.

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: 36486
    
  16
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
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: 1939
    
  28
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.
 
wood burning stoves
 
subject: Problem with Adding items from JComboBox to JList
 
Similar Threads
Set up Jlist
JList Problem
selecting item from a list to a table
drag and drop DND click, then move??
Clearing a JList