Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

java - Adding ChartPanel to JTabbedPane using JPanel

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());
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As shown in How to Use Tabbed Panes, you can add components to a tabbed pane. ChartPanel, a subclass of JPanel, 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.

tabbed charts

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.Timer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @see https://stackoverflow.com/a/15715096/230513
 * @see https://stackoverflow.com/a/11949899/230513
 */
public class TabChart {

    private static final int N = 128;
    private static final Random random = new Random();
    private int n = 1;

    private void display() {
        JFrame f = new JFrame("TabChart");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JTabbedPane jtp = new JTabbedPane();
        jtp.add(String.valueOf(n), createPane());
        f.add(jtp, BorderLayout.CENTER);
        JPanel p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        p.add(new JButton(new AbstractAction("Add") {
            @Override
            public void actionPerformed(ActionEvent e) {
                jtp.add(String.valueOf(++n), createPane());
                jtp.setSelectedIndex(n - 1);
            }
        }));
        f.add(p, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private ChartPanel createPane() {
        final XYSeries series = new XYSeries("Data");
        for (int i = 0; i < random.nextInt(N) + N / 2; i++) {
            series.add(i, random.nextGaussian());
        }
        XYSeriesCollection dataset = new XYSeriesCollection(series);
        new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                series.add(series.getItemCount(), random.nextGaussian());
            }
        }).start();
        JFreeChart chart = ChartFactory.createXYLineChart("Test", "Domain",
            "Range", dataset, PlotOrientation.VERTICAL, false, false, false);
        return new ChartPanel(chart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(480, 240);
            }
        };
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TabChart().display();
            }
        });
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...