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 :)

Recommended Answers

All 4 Replies

  1. model = new DefaultListModel(); should be declared as local variable or separate class

  2. List = new JList( this.observerlink.getlist() );

    a) List is reserved Java word for util.List and awt.List ....change that to myList (Java is CaseSensitive, for more infor to search for Java naming convention)

    b) myList should be local variable

  3. this.scrolllist = new JScrollPane(logList); ... as local variasble

  4. nobody knows whats drawGUI(); probably add JScrollPane with JList to the alreasy visible GUI, revalidate() notify used LayoutManager correctly

  5. don't to create, remove, modify the same JComponents on the runtime, use DeafultListModel, add, remove, modify value there, then all circus about add a new JList is more than contraproductive

  6. for better help sooner post an SSCCE, short, runnable, compilable, just about JFrame with one JList wrapped in JScrollPane

hmm all of that is good advice, and i did change it all but i still have the same problem. It actually seems to be the jbutton event is called twice. Adding 2 items to the list instead of just 1.

If i do :

public void actionPerformed(ActionEvent e) {

        if (e.getSource() == printbutton) 
        {
            System.out.println("triggered");
            this.validate();

        }

    }

instead of my add to list function then it prints the line triggered twice instead of just once, per button click.... im unsure why

You may have added your java.awt.event.ActionListener to your button twice. Nothing you have posted shows you adding it even once, but adding it twice would cause actionPerformed to be called twice for each button click. An SSCCE would make finding a problem like that easy.

Thank you all tons, it was the event listener but i also learned some other stuff!.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.