I want to add JFreeChart
to JPanel
and then add JPanel
to JTabbedPane
. I managed to display JFreeChart
on JFrame
, but I want to add JFreeChart
as a 4th tab of JTabbedPane
.
Code to display Chart:
public class Chart extends javax.swing.JPanel {
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JPanel jPanel1 = new JPanel();
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(60, "Marks", "Student 1");
dataset.setValue(40, "Marks", "Student 2");
dataset.setValue(90, "Marks", "Student 3");
dataset.setValue(50, "Marks", "Student 4");
dataset.setValue(70, "Marks", "Student 5");
dataset.setValue(30, "Marks", "Student 6");
JFreeChart chart = ChartFactory.createBarChart(
"Student Marks", "Student Name", "Marks", dataset,
PlotOrientation.VERTICAL, false, true, false);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.BLACK);
ChartPanel panel = new ChartPanel(chart);
panel.setDomainZoomable(true);
jPanel1.add(panel, BorderLayout.CENTER);
panel.setPreferredSize(new java.awt.Dimension(500, 270));
}
Code to add to TabbedPane:
tabStoreTrans.add("chart", new Chart());
As shown in How to Use Tabbed Panes, you can add components to a tabbed pane.
ChartPanel
, a subclass ofJPanel
, is such a component. In addition to these examples, you can use the example below to experiment with other features.As you are new, limit using the NetBeans GUI editor, implied in your question, but continue to use the NetBeans IDE.