Solved

JFreeChart. Closing a chart without closing my whole application

Posted on 2006-10-28
16
1,307 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
ID: 17825284
You need to

panelDisplayingFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

0
 

Author Comment

by:blarouche
ID: 17825295
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
ID: 17825301
This is my class declaration. it extends ApllicationFrame :

public class HighLowChartDemo extends ApplicationFrame {
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17825303
>>
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
ID: 17825306
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
ID: 17825323
I have added the code.
I still have the same problem, it closes my whole application
0
 

Author Comment

by:blarouche
ID: 17825337
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
ID: 17825346
Try

chrt.setDefaultCloseOperation(ApplicationFrame.DISPOSE_ON_CLOSE);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:blarouche
ID: 17825351
Same result
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17825358
OK. Try setting it to

chrt.setDefaultCloseOperation(ApplicationFrame.DO_NOTHING_ON_CLOSE);
0
 

Author Comment

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

Author Comment

by:blarouche
ID: 17825376
Same result
0
 

Author Comment

by:blarouche
ID: 17825384
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
ID: 17825414
I personally would make the container holding the chart a JDialog, not a JFrame
0
 

Author Comment

by:blarouche
ID: 17825813
I solved my problem in changing extends AplicationFrame by extends Frame in my ChartDemo class


Thank you
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17825855
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

929 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

11 Experts available now in Live!

Get 1:1 Help Now