Search code examples
javaswingjfreechartswingworker

How to use SwingWorker?


Friends, i am developing a java application. Thats for performance monitoring. on that i am getting values in one class and drawing a graph in another class. i want to use swingworker to perform those two class alternatively.

        ResultSet rs;
        Connection conn = null;

        conn = (Connection)getMySqlConnection();

        Statement st = conn.createStatement();
        rs = st.executeQuery("SHOW GLOBAL STATUS");
        while(rs.next())
        {
            Map_MySql.put(rs.getString(1), rs.getString(2));
        }
        conn.close();

Above class for collecting server status and store it in hash map. this class called as "MySQLClass".

        System.out.println("Graph Occur");
        XYDataset Dataset;
        TimeSeries Series = new TimeSeries("Random Data");
        Second sec = new Second();
        ChartPanel CPanel;
        if(Operation_Combo.getSelectedItem().toString() == "MySQL")
        {
         if(MySQLClass.Map_MySql.get(""+MainWindow.SelectedNode+"") == null)
         {
             Value = 0;
         }
         else
         {
             Value = Integer.parseInt(MySQLClass.Map_MySql.get(""+MainWindow.SelectedNode+""));
         }
         System.out.println(Value);
        }
        if(Operation_Combo.getSelectedItem().toString() == "SQL Server")
        {
         if(SqlServerClass.Map_SQLServer.get(""+MainWindow.SelectedNode+"") == null)
         {
             Value = 0;
         }
         else
         {
             Value = Integer.parseInt(SqlServerClass.Map_SQLServer.get(""+MainWindow.SelectedNode+""));
         }
         System.out.println(Value);
        }
        String CounterName = MainWindow.SelectedNode.toString();
        Series.add(sec, Value);
        Dataset = new TimeSeriesCollection(Series);
        Chart = ChartFactory.createTimeSeriesChart(CounterName, "Time", "Range", Dataset, true, false, false);
        XYPlot Plot = (XYPlot)Chart.getPlot();
        Plot.setBackgroundPaint(Color.LIGHT_GRAY);
        Plot.setDomainGridlinePaint(Color.WHITE);
        Plot.setRangeGridlinePaint(Color.RED);
        CPanel = new ChartPanel(Chart);
        Panel1.revalidate();
        Panel1.add(CPanel);
        System.out.println("Chart Added");
        Panel1.validate();
        Panel1.repaint();
        Thread.sleep((int)MainWindow.Interval_Combo.getSelectedItem() * 1000);
        System.out.println("Sleep="+((int)MainWindow.Interval_Combo.getSelectedItem() * 1000));
        System.gc();

Above is the code for drawing Graph in one class called "Graph". How can i use swing worker to perform this alternatively and draw graph in every iteration. if you know help me please.


Solution

  • At first, i just call the doInBackground() function from MySQL.execute(); using this. And then in doInBackground() function , i just collect those counter values and use publish(); function to passcertain value. here i just pass flag to denote data's were collected successfully. publish(GraphLock);

    After calling the Publish(); method, Process(List chunks) method get invoked. On that i just check the condition and call the Graph class to generate the graph.

        if(GraphLock==true)
            SwingUtilities.invokeLater(new Graph());
    

    It works properly...