1
votes

I'm making a Algorithm simulator for an assignment, I need to show line by line in the algorithm.

This is my insertion sort algo code:

private void insertionSort(int array[]) {
    DefaultListModel dlm = new DefaultListModel();
    algoShowingList.setModel(dlm);

    MyTimerTask mt = new MyTimerTask();

    int key, i = 0;

    for (int j = 1; j <= array.length - 1; j++) {
        dlm.removeAllElements();
        dlm.addElement("1. for j = " + (j + 1) + " to " + array.length);
        System.out.println("1. for j = " + (j + 1) + " to " + array.length);
        mt.delayRunning();

        dlm.addElement("2.     key = " + array[j]);
        System.out.println("2.     key = " + array[j]);
        key = array[j];
        mt.delayRunning();


        dlm.addElement("3.     i = " + i);
        System.out.println("3.     i = " + i);
        i = j - 1;
        mt.delayRunning();

        dlm.addElement("4.     while " + i + ">0 and " + array[i] + ">" + key);
        while (i > 0 && array[i] > key) {
            dlm.removeElementAt(3);
            dlm.addElement("4.     while " + i + ">0 and " + array[i] + ">" + key);


            if (algoShowingList.getLastVisibleIndex() == 4) {
                dlm.removeElementAt(4);
            }
            dlm.addElement("5.         array[" + (i + 1) + "] = " + array[i]);
            //changing txtfield colors
            changeColorToRed(findTextField(array[i + 1]));
            changeColorToGreen(findTextField(array[i]));

            array[i + 1] = array[i];
            printInitialArray(array);
            changeColorToGreen(findTextField(array[i + 1]));

            if (algoShowingList.getLastVisibleIndex() == 5) {
                dlm.removeElementAt(5);
            }
            dlm.addElement("6.         i = " + i);
            i = i - 1;
        }

        dlm.addElement("7. Array[" + (i + 1) + "] = " + key);
        array[i + 1] = key;
        printInitialArray(array);
        changeColorToRed(findTextField(key));

        resetTextFieldColorToDefault();
        printOneLoopInsertionSort(array, j);
    }

}

by calling mt.delayRunning(); i can delay executing next line for 3 secs, my problem is i want to add element by element to JList when code is running.

        dlm.addElement("1. for j = " + (j + 1) + " to " + array.length);
        System.out.println("1. for j = " + (j + 1) + " to " + array.length);
        mt.delayRunning();

        dlm.addElement("2.     key = " + array[j]);
        System.out.println("2.     key = " + array[j]);
        key = array[j];
        mt.delayRunning();

Like this System.out.println() is printing line by line i want to add element to JList, but it adds only when for loop ended and all lines added at once for the last loop.

output in console----------

  1. for j = 2 to 10 -sout prints but not adding to JList

TimerTask started

Timer task started at:Thu Sep 11 10:35:13 IST 2014

TimerTask cancelled

Timer task finished at:Thu Sep 11 10:35:16 IST 2014

  1. key = 9 -sout prints but not adding to JList

TimerTask started

Timer task started at:Thu Sep 11 10:35:19 IST 2014

TimerTask cancelled

Timer task finished at:Thu Sep 11 10:35:22 IST 2014

  1. i = 0 -sout prints but not adding to JList

TimerTask started

Timer task started at:Thu Sep 11 10:35:25 IST 2014

TimerTask cancelled

Timer task finished at:Thu Sep 11 10:35:28 IST 2014

All elements added to JList when loop ends, i need to show one by one, can someone please help me! i can't find why this is happening!

1

1 Answers

0
votes

Swing is a single threaded environment, this means two (basic) things, first, you should never block the Event Dispatching Thread in anyway (include Thread.sleep), as this will prevent the UI from been updated. Second, you should never update or modify the UI in anyway other then from within the context of the EDT.

See Concurrency in Swing for more details

Now, there are probably any number of ways you might achieve this, but the simplest might be to use a SwingWorker, do the sort (and pause) within the context of the doInBackground method and use publish/process to update the UI, that way you won't be blocking the Event Dispatching Thread

See Worker Threads and SwingWorker for more details