I have a JFrame.
In that JFrame I have a JPanel.
In the JPanel I have JTextFields and a JButton.
I take the values from the JTextFields and then press the JButton.
As a result a chart is generated.
The graph is created in an external panel.
I want it to appear in the same panel as the text fields and the button.
How do I do it?
What I've got:

The structure of my code is as follows:
GuiFrame class creates the frame and has main()
Code: Select all
public class GuiFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createShowGui();
}
});
}
private static void createShowGui() {
System.out.println("created gui on edt? " + SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(250,250);
f.add(new GuiPanel());
f.pack();
f.setVisible(true);
}
}
GuiPanel class has the textfields and button. In actionPerformed method I take the values and draw the graph with
Code: Select all
add(new MyGraph("title"));
Code: Select all
public class GuiPanel extends JPanel implements ActionListener {
JTextField tfx = new JTextField(3);
JTextField tfy = new JTextField(3);
JTextField tfw = new JTextField(3);
JTextField tfh = new JTextField(3);
JButton b = new JButton("Draw");
//GridBagLayout gbl = new GridBagLayout();
//FlowLayout fl = new FlowLayout();
public GuiPanel() {
setBorder(BorderFactory.createLineBorder(Color.black));
add(tfx);
add(tfy);
add(tfw);
add(tfh);
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String temp = tfx.getText();
Global.X1 = Integer.parseInt(temp);
String temp2 = tfy.getText();
Global.Y1 = Integer.parseInt(temp2);
String temp3 = tfw.getText();
Global.W1 = Integer.parseInt(temp3);
String temp4 = tfh.getText();
Global.H1 = Integer.parseInt(temp4);
//MyGraph my = new MyGraph("title");
//my.createDemoPanel();
add(new MyGraph("title"));
}
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
}
Code: Select all
public class MyGraph extends ApplicationFrame {
public MyGraph(String title) {
super(title);
CategoryDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
ChartPanel panel = new ChartPanel(chart);
panel.setPreferredSize(new Dimension(300, 200));
setContentPane(panel);
pack();
RefineryUtilities.centerFrameOnScreen(this);
setVisible(true);
}
public static CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(Global.X1,"series1","category X1");
dataset.addValue(Global.Y1,"series1","category Y1");
dataset.addValue(Global.W1,"series1","category W1");
dataset.addValue(Global.H1,"series1","category H1");
return dataset;
}
public static JFreeChart createChart(CategoryDataset dataset) {
JFreeChart chart = ChartFactory.createLineChart("My Line Chart", "Release", "Count", dataset, PlotOrientation.VERTICAL, true, true, false);
chart.addSubtitle(new TextTitle("Number of releases by count"));
TextTitle text = new TextTitle("Blah blah");
//...
//HERE IS MORE FORMATTING
//...
CategoryAxis axis = plot.getDomainAxis();
axis.setTickLabelsVisible(false);
return chart;
}
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}
}