Ericson Willians
Ericson Willians

Reputation: 7855

ChartPanel not fiting the size of a JPanel (JFreeChart)

I'm just using the method setSize of the ChartPanel. For some reason, it fails to work. Here's the code:

    mostSoldPanel = new JPanel();
    chartTabbedPane.addTab("Mais vendidos", null, mostSoldPanel, null);
    mostSoldChart = ChartFactory.createBarChart("Mais vendidos", "Produtos", "Quantidade", createDataset(),
            PlotOrientation.VERTICAL, true, true, false);

    ChartPanel chartPanel = new ChartPanel(mostSoldChart);
    mostSoldPanel.add(chartPanel);
    chartPanel.setSize(mostSoldPanel.getSize());

Here's the visual result:

enter image description here

Upvotes: 1

Views: 258

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347332

The likely cause is the fact that JPanel by default uses a FlowLayout, which is allowing the child components to use their own preferredSize to when been laid out.

Consider changing mostSoldPanel = new JPanel(); to mostSoldPanel = new JPanel(new BorderLayout()); and get rid of the get/setSize calls

Upvotes: 4

Related Questions