Re: Populating a Combo Box dynamically - please Help
<long explanation snipped>
Is there a way to dynamically keep track of the changes made to the
list in another class (AddAuctionsPanel class)?
When you want to programmatically/dynamically update a JList or a
JComboBox, use a ListModel or a ComboBoxModel. You can add data to the
model to your liking, but to see the changes in the view (the JComboBox
or the JLIst), you have to notify the view about the change by firing a
suitable event. Here is a piece of code that uses a common data-holding
class for both a JComboBox and a JList. When you comment the
fireIntervalAdded(this,list.size(),list.size()) line, you will see the
changes.
HTH, Piet
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.*;
public class JListDemo implements ListSelectionListener,ActionListener{
JFrame jFrame;
JList jList;
JButton addEntry = new JButton("Add entry");
UpdatingListModel listmodel;
int counter = 0;
public JListDemo(){
jFrame = new JFrame("List Demo");
JPanel panel = new JPanel();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
java.util.List list = new ArrayList();
list.add(new ListDataObject("Person Number "+ (++counter),counter));
list.add(new ListDataObject("Person Number "+ (++counter),counter));
list.add(new ListDataObject("Person Number "+ (++counter),counter));
listmodel = new UpdatingListModel(list);
JComboBox combo = new JComboBox();
combo.setModel(listmodel);
jList = new JList(listmodel);
jList.addListSelectionListener(this);
addEntry.addActionListener(this);
panel.add(new JScrollPane(jList));
panel.add(combo);
panel.add(addEntry);
jFrame.setContentPane(panel);
jFrame.pack();
jFrame.show();
}
public static void main(String[] args){
JListDemo jld = new JListDemo();
}
public void valueChanged(ListSelectionEvent lse){
if (!lse.getValueIsAdjusting()){
System.out.println("User Name: "+
((ListDataObject)jList.getSelectedValue()).getName());
System.out.println("User ID: "+
((ListDataObject)jList.getSelectedValue()).getId());
}
}
public void actionPerformed(ActionEvent ae){
listmodel.addObject(new ListDataObject("Person Number
"+(++counter),counter));
System.out.println("Number of items: "+listmodel.getSize());
}
}
class UpdatingListModel extends AbstractListModel implements
ComboBoxModel{
java.util.List list;
Object selected;
public UpdatingListModel(java.util.List list){
this.list = list;
this.selected = list.get(0);
}
public Object getElementAt(int index){
return list.get(index);
}
public int getSize(){
return list.size();
}
public void addObject(Object object){
this.list.add(object);
fireIntervalAdded(this,list.size(),list.size());
}
public Object getSelectedItem(){
return this.selected;
}
public void setSelectedItem(Object object){
this.selected = object;
}
}
class ListDataObject{
private int id;
private String name;
public ListDataObject(String name,int id){
this.name = name;
this.id = id;
}
public String toString(){
return this.id+" - "+this.name;
}
public int getId(){
return this.id;
}
public String getName(){
return this.name;
}
}