Solved

JFreeChart. Closing a chart without closing my whole application

Posted on 2006-10-28
16
1,271 Views
Last Modified: 2008-11-16
Hi Experts,

I have developed an application and in my main menu I have a choice to graph data.
I am using JFreeChart library to develop graphs.
Everything is OK. I can get a chart but it is in a separate Chart panel and when I close the ChartPanel it also closes my whole application. What can I do ?

I want to be able to put the graph in my application and be able to close the graph without closinh my application
0
Comment
Question by:blarouche
  • 9
  • 7
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You need to

panelDisplayingFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

0
 

Author Comment

by:blarouche
Comment Utility
CEHJ

Here is part of my code when I create the Chart panel :


final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(800, 400));
setContentPane(chartPanel);


Do I have to include this code ?

chartPanel.setDefaultCloseOperation(ApplicationFrame.DISPOSE_ON_CLOSE);
0
 

Author Comment

by:blarouche
Comment Utility
This is my class declaration. it extends ApllicationFrame :

public class HighLowChartDemo extends ApplicationFrame {
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>
Do I have to include this code ?

chartPanel.setDefaultCloseOperation(ApplicationFrame.DISPOSE_ON_CLOSE);
>>

Just

Do I have to include this code ?

setDefaultCloseOperation(ApplicationFrame.DISPOSE_ON_CLOSE);

by the looks
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Sorry, that was meant to be


>>
Do I have to include this code ?

chartPanel.setDefaultCloseOperation(ApplicationFrame.DISPOSE_ON_CLOSE);
>>

Just

setDefaultCloseOperation(ApplicationFrame.DISPOSE_ON_CLOSE);

by the looks
0
 

Author Comment

by:blarouche
Comment Utility
I have added the code.
I still have the same problem, it closes my whole application
0
 

Author Comment

by:blarouche
Comment Utility
My Listener is in my main application class but my ChartDemo class where I create my ChartPanel is in another class and package:

class MyChartListener extends AbstractAction{
           
                                     
                     //display message dialog when user selects Comments ...
                        public void actionPerformed( ActionEvent event )
                        {
                              
                        final HighLowChartDemo chrt = new HighLowChartDemo("eurodollars","Eurodollars Daily",quotes);
                      chrt.pack();
                      RefineryUtilities.centerFrameOnScreen(chrt);
                      chrt.setVisible(true);
                      setDefaultCloseOperation(ApplicationFrame.DISPOSE_ON_CLOSE);

                           }
                             
                  }// My ChartListener



      public class HighLowChartDemo extends ApplicationFrame {

      protected String symbol = "";    
      protected Price         price;
      protected String configurationFile = "";      
      
      private double[] high;
      private double[] low;
      private double[] close;
      private double[] open;
      private int[] date;      
      private Date[] d;
      private double[] volume;
      public DBQuotes pointer;

      /**
       * Constructor
       * @param symbol
       */
      public HighLowChartDemo(String Symbol, final String title, DBQuotes point){
      
            super(title);
            this.symbol = Symbol;
            this.pointer = point;
            execute();
 
      }


private void execute(){

      
    /**
     * Loads Daily Prices
     */
    price = getPrices(symbol,pointer);        
    if (price.getSize()==0){
        System.out.println("No quotes founds for symbol "+symbol+", system can't works.");
        System.exit(1);
    }

    high = new double[price.getSize()];
    low  = new double[price.getSize()];
    close = new double[price.getSize()];
    open = new double[price.getSize()];
    date = new int[price.getSize()];    
    volume = new double[price.getSize()];  
    d = new Date[price.getSize()];  
   
   
    for (int i=price.getFirst();i<price.getLast()+1;i++){
                      
          high[i] = price.getHigh(i);
          //System.out.println(price.getHigh(i));
          low[i] = price.getLow(i);
          //System.out.println(price.getLow(i));
          close[i] = price.getClose(i);
          //System.out.println(price.getClose(i));
          open[i] = price.getOpen(i);
          //System.out.println(price.getOpen(i));
          date[i] = price.getDate(i);
 
           try{
                 DateFormat sdf = new SimpleDateFormat("yyyyMMdd");      
                 d[i] = sdf.parse(Integer.toString(price.getDate(i)));
                 }catch (ParseException e) {
                 }
          
                volume[i] = price.getVolume(i);      
     
          }
      
      
   
          final DefaultHighLowDataset dataset = new DefaultHighLowDataset("Chart for "+symbol,d,high,low,open,close,volume);
 
          // code for a candlestick type chart
          final JFreeChart chart = createChart(dataset);
          chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);
       
          
          /**code for bar chart only
          final JFreeChart chart = ChartFactory.createHighLowChart("High-Low-Open-Close Demo","Time", "Value",dataset, true);
          final DateAxis axis = (DateAxis) chart.getXYPlot().getDomainAxis();
          axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
          
                    
            }





0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
Comment Utility
Try

chrt.setDefaultCloseOperation(ApplicationFrame.DISPOSE_ON_CLOSE);
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:blarouche
Comment Utility
Same result
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
OK. Try setting it to

chrt.setDefaultCloseOperation(ApplicationFrame.DO_NOTHING_ON_CLOSE);
0
 

Author Comment

by:blarouche
Comment Utility
Maybe I should include the ChartDemo class in my my application class and create the ChartPanel inside that class ?
0
 

Author Comment

by:blarouche
Comment Utility
Same result
0
 

Author Comment

by:blarouche
Comment Utility
CEHJ

I have to leave for a few hours. I'll be back this afternoon.
I would like to solve this problem as soon as possible.
I'll contact you this afternoon.


Thank you
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
I personally would make the container holding the chart a JDialog, not a JFrame
0
 

Author Comment

by:blarouche
Comment Utility
I solved my problem in changing extends AplicationFrame by extends Frame in my ChartDemo class


Thank you
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now