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
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 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 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 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.