Hello,
Iv recently been having trouble with a java project im working on as i need a button to add a sentence to a Swing list box. The problem im having is it uses an observable class and an observer.
Fundamentally the list adds the elements to an array in an observable class and the observer is class uses this array and adds them to the list box. The problem im having is it adds twice as many as the number of times you click the button :
public void update(Observable o, Object arg) {
updatelist();
}
public void updatelist()
{
this.model = new DefaultListModel();
this.List= new JList();
this.List = new JList( this.observerlink.getlist() );
// above gets array of elements from other class
this.scrolllist = new JScrollPane(logList);
drawGUI();
this.validate();
}
public void testregnum(String item)
{
if(this.observerlink.getMatch(item)!=null && !this.observerlink.suspendedpermit(item))
{
observerlink.addLog("works");
}
else if(this.lnkVehicle_list.getMatch(item)!=null && this.lnkVehicle_list.suspendedpermit(item))
{
observerlink.addLog("doesent work");
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==addtolist) //check for reg number
{
testString();
this.repaint();
}
}
These are the functions im using attached to a button. Ultimately they just add a string in an array inside class observerlink. It then uses this array as storage incase there are multiple windows. The only problem is when it updates it adds each item in the array twice to the list!
Any help in understanding or fixing this problem would be appreicated :)