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.
Code :
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);
}
}
Re: Adding Items to Jlist using JComboBox. Problem with avoiding duplicates.
Quote:
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.
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.
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?
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.
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.
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.
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.