Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Switching JFreeCharts on a JPanel using a JComboBox

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Switching JFreeCharts on a JPanel using a JComboBox

    Okay, so I'm trying to switch between different JFreeChart graphs on a JPanel by using a JComboBox.

    In the code below, you will see that I already have already created one such chart, and added as a child to the graphingPanel. This works fine. What I would like to do, is modify the Action Listener to the ComboBox, so that I can change the image on the graphingPanel. In the action listener, you will see my attempt to do so for what I have called "Option 2" in my JComboBox (cbbGraphs), i.e. index1. I thought graphingPanel.add(chartPanel2) might work inside the action listener, but that won't compile because I "Cannot refer to a non-final variable graphingPanel inside an inner class defined in a different method.


    import java.util.ArrayList;   
    import java.awt.Color;  
    import java.awt.Component;  
    import java.awt.GridBagConstraints;  
    import java.awt.GridBagLayout;  
    import java.awt.Insets;  
    import java.awt.event.ActionEvent;  
    import java.awt.event.ActionListener; 
     
    import javax.swing.JComboBox;  
    import javax.swing.JFrame;  
    import javax.swing.JPanel;    
    import javax.swing.border.EmptyBorder;  
     
    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.category.DefaultCategoryDataset;  
    import org.jfree.data.time.TimeSeries;  
    import org.jfree.data.time.TimeSeriesCollection;  
    import org.jfree.data.time.Day;
     
     
    public class Display extends JFrame {
     
    	private JPanel panelBackground;  
        private JPanel myPanel;  
     
        private JComboBox cbbGraphs;  
     
        private GridBagConstraints constraints;  
     
         ArrayList<String> categories = new ArrayList<String>();  
     
     
        public Display()   
        {  
            //Frame details  
        super("Title");  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        setBounds(100, 100, 1200, 700);  
        setLocationRelativeTo(null);  
        panelBackground = new JPanel();  
        panelBackground.setBorder(new EmptyBorder(5, 5, 5, 5));  
        setContentPane(panelBackground);  
        panelBackground.setBackground(new Color(20,50,75));  
        GridBagLayout gbl_panelBackground = new GridBagLayout();  
        panelBackground.setLayout(gbl_panelBackground);  
     
        //Panel  
        myPanel = new JPanel();  
        myPanel.setBorder(null);  
        myPanel.setBackground(new Color(240, 255, 255));   
        GridBagLayout gbl_myPanel = new GridBagLayout();  
        gbl_myPanel.columnWidths = new int[]{50, 125, 50, 50, 50, 50, 0,50,50,50,50,50,50,50};  
        gbl_myPanel.rowHeights = new int[]{20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20};  
        myPanel.setLayout(gbl_myPanel);  
     
        //Constraints  
        constraints = new GridBagConstraints();  
        constraints.anchor = GridBagConstraints.EAST;  
        constraints.gridx=0;  
        constraints.gridy=0;  
        panelBackground.add(myPanel,constraints);  
     
        //###############################################################################         
     
         GridBagConstraints gbc_lbl = new GridBagConstraints();  
         gbc_lbl.anchor = GridBagConstraints.NORTHWEST;  
         gbc_lbl.gridx = 0;  
         gbc_lbl.gridy = 0;  
     
     
        //Graphing Panel  
     
        JPanel graphingPanel = new JPanel();  
        constraints.anchor = GridBagConstraints.SOUTHWEST;  
        constraints.insets = new Insets(0,0,5,5);  
        constraints.fill = GridBagConstraints.BOTH;  
        addComponent(graphingPanel,6,1,8,13);  
     
        TimeSeries pop = new TimeSeries("Sales", Day.class);
    	 TimeSeries pop2 = new TimeSeries("Purchases", Day.class);
    	 pop2.add(new Day(20, 1, 2004), 200);
    	 pop2.add(new Day(20, 2, 2004), 250);
    	 pop2.add(new Day(20, 3, 2004), 450);
    	 pop2.add(new Day(20, 4, 2004), 475);
    	 pop2.add(new Day(20, 5, 2004), 125);
    	 pop2.add(new Day(20, 6, 2004), 100);
    	 pop2.add(new Day(20, 7, 2004), 400);
    	 pop2.add(new Day(20, 8, 2004), 125);
    	 pop2.add(new Day(20, 9, 2004), 300);
    	 pop2.add(new Day(20, 10, 2004), 223);
    	 pop2.add(new Day(20, 11, 2004), 125);
    	 pop2.add(new Day(20, 12, 2004), 125);
     
    	 pop.add(new Day(10, 1, 2004), 100);
    	 pop.add(new Day(10, 2, 2004), 150);
    	 pop.add(new Day(10, 3, 2004), 250);
    	 pop.add(new Day(10, 4, 2004), 275);
    	 pop.add(new Day(10, 5, 2004), 325);
    	 pop.add(new Day(20, 6, 2004), 57);
    	 pop.add(new Day(20, 7, 2004), 125);
    	 pop.add(new Day(20, 8, 2004), 98);
    	 pop.add(new Day(20, 9, 2004), 200);
    	 pop.add(new Day(20, 10, 2004), 100);
    	 pop.add(new Day(20, 11, 2004), 200);
    	 pop.add(new Day(20, 12, 2004), 100);
     
     
    	 TimeSeriesCollection dataset = new TimeSeriesCollection();
    	 dataset.addSeries(pop2);
    	 dataset.addSeries(pop);
    	 JFreeChart chart = ChartFactory.createTimeSeriesChart(
    	 "Sales and Purchases",
    	 "Date", 
    	 "Quantity",
    	 dataset,
    	 true,
    	 true,
    	 false);
     
     
     
    	ChartPanel chartPanel = new ChartPanel(chart); 	
    	graphingPanel.add(chartPanel);
     
        String [] graphOptions ={"Option 1","Option 2","Option 3"};
     
        cbbGraphs = new JComboBox(graphOptions);  
        constraints.fill = GridBagConstraints.HORIZONTAL;  
        constraints.anchor = GridBagConstraints.EAST;  
        constraints.insets = new Insets(0, 0, 5, 5);  
        addComponent(cbbGraphs, 3, 4,2,1);  
     
     
        cbbGraphs.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent e){  
            	int selection =cbbGraphs.getSelectedIndex();
            	if(selection ==1){ 
            		//Create bar chart
            	    DefaultCategoryDataset data = new DefaultCategoryDataset();
            	    data.setValue(6, "Profit1", "Jane");
            	    data.setValue(3, "Profit2", "Jane");
            	    data.setValue(7, "Profit1", "Tom");
            	    data.setValue(10, "Profit2", "Tom");
            	    data.setValue(8, "Profit1", "Jill");
            	    data.setValue(8, "Profit2", "Jill");
            	    data.setValue(5, "Profit1", "John");
            	    data.setValue(6, "Profit2", "John");
            	    data.setValue(12, "Profit1", "Fred");
            	    data.setValue(5, "Profit2", "Fred");
     
            	 	JFreeChart chart = ChartFactory.createBarChart3D("Comparison between Salesman", 
            	 			 "Salesman", "Value ($)", data, PlotOrientation.VERTICAL, true, true, false );
            		ChartPanel chartPanel2 = new ChartPanel(chart);
     
            	}
     
     
                  }  
     
     
                });  
     
        setContentPane(panelBackground);  
        panelBackground.add(myPanel);  
     
        setVisible(true);  
    }  
     
        //###############################################################################         
     
        //Main Method  
     
        public static void main (String[]args)  
        {  
            Display a = new Display();  
        }  
     
        //###############################################################################         
     
     
        public void addComponent(Component component, int row, int column, int width,int height ){  
            constraints.gridx=column;  
            constraints.gridy=row;  
            constraints.gridwidth=width;  
            constraints.gridheight=height;  
            myPanel.add(component,constraints);  
     
     
     
     
     
        }  
     
     
    }

    So, how would I make it, so that the JComboBox will allow me to switch the chart displayed on the panel. Can it be done?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,399
    My Mood
    Mellow
    Thanks
    83
    Thanked 471 Times in 462 Posts

    Default Re: Switching JFreeCharts on a JPanel using a JComboBox

    Without spending too much time looking at your code, what you describe sounds like a job for CardLayout. I'll spend more time looking at your code and let you know if I have other ideas.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Melodia (September 3rd, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Switching JFreeCharts on a JPanel using a JComboBox

    So, after experimenting, I have done it in two different ways. The first is through the use of CardLayout, which you recommended. It is mostly a result of playing around with the first demo on the Javadocs.

    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PiePlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.data.general.DefaultPieDataset;
    import org.jfree.data.time.Day;
    import org.jfree.data.time.TimeSeries;
    import org.jfree.data.time.TimeSeriesCollection;
     
    public class Display implements ItemListener {
        JPanel cards; 
     
        final static String LINEGRAPHPANEL = "Line graph";
        final static String BARCHARTPANEL = "Bar chart";
        final static String PIECHARTPANEL ="Pie chart";
     
        public void addComponentToPane(Container pane) {
            //Put the JComboBox in a JPanel to get a nicer look.
            JPanel comboBoxPane = new JPanel(); //use FlowLayout
            String comboBoxItems[] = { LINEGRAPHPANEL, BARCHARTPANEL, PIECHARTPANEL};
            JComboBox cb = new JComboBox(comboBoxItems);
            cb.setEditable(false);
            cb.addItemListener(this);
            comboBoxPane.add(cb);
     
            //############################Build first chart###################################
            TimeSeries pop = new TimeSeries("Sales", Day.class);
       	 TimeSeries pop2 = new TimeSeries("Purchases", Day.class);
       	 pop2.add(new Day(20, 1, 2004), 200);
       	 pop2.add(new Day(20, 2, 2004), 250);
       	 pop2.add(new Day(20, 3, 2004), 450);
       	 pop2.add(new Day(20, 4, 2004), 475);
       	 pop2.add(new Day(20, 5, 2004), 125);
       	 pop2.add(new Day(20, 6, 2004), 100);
       	 pop2.add(new Day(20, 7, 2004), 400);
       	 pop2.add(new Day(20, 8, 2004), 125);
       	 pop2.add(new Day(20, 9, 2004), 300);
       	 pop2.add(new Day(20, 10, 2004), 223);
       	 pop2.add(new Day(20, 11, 2004), 125);
       	 pop2.add(new Day(20, 12, 2004), 125);
     
       	 pop.add(new Day(10, 1, 2004), 100);
       	 pop.add(new Day(10, 2, 2004), 150);
       	 pop.add(new Day(10, 3, 2004), 250);
       	 pop.add(new Day(10, 4, 2004), 275);
       	 pop.add(new Day(10, 5, 2004), 325);
       	 pop.add(new Day(20, 6, 2004), 57);
       	 pop.add(new Day(20, 7, 2004), 125);
       	 pop.add(new Day(20, 8, 2004), 98);
       	 pop.add(new Day(20, 9, 2004), 200);
       	 pop.add(new Day(20, 10, 2004), 100);
       	 pop.add(new Day(20, 11, 2004), 200);
       	 pop.add(new Day(20, 12, 2004), 100);
     
     
       	 TimeSeriesCollection dataset = new TimeSeriesCollection();
       	 dataset.addSeries(pop2);
       	 dataset.addSeries(pop);
       	 JFreeChart chart = ChartFactory.createTimeSeriesChart(
       	 "Sales and Purchases",
       	 "Date", 
       	 "Quantity",
       	 dataset,
       	 true,
       	 true,
       	 false);
     
     
     
       	ChartPanel firstChartPanel = new ChartPanel(chart); 	
     
     
          //#############################Build second chart##################################
     
            DefaultCategoryDataset profitAndLossData = new DefaultCategoryDataset();
    		profitAndLossData.setValue(123, "Purchases", "January");
    		profitAndLossData.setValue(213, "Sales", "January");
    		profitAndLossData.setValue(321, "Purchases", "February");
    		profitAndLossData.setValue(23, "Sales", "February");
    		profitAndLossData.setValue(322, "Purchases", "March");
    		profitAndLossData.setValue(232, "Sales", "March");
    		profitAndLossData.setValue(222, "Purchases", "April");
    		profitAndLossData.setValue(222, "Sales", "April");
    		profitAndLossData.setValue(111, "Purchases", "May");
    		profitAndLossData.setValue(222, "Sales", "May");
     
     
    		// create a chart...
    		JFreeChart secondChart = ChartFactory.createBarChart(
    				"Purchases and Sales", "Key", "Amount ()",
    				profitAndLossData, PlotOrientation.VERTICAL, true, true, false);
    		ChartPanel secondChartPanel = new ChartPanel (secondChart);
     
     
    	     //#############################Build third chart##################################
     
    		DefaultPieDataset categoryData = new DefaultPieDataset();
    		categoryData.setValue("capacitors", new Double(100));
    		categoryData.setValue("Resistors", new Double(100));
    		categoryData.setValue("Interface Devices", new Double(
    				40));
    		categoryData
    				.setValue("Transformers", new Double(100));
    		categoryData.setValue("Monitors", new Double(100));
    		categoryData.setValue("Other", new Double(100));
     
    		// create a pie chart
    		secondChart = ChartFactory.createPieChart(
    				"Stock Distribution by category", categoryData, true, true,
    				true);
    		PiePlot categoryPlot = (PiePlot) secondChart.getPlot();
    		categoryPlot.setSimpleLabels(true);
     
    		ChartPanel thirdChartPanel = new ChartPanel(secondChart);
     
     
    		//#####################################################################################
     
            //Create the "cards".
            JPanel card1 = new JPanel();
            JPanel card2 = new JPanel();
            JPanel card3 = new JPanel();
     
            card1.add(firstChartPanel);
            card2.add(secondChartPanel);
            card3.add(thirdChartPanel);
     
     
     
            //Create the panel that contains the "cards".
            cards = new JPanel(new CardLayout());
            cards.add(card1, LINEGRAPHPANEL);
            cards.add(card2, BARCHARTPANEL);
            cards.add(card3,PIECHARTPANEL);
     
            pane.add(comboBoxPane, BorderLayout.PAGE_START);
            pane.add(cards, BorderLayout.CENTER);
        }
     
        public void itemStateChanged(ItemEvent evt) {
            CardLayout cl = (CardLayout)(cards.getLayout());
            cl.show(cards, (String)evt.getItem());
        }
     
     
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("CardLayoutDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            Display demo = new Display();
            demo.addComponentToPane(frame.getContentPane());
     
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            /* Use an appropriate Look and Feel */
            try {
                //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            /* Turn off metal's use of bold fonts */
            UIManager.put("swing.boldMetal", Boolean.FALSE);
     
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

    However, I also discovered a way of doing it while keeping the original GridBagLayout, thus allowing me to keep any JButtons or JLabels in the exact same position (and there were some in the real program, though I've removed them from any of my SSCCEs for the sake of simplicity.

    import java.util.ArrayList;   
    import java.awt.Color;  
    import java.awt.Component;  
    import java.awt.Font;
    import java.awt.GridBagConstraints;  
    import java.awt.GridBagLayout;  
    import java.awt.Insets;  
    import java.awt.event.ActionEvent;  
    import java.awt.event.ActionListener; 
     
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
     
    import javax.swing.JComboBox;  
    import javax.swing.JFrame;  
    import javax.swing.JPanel;    
    import javax.swing.border.EmptyBorder;  
     
     
     
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PiePlot;
    import org.jfree.chart.plot.PlotOrientation;  
    import org.jfree.data.category.DefaultCategoryDataset;  
    import org.jfree.data.general.DefaultPieDataset;
    import org.jfree.data.time.TimeSeries;  
    import org.jfree.data.time.TimeSeriesCollection;  
    import org.jfree.data.time.Day;
     
     
    public class Display extends JFrame {
     
    	public static JPanel graphingPanel;
    	public static ChartPanel chartPanel;
     
    	public static JFreeChart firstChart;
    	public static JFreeChart secondChart;
    	public static JFreeChart thirdChart;
     
    	private JPanel panelBackground;  
        private JPanel myPanel;  
     
        private JComboBox cbbGraphs;  
     
        private GridBagConstraints constraints;  
     
         ArrayList<String> categories = new ArrayList<String>();  
     
     
     
        public Display()   
        {  
            //Frame details  
        super("Title");  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        setBounds(100, 100, 1200, 700);  
        setLocationRelativeTo(null);  
        panelBackground = new JPanel();  
        panelBackground.setBorder(new EmptyBorder(5, 5, 5, 5));  
        setContentPane(panelBackground);  
        panelBackground.setBackground(new Color(20,50,75));  
        GridBagLayout gbl_panelBackground = new GridBagLayout();  
        panelBackground.setLayout(gbl_panelBackground);  
     
        //Panel  
        myPanel = new JPanel();  
        myPanel.setBorder(null);  
        myPanel.setBackground(new Color(240, 255, 255));   
        GridBagLayout gbl_myPanel = new GridBagLayout();  
        gbl_myPanel.columnWidths = new int[]{50, 125, 50, 50, 50, 50, 0,50,50,50,50,50,50,50};  
        gbl_myPanel.rowHeights = new int[]{20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20};  
        myPanel.setLayout(gbl_myPanel);  
     
        //Constraints  
        constraints = new GridBagConstraints();  
        constraints.anchor = GridBagConstraints.EAST;  
        constraints.gridx=0;  
        constraints.gridy=0;  
        panelBackground.add(myPanel,constraints);  
     
        //###############################################################################         
     
         GridBagConstraints gbc_lbl = new GridBagConstraints();  
         gbc_lbl.anchor = GridBagConstraints.NORTHWEST;  
         gbc_lbl.gridx = 0;  
         gbc_lbl.gridy = 0;  
     
     
        //Graphing Panel  
     
        graphingPanel = new JPanel();  
        constraints.anchor = GridBagConstraints.SOUTHWEST;  
        constraints.insets = new Insets(0,0,5,5);  
        constraints.fill = GridBagConstraints.BOTH;  
        addComponent(graphingPanel,6,1,8,13);  
     
        //###############################################Construct first chart####################################################
     
        TimeSeries pop = new TimeSeries("Sales", Day.class);
    	 TimeSeries pop2 = new TimeSeries("Purchases", Day.class);
    	 pop2.add(new Day(20, 1, 2004), 200);
    	 pop2.add(new Day(20, 2, 2004), 250);
    	 pop2.add(new Day(20, 3, 2004), 450);
    	 pop2.add(new Day(20, 4, 2004), 475);
    	 pop2.add(new Day(20, 5, 2004), 125);
    	 pop2.add(new Day(20, 6, 2004), 100);
    	 pop2.add(new Day(20, 7, 2004), 400);
    	 pop2.add(new Day(20, 8, 2004), 125);
    	 pop2.add(new Day(20, 9, 2004), 300);
    	 pop2.add(new Day(20, 10, 2004), 223);
    	 pop2.add(new Day(20, 11, 2004), 125);
    	 pop2.add(new Day(20, 12, 2004), 125);
     
    	 pop.add(new Day(10, 1, 2004), 100);
    	 pop.add(new Day(10, 2, 2004), 150);
    	 pop.add(new Day(10, 3, 2004), 250);
    	 pop.add(new Day(10, 4, 2004), 275);
    	 pop.add(new Day(10, 5, 2004), 325);
    	 pop.add(new Day(20, 6, 2004), 57);
    	 pop.add(new Day(20, 7, 2004), 125);
    	 pop.add(new Day(20, 8, 2004), 98);
    	 pop.add(new Day(20, 9, 2004), 200);
    	 pop.add(new Day(20, 10, 2004), 100);
    	 pop.add(new Day(20, 11, 2004), 200);
    	 pop.add(new Day(20, 12, 2004), 100);
     
     
    	 TimeSeriesCollection dataset = new TimeSeriesCollection();
    	 dataset.addSeries(pop2);
    	 dataset.addSeries(pop);
    	 firstChart = ChartFactory.createTimeSeriesChart(
    	 "Sales and Purchases",
    	 "Date", 
    	 "Quantity",
    	 dataset,
    	 true,
    	 true,
    	 false);
     
    	 chartPanel=new ChartPanel(firstChart);
     
     
    	 //###############################################Construct second chart####################################################
     
    	 DefaultPieDataset categoryData = new DefaultPieDataset();
    		categoryData.setValue("capacitors", new Double(100));
    		categoryData.setValue("Resistors", new Double(100));
    		categoryData.setValue("Interface Devices", new Double(
    				40));
    		categoryData
    				.setValue("Transformers", new Double(100));
    		categoryData.setValue("Monitors", new Double(100));
    		categoryData.setValue("Other", new Double(100));
     
    		// create a pie chart
    		secondChart = ChartFactory.createPieChart(
    				"Stock Distribution by category", categoryData, true, true,
    				true);
    		PiePlot categoryPlot = (PiePlot) secondChart.getPlot();
    		categoryPlot.setSimpleLabels(true);
     
     
    	//###############################################Construct third chart####################################################
     
    		DefaultCategoryDataset profitAndLossData = new DefaultCategoryDataset();
    		profitAndLossData.setValue(123, "Purchases", "January");
    		profitAndLossData.setValue(213, "Sales", "January");
    		profitAndLossData.setValue(321, "Purchases", "February");
    		profitAndLossData.setValue(23, "Sales", "February");
    		profitAndLossData.setValue(322, "Purchases", "March");
    		profitAndLossData.setValue(232, "Sales", "March");
    		profitAndLossData.setValue(222, "Purchases", "April");
    		profitAndLossData.setValue(222, "Sales", "April");
    		profitAndLossData.setValue(111, "Purchases", "May");
    		profitAndLossData.setValue(222, "Sales", "May");
     
     
    		// create a chart...
    		thirdChart = ChartFactory.createBarChart(
    				"Purchases and Sales", "Key", "Amount ()",
    				profitAndLossData, PlotOrientation.VERTICAL, true, true, false);
     
    		// ######################################################################################################
     
     
     
    	graphingPanel.add(chartPanel);
     
        String [] graphOptions ={"Option 1","Option 2","Option 3"};
     
        cbbGraphs = new JComboBox(graphOptions);  
        constraints.fill = GridBagConstraints.HORIZONTAL;  
        constraints.anchor = GridBagConstraints.EAST;  
        constraints.insets = new Insets(0, 0, 5, 5);  
        addComponent(cbbGraphs, 3, 4,2,1);  
     
     
        cbbGraphs.addItemListener(new ItemListener() {
    		@Override
    		public void itemStateChanged(ItemEvent e) {
    			int item = cbbGraphs.getSelectedIndex();
     
    			switch (item) {
    			case 0:
    				graphingPanel.removeAll();
    				graphingPanel.setVisible(false);
    				chartPanel = new ChartPanel(firstChart);
    				graphingPanel.add(chartPanel);
    				graphingPanel.setVisible(true);
    				break;
    			case 1:
    				graphingPanel.removeAll();
    				graphingPanel.setVisible(false);
    				chartPanel = new ChartPanel(secondChart);
    				graphingPanel.add(chartPanel);
    				graphingPanel.setVisible(true);
    				break;
    			case 2:
    				graphingPanel.removeAll();
    				graphingPanel.setVisible(false);
    				chartPanel = new ChartPanel(thirdChart);
    				graphingPanel.add(chartPanel);
    				graphingPanel.setVisible(true);
    				break;
     
    			default:
    				graphingPanel.removeAll();
    				graphingPanel.setVisible(false);
    				//chartPanel = new ChartPanel(salesAndPurchasesChart);
    				graphingPanel.add(chartPanel);
    				graphingPanel.setVisible(true);
    				break;
    			}
     
    		}
    	});
     
        setContentPane(panelBackground);  
        panelBackground.add(myPanel);  
     
        setVisible(true);  
    }  
     
        //###############################################################################         
     
        //Main Method  
     
        public static void main (String[]args)  
        {  
            Display a = new Display();  
        }  
     
        //###############################################################################         
     
     
        public void addComponent(Component component, int row, int column, int width,int height ){  
            constraints.gridx=column;  
            constraints.gridy=row;  
            constraints.gridwidth=width;  
            constraints.gridheight=height;  
            myPanel.add(component,constraints);  
     
     
     
     
     
        }  
     
     
    }

Similar Threads

  1. Switching between classes
    By SACoder in forum Object Oriented Programming
    Replies: 4
    Last Post: July 20th, 2012, 03:17 PM
  2. Add extended JPanel on selected index change of JComboBox
    By mikejr76 in forum AWT / Java Swing
    Replies: 3
    Last Post: February 20th, 2012, 09:32 PM
  3. swing - switching JPanels
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 5th, 2010, 09:18 AM
  4. Plotting w/JFreeCharts
    By Javajava in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 12th, 2010, 10:02 AM
  5. Replies: 4
    Last Post: May 27th, 2008, 01:20 PM

Tags for this Thread