1

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.

P Zhu
  • 21
  • 3

1 Answers1

4

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.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • Thank you very much! I'm trying it out but have some questions. Is the Observable pattern imported, or I should create my own? Also does the notifyObservers() method have argument? I see in the sorting loop it passes observers as argument but in the method signature there is none. – P Zhu Mar 08 '16 at 22:33
  • I'm all for the use of the observer pattern, but if I was to setup a custom observer like this, I'd be more tempted to provide better naming for the events (such as `itemsWereSwapped`), when there is only one possible event, it might not make a lot of difference, but if you wanted to add more events, it can make life a little easier (IMHO). I'd also be tempted to extend from `EventListener`, this way you could use the `EventListenerList` which has `protected` access in the Swing components to manage it, just one less thing to have to deal with ;) - Still +1 for the recommendation :) – MadProgrammer Mar 08 '16 at 23:52
  • @PZhu The Observer pattern ***do not*** have to be imported, the only thing you need to do is to create 2 more interfaces in your project. (Observer and Observable interface like what I shown in the example). Even though Java does have Observer interface as well, I will suggest you to implement your own. This way, you can customize the abstract method(s) you need.`notifyObservers()` does not have an argument. I have amended it. (I was too sleepy when I wrote this at 4am). – user3437460 Mar 09 '16 at 05:47
  • @PZhu If my solution does help you, you may accept my solution by clicking the hollow looking tick beside my answer. :) – user3437460 Mar 09 '16 at 05:49
  • @MadProgrammer Perhaps you can show us the implementation with `EventListenerList`? I will not hesitate to up vote it. :) – user3437460 Mar 09 '16 at 05:55