jFreeChart in Applet window with NetBeans


N

NickPick

I'm having trouble putting a jFreeChart into an applet window. I'm
using Netbeans to create an interface in my applet. Separately I'm
using a class called RandomWalk which opens a jFreeChart. How can I
bring this jFreeChart not to open a separate window but to simply
appear in the original Applet window? Is there a way of configuring
this in NetBeans.

The code of the RandomWalk class that opens the window that it
shouldn't is as follows:

JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart);
getContentPane().add(chartPanel);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);

and this is the automatically generated part from NetBeans which
creates the original Applet window with some buttons and menubars
where I'd also like the JFreeChart to appear in. How do I have to
modify to code above?

private void initComponents() {

jDialog1 = new javax.swing.JDialog();
jFrame1 = new javax.swing.JFrame();
jButton1 = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();

javax.swing.GroupLayout jDialog1Layout = new
javax.swing.GroupLayout(jDialog1.getContentPane());
jDialog1.getContentPane().setLayout(jDialog1Layout);
jDialog1Layout.setHorizontalGroup(
jDialog1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jDialog1Layout.setVerticalGroup(
jDialog1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);

javax.swing.GroupLayout jFrame1Layout = new
javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);

jButton1.setText("Recalculate");
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 361, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 196, Short.MAX_VALUE)
);

jMenu1.setText("File");
jMenuBar1.add(jMenu1);

jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout
(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(21, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(159, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(154, 154, 154))
);
layout.setVerticalGroup(
layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap
(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35,
Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(21, 21, 21))
);
}// </editor-fold>

many thanks
 
Ad

Advertisements

J

John B. Matthews

[...]
The code of the RandomWalk class that opens the window that it
shouldn't is as follows:

JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart);
getContentPane().add(chartPanel);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);

This seems unlikely, as suggested by the examples cited in a previous
thread on this topic:

<http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thre
ad/a9043d02fee7671e>
and this is the automatically generated part from NetBeans which
creates the original Applet window with some buttons and menubars
where I'd also like the JFreeChart to appear in. How do I have to
modify to code above?

private void initComponents() {

jDialog1 = new javax.swing.JDialog();
jFrame1 = new javax.swing.JFrame();
[...]

I see two top-level containers, which may affect your result:

http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html

A more carefully formatted example <http://pscode.org/sscce.html> might
be more legible, although machine generated code is meant for machines.
 
A

Arne Vajhøj

NickPick said:
I'm having trouble putting a jFreeChart into an applet window. I'm
using Netbeans to create an interface in my applet. Separately I'm
using a class called RandomWalk which opens a jFreeChart. How can I
bring this jFreeChart not to open a separate window but to simply
appear in the original Applet window? Is there a way of configuring
this in NetBeans.

We already gave advice in the other thread.

If we are to explain why it is not working for you, then we
need a complete example showing the problem.

Arne
 
N

NickPick

We already gave advice in the other thread.

If we are to explain why it is not working for you, then we
need a complete example showing the problem.

Arne

Many thanks! Here is the full example: There are two classes:

package javaapplication9;

import org.jfree.ui.RefineryUtilities;

public class NewJApplet extends javax.swing.JApplet {
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void start()
{

}

/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jDialog1 = new javax.swing.JDialog();
jFrame1 = new javax.swing.JFrame();
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();

javax.swing.GroupLayout jDialog1Layout = new
javax.swing.GroupLayout(jDialog1.getContentPane());
jDialog1.getContentPane().setLayout(jDialog1Layout);
jDialog1Layout.setHorizontalGroup(
jDialog1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jDialog1Layout.setVerticalGroup(
jDialog1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);

javax.swing.GroupLayout jFrame1Layout = new
javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);

javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 243, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 160, Short.MAX_VALUE)
);

jButton1.setText("Recalculate");
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout
(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(56, 56, 56)
.addComponent(jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE, 164,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(703, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE, 33,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(166, Short.MAX_VALUE))
);
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
RandomWalk demo = new RandomWalk("Random Walk");
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}


// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JDialog jDialog1;
private javax.swing.JFrame jFrame1;
private javax.swing.JPanel jPanel1;
// End of variables declaration

}



--------------------------------




package javaapplication9;

import java.awt.Color;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;

public class RandomWalk extends ApplicationFrame {

public RandomWalk(String title) {
super(title);
XYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart);
getContentPane().add(chartPanel);


//chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
//setContentPane(chartPanel);
}
/**
* Creates a sample dataset.
*
* @return a sample dataset.
*/
private static XYDataset createDataset() {
double r;
double[] p = new double[1001];
p[0]=10;
XYSeries series1 = new XYSeries("Share Price");
for (int x = 0; x < 1000; x++) {
r=Math.random()*2-1;
p[x+1]=p[x]+r;
System.out.println (p[x]);
series1.add(x, p[x+1]);
}
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);


return dataset;
}
/**
* Creates a chart.
*
* @param dataset the data for the chart.
*
* @return a chart.
*/
private static JFreeChart createChart(XYDataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createXYLineChart(
"Ramdom Walk", // chart title
"Days", // x axis label
"Price", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
false, // include legend
true, // tooltips
false // urls
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
XYLineAndShapeRenderer renderer
= (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(true);
renderer.setShapesFilled(true);
// change the auto tick unit selection to integer units only...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}
}
 
A

Arne Vajhøj

package javaapplication9;

import org.jfree.ui.RefineryUtilities;

public class NewJApplet extends javax.swing.JApplet {
private void initComponents() {

jDialog1 = new javax.swing.JDialog();
jFrame1 = new javax.swing.JFrame();

A new JFrame does not sound good if you do not want a new frame.
public class RandomWalk extends ApplicationFrame {

public RandomWalk(String title) {
super(title);
XYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart);
getContentPane().add(chartPanel);

This getContentPane() is not the applets getContentPane().

It need to be the applets getContentPane() or some other
JPanel in the applet.

Arne
 
Ad

Advertisements

J

John B. Matthews

[...]
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JDialog jDialog1;
private javax.swing.JFrame jFrame1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
[...]

You've got three top-level containers: JDialog, JFrame and JApplet. One
works fine:

public class NewJApplet extends JApplet {

public void init() {
this.add(RandomWalk.createDemoPanel());
}
}

JApplet has a default BorderLayout. You can add other components to its
various regions:

public class RandomWalk extends ApplicationFrame {

Also a JFrame.

[...]
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top