Jfreechart - Java Swing charts do not appear on the screen

Asked

Viewed 1,040 times

2

Hello, I’m developing a Java swing application for a university project. Basically it is a program that reads XML files from another application (mobile game) and generates graphic reports from the data (easy thing...). My question is the following, I am using the Jfreechart library that is responsible for creating the charts, I took a basic example to understand how it works and it is quite simple, except for not having worked.

I created a JFrame with a JPanel and a button, on the button I put the following code:

DefaultPieDataset dpd = new DefaultPieDataset();
dpd.setValue("Valor 1", 10);
dpd.setValue("Valor 2", 20);
dpd.setValue("Valor 3", 30);
dpd.setValue("Valor 4", 40);

JFreeChart grafico = ChartFactory.createPieChart("Nome do Grafico", dpd, true, true, true);

ChartPanel chartPanel = new ChartPanel(grafico);
primeiroGrafico.add(chartPanel);
primeiroGrafico.validate();

But the chart does not appear inside the panel!?

These are the namespaces:

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

From now on, thank you!

1 answer

2


I tested here with this code and it worked:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class Grafico {
    public static void main(String[] args) {
        EventQueue.invokeLater(Grafico::run);
    }

    private static void run() {
        // Cria uma tela com uma panel vazia dentro. Usa esta panel para definir
        // o tamanho mínimo e preferencial da janela.
        JFrame frame = new JFrame("Olá, JFreeChart!");
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JPanel raiz = new JPanel();
        raiz.setLayout(new BorderLayout());
        Dimension tamanho = new Dimension(600, 480);
        raiz.setPreferredSize(tamanho);
        raiz.setMinimumSize(tamanho);
        frame.add(raiz);
        frame.pack();
        Dimension sd = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((sd.width - frame.getWidth()) / 2, (sd.height - frame.getHeight()) / 2);

        // Cria o painel aonde o gráfico será mostrado.
        JPanel primeiroGrafico = new JPanel();
        Dimension tamanhoArea = new Dimension(600, 450);
        primeiroGrafico.setPreferredSize(tamanhoArea);
        primeiroGrafico.setMinimumSize(tamanhoArea);
        raiz.add(primeiroGrafico, BorderLayout.CENTER);

        // Cria o botão.
        JPanel areaBotoes = new JPanel();
        JButton botao = new JButton("Criar gráfico");
        areaBotoes.add(botao);
        raiz.add(areaBotoes, BorderLayout.SOUTH);

        // Define o que acontece ao clicar no botão.
        botao.addActionListener(e -> {
            DefaultPieDataset dpd = new DefaultPieDataset();
            dpd.setValue("Valor 1", 10);
            dpd.setValue("Valor 2", 20);
            dpd.setValue("Valor 3", 30);
            dpd.setValue("Valor 4", 40);

            JFreeChart grafico = ChartFactory.createPieChart("Nome do Gráfico", dpd, true, true, true);

            ChartPanel chartPanel = new ChartPanel(grafico);
            primeiroGrafico.add(chartPanel);
            primeiroGrafico.validate();
        });

        // Mostra a tela.
        frame.setVisible(true);
    }
}

Here is the result after clicking the button:

Tela com o gráfico

  • 1

    Thanks! I created a new project and it worked, now I need to see why it doesn’t work in the previous project! more thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.