Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Adding Items to Jlist using JComboBox. Problem with avoiding duplicates.

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    19
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Adding Items to Jlist using JComboBox. Problem with avoiding duplicates.

    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.getEleme ntAt(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.

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    class Jlist5 extends JFrame implements ItemListener{
     
    	private DefaultComboBoxModel comboModel;
    	private DefaultListModel listModel;
        private JList list;
        private JComboBox combo;
        private JPanel JP1;
     
     
    		Jlist5(){
     
    			super("Option Form 1");
    			JFrame f1 = new JFrame();
    			setSize(100,00);
    			setLocationRelativeTo(null);
    			setDefaultCloseOperation(f1.EXIT_ON_CLOSE);
     
    			JP1 = new JPanel();
    			JP1.setLayout(new FlowLayout());
     
     
    			combo = new JComboBox();
    			combo.addItemListener(this);
     
    			listModel = new DefaultListModel();
    			list = new JList(listModel);
     
     
    			JScrollPane scroll = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    			scroll.setPreferredSize(new Dimension(250,150));
     
    			String []ary1 = {"Item 1111","Item 2222","Item 3333","Item 4444","Item 5555"};
     
    			for (int i = 0; i <ary1.length ; i++)
    			{
    				combo.addItem(ary1[i]);
    			}
     
    			JP1.add(combo);
    			JP1.add(scroll);
    			add(JP1);
    			pack();
    		}
     
     
    	public void itemStateChanged(ItemEvent e){
    		if(!(e.getStateChange()== ItemEvent.SELECTED)){
     
    				for (int i = 0; i < listModel.getSize(); i++) {
    					 if (combo.getSelectedItem().equals(listModel.getElementAt(i)))
    					 {				
    							JOptionPane.showMessageDialog(null,"Duplicate");
     
    					 }else 
    							listModel.addElement(combo.getSelectedItem());
    							JOptionPane.showMessageDialog(null,"Added");
    				  }
    		}
     
    //		if(!(e.getStateChange()== ItemEvent.SELECTED)){
    //			 listModel.addElement(combo.getSelectedItem());
    //	      }	
       }  
     
    	public static void main(String args[]){
    		Jlist5 cmb = new Jlist5();
    		cmb.setVisible(true);
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding Items to Jlist using JComboBox. Problem with avoiding duplicates.

    program didnt wiork.
    Please explain what "didn't work" means. What happened when the code was compiled and executed?
    If there were errors, copy the full text and post it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    19
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Adding Items to Jlist using JComboBox. Problem with avoiding duplicates.

    Code Excutes. But when i select items on combo box, it addes all items to Jlist without giving error message to avoid Duplicates.
    Sorry about confusion.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding Items to Jlist using JComboBox. Problem with avoiding duplicates.

    What do I need to do when executing the code to see the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    19
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Adding Items to Jlist using JComboBox. Problem with avoiding duplicates.

    When you click on the item on the Combo box, those items should add to JList. (If the item is already in JList, Message will pop up to to avoid Duplicate adding)
    Problem is Items on combobox not adding to Jlist, when i click on the combo box items.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding Items to Jlist using JComboBox. Problem with avoiding duplicates.

    With the posted code, nothing happens when I select an item in the combox. Have you tested the posted code?

    For debugging I suggest that the JOPtionPane methods be replaced with calls to println() so all the program's output will be in the console.
    Next add some extra println statements to print out the value of e when the listener is called.
    Also add extra info to the print outs to show the value of the selected item and the value of the element currently being looked at from the lisModel.

    When you print out all these values as the code is executed, you will see what the problems are.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    19
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Adding Items to Jlist using JComboBox. Problem with avoiding duplicates.

    Thank you very much for your advice. Im new to Programming. So struggling even with small programs. I ll try according to your gudence and see.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding Items to Jlist using JComboBox. Problem with avoiding duplicates.

    Learning how to debug your code will be useful forever.
    Sometimes printlns are a good technique,
    sometimes using breakpoints in an IDE is good.

    Print out most variables and value returned from methods to see what the computer sees. There are often surprises.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Adding a value to a DefaultListModel via ListSelectionListener (JList)
    By chronoz13 in forum AWT / Java Swing
    Replies: 4
    Last Post: August 1st, 2012, 11:39 AM
  2. JcomboBox duplicates problem
    By ishiro in forum AWT / Java Swing
    Replies: 0
    Last Post: May 28th, 2012, 01:16 PM
  3. adding varibles to array and using Jlist
    By Fantasy in forum Collections and Generics
    Replies: 1
    Last Post: November 24th, 2011, 06:19 AM
  4. JComboBox help again but different help 75+ items in it
    By derekxec in forum AWT / Java Swing
    Replies: 7
    Last Post: August 27th, 2011, 06:53 PM
  5. [SOLVED] adding items to a binary tree
    By vendetta in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 26th, 2010, 09:32 PM