P Zhu
P Zhu

Reputation: 21

updating change to another class

I'm new to java and I'm stuck here...What I want to do is to update the changes of an arraylist in one java file to JPanel in another file. I'm doing sorting to the arraylist so it can't be done manually. Is there a way I can "tell" the JPanel what's going on in the soring?

I have BubbleSort.java which does the sorting (works), Animation.java which contains the JPanel and JFrame class (works for displaying unsorted histograms).

It's homework so I prefer not to post my code here. But if my description doesn't work I'll post it.

Thank you!

Updating: Well the homework requires bubble sort so I have to. https://www.youtube.com/watch?v=wB7ovstyH4E Here is what it suppose to be. I only have the unsorted yellow histograms in the first few seconds and a working sorting method.

Upvotes: 1

Views: 136

Answers (1)

user3437460
user3437460

Reputation: 17454

Is there a way I can "tell" the JPanel what's going on in the sorting?

There are many ways to do it. The most direct way would be keeping a reference of the current unsorted list and perform the sorting in the panel class. Every time when 2 elements are swapped from the list, invoke repaint() to repaint the current positions of the elements in the list.


However, the more elegant way will be using Observer pattern by establishing a contract between the DrawingPanel and the class executing the sort.

The DrawingPanel can implements an Observer interface, while the SortAlgorightm class implements an Observable interface.

You can let the DrawingPanel be notified everytime 2 elements are swapped in the sorting class.


In your Observer Pattern, you will have the following interfaces:

public interface Observer
{
    public void update(ArrayList<Integer> list);    
}

public interface Observable
{
    public void register(Observer o);
    public void unregister(Observer o);
    public void notifyObservers();
}

Establishing the contract between GUI and Sorting Algorithm:

class DrawingPanel extends JPanel implements Observer{
    //Other attributes and initializations not shown
    @Override
    public void update(ArrayList<Integer> list){
        this.list = list;   //You can choose to receive element 
                            //indexs which got swapped instead (this is up to you)
        repaint();          //Repaint your current display when list is updated
    }

    //Register myself with the sorting algorithm class
    public void registerWith(Observable ob){
        if(ob != null)
            ob.register(this);
    }
}

In your SortingAlgorithm class, enable it to send updates to all observers which already registered itself with this class:

class SortingAlgorithm implements Observable{
    private ArrayList<Observer> observers;  //keep a list of observers for notifying
    //Other attributes and initializations not shown

    @Override
    public void register(Observer o){
        observers.add(o);       
    }

    @Override
    public void unregister(Observer o){
        observers.remove(o);
    }

    @Override
    public void notifyObservers(){
        for(Observer o : observers)
            o.update(list);  //Update all observers with your latest list updates
    }

    public void bubbleSort(){
        //Your sorting happens here..
        for(int i=0; i < n; i++){
            for(int j=1; j < (n-i); j++){
                if(intArray[j-1] > intArray[j]){
                    //swap the elements!
                    temp = intArray[j-1];
                    intArray[j-1] = intArray[j];
                    intArray[j] = temp;

                    //Notify GUI to update screen
                    notifyObservers();
                }    
            }
        }
    } 
}

With the above, the GUI will be updated when ever you want it to. In this case, since we placed notifyObservers(observers); in the bubbleSort(), particularly when elements are swapped, hence the GUI will only be updated when the list changes.

Even if you are not displaying your GUI on JPanel but other contentPanes, the same logic can be applied. Just let the class handling UI to implement the Observer and register it to the SortingClass.

If you have only 1 observer, you don't even need to keep a list of Observers. You can always tweak on the minor details in my example.


If you do not want the GUI to update when 2 elements are swapped, you can always move the notifyObservers(); to another location where you want an update.

Upvotes: 4

Related Questions