Marcinnnn Marcinnnn - 1 month ago 14
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()
{
setSize(1600,900);
setTitle("Project");
setLayout(null);

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

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

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


// and it continues like that

}

public void Button1()
{
//sth
}
public void Button2()
{
//sth
}

// and continues like that



public static void main(String[] args)
{
clazz aplication = new clazz();
aplication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aplication.setVisible(true);
}
}


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!

EDIT

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);
}
};

cp.setMouseWheelEnabled(true);
add(cp);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
frame.add(cp);
frame.setVisible(true);

}


Good luck to everybody!

Answer

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();
x.setVisible(true);
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.

Comments