Hello, peculiar behaviour with JTabbedPane

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
lucky7456969
Posts: 15
Joined: Thu Nov 01, 2012 6:41 am
antibot: No, of course not.

Hello, peculiar behaviour with JTabbedPane

Post by lucky7456969 » Thu Feb 07, 2013 6:25 am

Code: Select all

class myFrame extends JFrame
{
     myFrame()
     {
          chart1 =  new PatientDistribution("Patient Distribution");
          chart1.setDatabase(db);
          pane1  = chart1.createDemoPanel();

          chart2 = new LivingAreaDistribution("Living Area Distribution");
          chart2.setDatabase(db);
          pane2 = chart2.createDemoPanel();
   
          
         tabbedPane.add(pane1);
         tabbedPane.add(pane2);
     }


     private void refresh() throws ParseException
    {
       
       db.LoadMainData();
       db.UpdateTable();
       model.fireTableDataChanged();
       jTable1.repaint();
       changeChart();
     }

    public void changeChart()
    {
        //System.out.println("In changeChart");
        chartCanvas.changeDataset(/*db.getAllPatientsData()*/);
        livingAreaDis.changeDataset(/*db.getAllPatientByArea()*/);
    }

    looseFocusDOB()
    {
        // blah
         refresh();
    }
   
    updateButton()
    {  
       // blah
       refresh();
    }
}

public abstract class ChartCanvas extends ApplicationFrame
{
   
     /**
     *
     * @param str
     */
    public ChartCanvas(String str)
    {   
        super(str);
        
    }
    
    
    abstract DefaultPieDataset createDataset();
    abstract DefaultPieDataset createDataset(List<List<PatientRec>> data);
     
      protected void changeDataset(/*List<List<PatientRec>> _data*/)
     {
       if (dataset != null)
       {
         dataset.clear();
         dataset = createDataset();
        }
         
     }
    
    public void setDatabase(Database _db)
    {
        db = _db;
    }
     
    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     * 
     * @return A panel.
     */
    public JPanel createDemoPanel() {
        dataset = new DefaultPieDataset();
        chart = createChart(createDataset());
        return new ChartPanel(chart);
    }
     
    protected JFreeChart createChart(final DefaultPieDataset dataset) {

         JFreeChart chart = ChartFactory.createPieChart(this.getTitle().toString(), dataset, true, true, Locale.TAIWAN);

         
         chart.getTitle().setFont(new Font("新細明體", Font.BOLD, 22));
          
         
         
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new Font("新細明體", Font.PLAIN, 12));
        chart.getLegend().setItemFont(new Font("新細明體", Font.BOLD, 22));  
        
        plot.setNoDataMessage("No data available");
        plot.setCircular(false);
        plot.setLabelGap(0.02);
        return chart;
     } 
    
     
    public void saveToPNG(String filename)
    {
        try {
            final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
            final File file1 = new File(filename);
            ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
            
            
        } catch(Exception e)
        {
            e.printStackTrace();
        }
        
    }
    static Database db;
    DefaultPieDataset dataset;
    JFreeChart chart;
    
  
}

public class PatientDistribution extends ChartCanvas {
    
    public PatientDistribution(String _name)
    {
        super(_name);
    }
    
     
    protected DefaultPieDataset createDataset()
    {
        return createDataset(db.getAllPatientsData());
    }
     
      protected DefaultPieDataset createDataset(List<List<PatientRec>> data)
      {

        // create the dataset...
        if (db != null)
        {
        int total_Patients = db.getTotalPatient();
       
        for (int i = 0; i < data.size(); i++)
        {
           if (data.get(i).size() != 0) // i = 0 is not set
            {
                dataset.setValue(data.get(i).get(0).getHospital().getHospitalName(), new Double ((double)data.get(i).size()/ total_Patients)*100.0);
            }
         }
       
         return dataset; 
        }
        else
        {
            return null;
        }
         
         
    } 
    
}

public class LivingAreaDistribution extends ChartCanvas
{
    public LivingAreaDistribution(String str)
    {
        super(str);
    }
  
    
    protected DefaultPieDataset createDataset()
    {
        return createDataset(db.getAllPatientByArea());
    }
     
    
    
     protected DefaultPieDataset createDataset(List<List<PatientRec>> data) 
     {
                 

           if (db != null)
           {
               
               int total_Patients = db.getTotalPatient();
         
               // get number of living areas
            
                
        

               for (int i = 0; i < data.size(); i++)
               {
       

                   if (!data.get(i).isEmpty())
                   {


     
                       dataset.setValue(data.get(i).get(0).getPatient().getPatientArea(), new Double ((double)data.get(i).size()/ total_Patients)*100.0);

                   }

        



               }



        return dataset;        
       }
       else
       {
            return null;
       }


    }    
     
     
    
  
}

Sometimes The tab for chart1 is active but the chart actually displayed was chart2, or vice versa.
Any ideas why?
Thanks
Jack

lucky7456969
Posts: 15
Joined: Thu Nov 01, 2012 6:41 am
antibot: No, of course not.

Re: Hello, peculiar behaviour with JTabbedPane

Post by lucky7456969 » Thu Feb 14, 2013 4:35 am

Any help would be greatly appreciated!

I found out that if I don't fireDataChanged to the dataset, the jumpy behaviour is not exhibited, but if I don't do it,
the dataset will be mixed up. The chart will contain errors consisting of data of 2 different datasets.
Thanks
Jack

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Hello, peculiar behaviour with JTabbedPane

Post by John Matthews » Thu Feb 14, 2013 7:30 pm

You might try preparing an sscce using synthetic data. The word "jumpy" suggests a failure to pack() the enclosing Window, which leaves the enclosed Container invalid. The word "sometimes" suggests a failure to construct the GUI on the event dispatch thread.

Update: For reference, the JTabbedPane example seen here runs smoothly. It incorporates both principles suggested above.

Locked