0

I would like to share an issue I am having and see if anyone has any ideas. Let me explain.

I have created a rather complex graph that updates dynamically during runtime as per the code snippet below.

within Main:

if(singleRun){
     try{
       FileInputStream fin = new FileInputStream("C:\\eclipse\\data\\so.gad");
       ObjectInputStream ois = new ObjectInputStream(fin);
       SO = (GAStatisticObject) ois.readObject();
       ois.close();
     }
   catch(FileNotFoundException FNFE){System.out.println("file not found"); System.exit(1);}
   catch(ClassNotFoundException CNFE){System.out.println("class not found");System.exit(1);}
   catch(IOException IOE){System.out.println("error during data save: "); IOE.printStackTrace 
       ();System.exit(1);}

    GenotypeSimulator GS = new GenotypeSimulator(SO, populationSize); //statistic object, population size, enable immediately
    GS.simulateEvolution();     
}

basically, read data from and object file create a new simulation engine (which in turn creates the GUI and charts) and then start the simulation which basically reads the data file one data point at a time and updates the graphs. (code for simulator is below)

public void simulateEvolution(){

   for(int x=0; x<genNumberSolutionFound; x++){
    //System.out.println("updating data. GEN # = " + (x+1));   

    //if(x!=0){   
       try {Thread.sleep(1000);} 
       catch (InterruptedException e) {e.printStackTrace();}
    //}

    //data series 1
    XYDataItem point1 = null;
    if(x<genNumberSolutionFound-1){//parent vector is always 1 less in length then the rest
       point1 = new XYDataItem(new Double(x+1), new Double(selectedFitnessAvg.get(x)));    //parent select avg fitness
    }
    XYDataItem point2 = new XYDataItem(new Double(x+1), new Double(populationFitnessAvg.get(x)));  //avg fitness
    XYDataItem point3 = new XYDataItem(new Double(x+1), new Double(populationSmallestFitness.get(x))); //low fitness
    XYDataItem point4 = new XYDataItem(new Double(x+1), new Double(populationLargestFitness.get(x))); //high fitness

    //data series 2
    XYDataItem point5 = new XYDataItem(new Double(x+1), new Double(cumulativeAllelDistributionVector.get(x))); //diversity

    Vector<XYDataItem> DI = new Vector<XYDataItem>();
    DI.add(point1); //parent avg fitness
    DI.add(point2); //avg fitness
    DI.add(point3); //low fitness
    DI.add(point4); //high fitness
    DI.add(point5); //average diversity

    LGP.updateAllDataSeries(DI, new Integer(sizeOfPopulation*(x+1)));
    BGP.updateDataSet(x+1, instantaneousAllelDistributionMatrix.get(x));
   }

   BGP.setGARunComplete();

}

(note I added a sleep timer just to make the update slow down a little)

THIS WORKS GREAT!

Now here is where the problem comes in. What I really want to do is I want to start the simulation from another JFrame when I click on a JButton instead of from my main function directly. So I created a new "execution control panel" and when you click on the start button, it executes exactly the same code previously displayed within its event handler.

However, all I get is a window that is mostly blank and no longer updates dynamically like before. I know the simulation is running fine because I still see the println to StdOut, but the GUI remains blank until the "evolveSolution()" method completes and then updates all at once.

Anyone have any idea why I am getting this different behavior when executing from another GUI?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
  • You hopefully avoid doing long simulation calculations on the [EDT](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html), right? – Jens Piegsa Dec 15 '13 at 22:15
  • 2
    *"until the "evolveSolution()" method completes and then updates all at once."* Classic symptom of 'blocking the EDT'. Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Dec 15 '13 at 22:19
  • Obligatory: http://stackoverflow.com/q/9554636/1702990 – Sinkingpoint Dec 15 '13 at 22:24
  • Thanks all I figured it out. it apparently has something to do with how java does event handling. it appears that once the action event (listening for the button click) is called, this stops other GUIS from being updated until the event completes (hence why the new frame updates at the end). I wrapped my simulator into a new thread and that solved the problem. who knew? – user3050608 Jan 04 '14 at 17:17

0 Answers0