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 4 of 4

Thread: how to using button to change linechart

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to using button to change linechart

    This is code, but how the ability is pressing button"change chart", by at first of figure a get empty of sketch, after becoming to obtain data of curve sketch??

     
    import java.awt.BorderLayout;
     
    import java.awt.Dimension;
    import java.awt.FlowLayout;
     
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
     
    import javax.swing.JButton;
    import javax.swing.JPanel;
     
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.AxisLocation;
    import org.jfree.chart.axis.DateAxis;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.plot.CombinedDomainXYPlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
    import org.jfree.data.time.Day;
    import org.jfree.data.time.Hour;
    import org.jfree.data.time.TimeSeries;
    import org.jfree.data.time.TimeSeriesCollection;
    import org.jfree.data.xy.IntervalXYDataset;
    import org.jfree.data.xy.XYDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;
     
    public class ResponseChart extends ApplicationFrame {
     
      public static ArrayList mlist;
     
      public static ArrayList jlist;
     
      public static ArrayList lrlist;
     
      public static ArrayList bwlist;
     
      public static ArrayList dlist;
     
      public static ArrayList<String> datelist;
     
      public ResponseChart(String s) {
        super(s);
        JPanel content = new JPanel(new FlowLayout());
        JFreeChart jfreechart = createCombinedChart();
        JPanel toppanel = new JPanel(new BorderLayout()) ;
     
        ChartPanel chartpanel = new ChartPanel(jfreechart, true, true, true,
            true, true);
        JButton btn =new JButton("change chart");
     
              toppanel.setSize(400,400);
           toppanel.add(btn);
          content.add(toppanel,BorderLayout.NORTH);
     
        content.add(chartpanel, BorderLayout.SOUTH);
     
        chartpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(content);
      }
     
     
      public static JFreeChart createCombinedChart() {
     
     
        XYDataset xydataset1 = createdelayDataset();
        StandardXYItemRenderer standardxyitemrenderer1 = new StandardXYItemRenderer();
        NumberAxis numberaxis1 = new NumberAxis("price");
        numberaxis1.setAutoRangeIncludesZero(false);
        XYPlot xyplot1 = new XYPlot(xydataset1, null, numberaxis1,
            standardxyitemrenderer1);
        xyplot1.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
     
        XYDataset xydataset2 = createdelayDataset();
        StandardXYItemRenderer standardxyitemrenderer2 = new StandardXYItemRenderer();
        NumberAxis numberaxis2 = new NumberAxis("averagy");
        numberaxis2.setAutoRangeIncludesZero(false);
        XYPlot xyplot2 = new XYPlot(xydataset2, null, numberaxis2,
            standardxyitemrenderer2);
        xyplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
     
     
     
        CombinedDomainXYPlot combineddomainxyplot = new CombinedDomainXYPlot(
            new DateAxis("Year"));
        combineddomainxyplot.setGap(10D);
        combineddomainxyplot.add(xyplot1, 1);
        combineddomainxyplot.add(xyplot2, 1);
     
     
        combineddomainxyplot.setOrientation(PlotOrientation.VERTICAL);
        return new JFreeChart(null,
            JFreeChart.DEFAULT_TITLE_FONT, combineddomainxyplot, true);
      }
     
     
     
     
      private static IntervalXYDataset createDataset1() {
        mlist = jdbcmysql.getmlist(); 
        datelist = jdbcmysql.getdatelist(); //2009-10-26
        //TimeSeries series1 = new TimeSeries("Series 1", Day.class);
        TimeSeries series1 = new TimeSeries("Series 1",Hour.class);  
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(int i =0;i<mlist.size();i++)
        try {
           java.util.Date d  = new Date();
     
          String datestr = datelist.get(i).toString();
           d = formatter.parse(datestr);
          System.out.println("***************"+d  );
          double value = Double.parseDouble(mlist.get(i).toString());
          series1.add(new Hour(formatter.parse(datestr)), value);
     
        } catch (java.text.ParseException e) {
          // TODO 自動產生 catch 區塊
          e.printStackTrace();
        }
     
        return new TimeSeriesCollection(series1);
     
      }
     
      private static IntervalXYDataset createdelayDataset() {
        dlist = jdbcmysql.getdlist(); 
        datelist = jdbcmysql.getdatelist(); //2009-10-26
        //TimeSeries series1 = new TimeSeries("Series 1", Day.class);
        TimeSeries series1 = new TimeSeries("Series 1",Hour.class);  
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(int i =0;i<dlist.size();i++)
        try {
           java.util.Date d  = new Date();
     
          String datestr = datelist.get(i).toString();
           d = formatter.parse(datestr);
          System.out.println("***************"+d  );
          double value = Double.parseDouble(dlist.get(i).toString());
          series1.add(new Hour(formatter.parse(datestr)), value);
     
        } catch (java.text.ParseException e) {
          // TODO 自動產生 catch 區塊
          e.printStackTrace();
        }
     
        return new TimeSeriesCollection(series1);
     
      }
     
      public static void main(String args[]) {
         jdbcmysql db = new jdbcmysql();
     
         db.SelectTable();
        ResponseChart combinedxyplotdemo1 = new ResponseChart(
            "CombinedDomainXYPlot   Demo");
        combinedxyplotdemo1.pack();
        RefineryUtilities.centerFrameOnScreen(combinedxyplotdemo1);
        combinedxyplotdemo1.setVisible(true);
      }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how to using button to change linechart

    If I understand your question correctly, you're JButton is not working? In your ResponseChart constructor you do not seem to have added a listener to the JButton. See How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners) and http://www.javaprogrammingforums.com...ava-swing.html

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to using button to change linechart

    hi
    i have modify my code as follows :
    import java.awt.BorderLayout;
     
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
     
    import javax.swing.JButton;
    import javax.swing.JPanel;
     
     
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.AxisLocation;
    import org.jfree.chart.axis.DateAxis;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.plot.CombinedDomainXYPlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
    import org.jfree.data.time.Day;
    import org.jfree.data.time.Hour;
    import org.jfree.data.time.TimeSeries;
    import org.jfree.data.time.TimeSeriesCollection;
    import org.jfree.data.xy.IntervalXYDataset;
    import org.jfree.data.xy.XYDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;
     
    public class ResponseChart extends ApplicationFrame {
     
    	public static ArrayList mlist;
     
    	public static ArrayList jlist;
     
    	public static ArrayList lrlist;
     
    	public static ArrayList bwlist;
     
    	public static ArrayList dlist;
     
    	public static ArrayList<String> datelist;
     
     
    	public ResponseChart(String s) {
    		super(s);
    		JPanel content = new JPanel(new FlowLayout());
    		  JFreeChart jfreechart=createCombinedChart(); 
    		JPanel toppanel = new JPanel(new BorderLayout()) ;
     
    		ChartPanel chartpanel = new ChartPanel(jfreechart, true, true, true,
    				true, true);
    		JButton btn =new JButton("change chart");
    		btn.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e) {
           		 jdbcmysql db = new jdbcmysql();
     
        		 db.SelectTable();
                	JFreeChart jfreechart = createCombinedChart();
                	ChartPanel	chartpanel = new ChartPanel(jfreechart, true, true, true,
            				true, true);
    			System.out.println("You clicked the button");
    		}
    	}); 
     
     
     
    	        toppanel.setSize(400,400);
    	     toppanel.add(btn);
    			content.add(toppanel,BorderLayout.NORTH);
     
    		content.add(chartpanel, BorderLayout.SOUTH);
     
    		chartpanel.setPreferredSize(new Dimension(500, 270));
    		setContentPane(content);
    	}
     
     
     
    	public static JFreeChart createCombinedChart() {
     
     
    		XYDataset xydataset1 = createdelayDataset();
    		StandardXYItemRenderer standardxyitemrenderer1 = new StandardXYItemRenderer();
    		NumberAxis numberaxis1 = new NumberAxis("price");
    		numberaxis1.setAutoRangeIncludesZero(false);
    		XYPlot xyplot1 = new XYPlot(xydataset1, null, numberaxis1,
    				standardxyitemrenderer1);
    		xyplot1.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
     
    		XYDataset xydataset2 = createdelayDataset();
    		StandardXYItemRenderer standardxyitemrenderer2 = new StandardXYItemRenderer();
    		NumberAxis numberaxis2 = new NumberAxis("averagy");
    		numberaxis2.setAutoRangeIncludesZero(false);
    		XYPlot xyplot2 = new XYPlot(xydataset2, null, numberaxis2,
    				standardxyitemrenderer2);
    		xyplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
     
     
     
    		CombinedDomainXYPlot combineddomainxyplot = new CombinedDomainXYPlot(
    				new DateAxis("Year"));
    		combineddomainxyplot.setGap(10D);
    		combineddomainxyplot.add(xyplot1, 1);
    		combineddomainxyplot.add(xyplot2, 1);
     
     
    		combineddomainxyplot.setOrientation(PlotOrientation.VERTICAL);
    		return new JFreeChart(null,
    				JFreeChart.DEFAULT_TITLE_FONT, combineddomainxyplot, true);
    	}
     
     
     
     
    	private static IntervalXYDataset createDataset1() {
    		mlist = jdbcmysql.getmlist(); 
    		datelist = jdbcmysql.getdatelist(); //2009-10-26
    		//TimeSeries series1 = new TimeSeries("Series 1", Day.class);
    		TimeSeries series1 = new TimeSeries("Series 1",Hour.class);  
    		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		for(int i =0;i<mlist.size();i++)
    		try {
    			 java.util.Date d  = new Date();
     
    			String datestr = datelist.get(i).toString();
    			 d = formatter.parse(datestr);
    			System.out.println("***************"+d  );
    			double value = Double.parseDouble(mlist.get(i).toString());
    			series1.add(new Hour(formatter.parse(datestr)), value);
     
    		} catch (java.text.ParseException e) {
    			// TODO 自動產生 catch 區塊
    			e.printStackTrace();
    		}
     
    		return new TimeSeriesCollection(series1);
     
    	}
     
    	private static IntervalXYDataset createdelayDataset() {
    		dlist = jdbcmysql.getdlist(); 
    		datelist = jdbcmysql.getdatelist(); //2009-10-26
    		//TimeSeries series1 = new TimeSeries("Series 1", Day.class);
    		TimeSeries series1 = new TimeSeries("Series 1",Hour.class);  
    		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		for(int i =0;i<dlist.size();i++)
    		try {
    			 java.util.Date d  = new Date();
     
    			String datestr = datelist.get(i).toString();
    			 d = formatter.parse(datestr);
    			System.out.println("***************"+d  );
    			double value = Double.parseDouble(dlist.get(i).toString());
    			series1.add(new Hour(formatter.parse(datestr)), value);
     
    		} catch (java.text.ParseException e) {
    			// TODO 自動產生 catch 區塊
    			e.printStackTrace();
    		}
     
    		return new TimeSeriesCollection(series1);
     
    	}
     
     
     
    	public static void main(String args[]) {
     
    		ResponseChart combinedxyplotdemo1 = new ResponseChart(
    				"CombinedDomainXYPlot   Demo");
    		combinedxyplotdemo1.pack();
    		RefineryUtilities.centerFrameOnScreen(combinedxyplotdemo1);
    		combinedxyplotdemo1.setVisible(true);
    	}
    }

    but still can't work , do you have any idea about it ?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how to using button to change linechart

    Does your button fire (eg does it print out your debugging "You clicked the button")? My guess is that it does, but you create a new chartPanel without adding it to the GUI (and I'm also unclear of the changes you make, but thats beside the point). Solution 1 would be to not create a whole new chartpanel but rather just update the old one upon button click. Solution 2 would be to create the new chartpanel as this code demonstrates, and add it to the gui (make sure to call validate() on your frame to update the changes).

Similar Threads

  1. Want to move my button away from center.
    By Fendaril in forum AWT / Java Swing
    Replies: 2
    Last Post: November 6th, 2009, 10:05 PM
  2. [SOLVED] Change the size of an image
    By subhvi in forum Algorithms & Recursion
    Replies: 4
    Last Post: August 23rd, 2009, 11:44 PM
  3. [SOLVED] Implementing push button
    By IDK12 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 10th, 2009, 10:13 AM
  4. Change JFrame components problem
    By bruno88 in forum AWT / Java Swing
    Replies: 0
    Last Post: June 30th, 2009, 01:25 PM
  5. How to upload a file by clicking a link instead of button?
    By raghuprasad in forum Java Theory & Questions
    Replies: 2
    Last Post: May 3rd, 2009, 05:21 AM