java - 如何让Timer类在Java中每10秒加载一个jfreechart?

标签 java swing timer jframe jfreechart

我有一个使用 jfreechart API 显示网站登录的 JFrame。这是一个条形图,数据集来自 SQL 数据库。

java项目加载图表,图表显示当前网站登录情况。由于数据集每秒都在变化,因此条形图应该是动态的。我使用 Timer 类来做到这一点。它每 10 秒运行一次 loadLogonChart()。到目前为止我没有问题。但问题是它确实会在 10 秒内更新图表。然而,20 秒后,当我首先运行表单时,它会加载值。然后这个过程会一遍又一遍地重复。我使用的代码如下。我无法弄清楚它出了什么问题。

public final class MainForm extends javax.swing.JFrame implements ActionListener{


private static int AUTO_UPDATE_CHARTS = 10000;


public MainForm() throws SQLException {
    //Timer for the barchart    
    Timer timerChart = new Timer(AUTO_UPDATE_CHARTS, new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                loadLogonChart();
            } catch (SQLException ex) {
                Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }); 
    timerChart.start();


    //LOAD THE FORM
    loadForm();

}



public void loadForm() throws SQLException{
    initComponents();
    loadLogonChart();

}

public void loadLogonChart() throws SQLException{
     //database class that I need for the barchart datasets
    Database db = new Database();

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    JFreeChart chart = ChartFactory.createBarChart("Web Site Activity Chart", "Days", "# of visits", dataset, PlotOrientation.VERTICAL, false, true, false);
    CategoryPlot catPlot = chart.getCategoryPlot();

    try {
        dataset.setValue(db.showMonday(), "logon", "Monday");
        dataset.setValue(db.showTuesday(), "logon", "Tuesday");
        dataset.setValue(db.showWednesday(), "logon", "Wednesday");
        dataset.setValue(db.showThursday(), "logon", "Thursday");
        dataset.setValue(db.showFriday(), "logon", "Friday");
        dataset.setValue(db.showSaturday(), "logon", "Saturday");
        dataset.setValue(db.showSunday(), "logon", "Sunday");

        chart = ChartFactory.createBarChart("Web Site Activity Chart", "Days", "# of visits", dataset, PlotOrientation.VERTICAL, false, true, false);
        ChartPanel myChart = new ChartPanel(chart);
        myChart.setMouseWheelEnabled(true);


        BarRenderer renderer = (BarRenderer) catPlot.getRenderer();
        DecimalFormat decimalFormat = new DecimalFormat("##.##");
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",decimalFormat));
        catPlot.setRenderer(renderer);
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.TOP_LEFT));
        renderer.setItemLabelsVisible(true);
        chart.getCategoryPlot().setRenderer(renderer);

        //renderer.setBaseItemLabelGenerator(CategoryItemLabelGenerator generator)

        chartPanel.setLayout(new java.awt.BorderLayout());
        chartPanel.add(myChart,BorderLayout.CENTER);
        chartPanel.revalidate();
        chartPanel.validate();

    } catch (SQLException ex) {
        Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
    }

}

最佳答案

    chartPanel.add(myChart,BorderLayout.CENTER);
    chartPanel.revalidate();
    chartPanel.validate();

在我看来,您试图不断向图表面板添加不同的组件。添加一个组件并不会删除之前的组件。

Swing 绘制最先添加的最后一个组件,以便旧面板将显示在新面板的顶部。尝试:

    chartPanel.removeAll();
    chartPanel.add(myChart,BorderLayout.CENTER);
    chartPanel.revalidate();
    chartPanel.validate(); // not needed since you do revalidate
    chartPanel.repaint();

关于java - 如何让Timer类在Java中每10秒加载一个jfreechart?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33614632/

相关文章:

java - Java中重载构造函数时

Java - 使用 Runtime.getRuntime().exec 启动的进程(无法创建临时文件

java - paintComponent() 正在绘制其他组件

Java 计时器程序不会更新显示的时间

c++ - OnTimer 永远不会被调用,不确定为什么?

java - 使用不同参数调用 REST WS

java - JTable 不想显示内容

java - TreeModel 接口(interface)中的方法 valueForPathChanged (Swing)

C#:如何在计时期间停止计时器

java - 在 Intellij : ClassNotFoundException: org. apache.woden.WSDLException 中生成 WSDL 时出错