Marcinnnn Marcinnnn - 1 year ago 78
Java Question

JAVA button inside a frame generates chart

it's my first post here but desperate times require desperate moves. My problem is that I've made a JFrame with buttons and others that does my project (every next button shows when the previous one is clicked (code consists on if and else). When the penultimate button (the last one else) is clicked it does the last part od the project and shows, let's call it, "Create Chart" button. What I want do is to have new frame being showed after clicking this button that will show a chart in a new frame. Here's my code (shortened and simplified just to show the idea):

public class clazz extends JFrame
static JButton // my buttons
static JLabel //my labels
static JCheckBox // my checkBoxes
static JTextField // my textfields

public double //declaration of my variables

public clazz()

// created and set up lots of buttons/labels/textfields etc.

button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

// and it continues like that


public void Button1()
public void Button2()

// and continues like that

public static void main(String[] args)
clazz aplication = new clazz();

For the purpose of my question lets say button3 will be the one which creates chart (in another frame). What should I do to get a new frame with a chart based on data genereated by previous buttons. All other topics I saw do the chart using a new class and those which are not are to complicated for me (I'm a fish).

Thanks for every help!


DONE IT! If you know easier ways I am interested.

// the beginning is the same

// to the last button in the constructor (let's say the third one) I've added method chartt()

// here's the method chartt()
public void chartt() {
JFrame frame = new JFrame();

XYSeries DATA = new XYSeries("");
matrix1 = new double[641];
matrix2 = new double[641];
for (int x=0; x<=640; x++)
matrix1[x]= pressure(x*1000); //pressure is defined in another method with argument x
matrix2[x] = x;

for (int y=0;y<=640;y++)
DATA.add(matrix2[y], matrix1[y]);

XYDataset xyDataset = new XYSeriesCollection(DATA);
JFreeChart chart = ChartFactory.createXYLineChart(
"", "", "",
xyDataset, PlotOrientation.VERTICAL, true, true, false);

ChartPanel cp = new ChartPanel(chart)

public Dimension getPreferredSize()
return new Dimension(600, 600);



Good luck to everybody!

Answer Source

What I get from your question is that when you hit the last button, another JFrame(Probably you had already designed it) lets name that XYZ has to be shown. XYZ will contain a chart designed by you.

so on the ActionPerformed event you need to write the following code :

XYZ x = new XYZ();
this.dispose();   // use this line if you wish to close the first frame with buttons.

Hope it helps. If there is something else you're asking about, please mention it in the comments.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download