How to make a chart appear in a GUI (not in external panel)

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
cka
Posts: 3
Joined: Thu Jul 15, 2010 2:53 pm
antibot: No, of course not.

How to make a chart appear in a GUI (not in external panel)

Post by cka » Thu Jul 15, 2010 3:04 pm

Hi,
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:
Image

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);
	}
}
MyGraph class creates a chart.

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);
	}
}
Last edited by cka on Fri Jul 16, 2010 11:29 am, edited 1 time in total.

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: How to make a chart appear in a GUI (not in external panel)

Post by skunk » Thu Jul 15, 2010 5:07 pm

Modify MyGraph so it doesn't extend ApplicationFrame -- just add the created ChartPanel to your GuiPanel

cka
Posts: 3
Joined: Thu Jul 15, 2010 2:53 pm
antibot: No, of course not.

Re: How to make a chart appear in a GUI (not in external panel)

Post by cka » Fri Jul 16, 2010 11:24 am

I have done what you've said.
But it doesn't seem to work (unless I have misunderstood and done something stupid).

I've tried the following:
- removed extends Application Frame
- instead I extended as JFrame
- changed and extended as JPanel
- removed extends
In each case I have tried to modify the constructor and/or createDemoPanel() method in MyGraph class.
I also tried to add the ChartPanel to JPanel in many different ways.

However I am still failing to achieve what I need
i.e. after I enter values in the text fields and press the button, to make that chart appear in the space beneath them.

Could you please elaborate on your solution or maybe point me to a specific example?

pooo
Posts: 9
Joined: Tue Jul 13, 2010 2:04 pm
antibot: No, of course not.

Re: How to make a chart appear in a GUI (not in external panel)

Post by pooo » Fri Jul 16, 2010 2:53 pm

To make the chart appear beneath the buttons you need a LayoutManager.

What I suggest you to do :

Create 3 different panels : one for the buttons, the 2nd for the chart, and the other to wrap them.
The third Panel needs to display its elements vertically, thats why you need to specify a LayoutManager. BoxLayout does the job.

1rst Panel for the buttons

Code: Select all

JPanel buttonsPanel = new Jpanel();
//Add your buttons to this panel
2nd panel for the chart

Code: Select all

ChartPanel chartPanel = new ChartPanel(yourChart);
3rd panel to wrap the 2 other

Code: Select all

JPanel mainPanel = new JPanel();
//Set the layout
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
//Add the other panels
mainPanel.add(buttonsPanel);
mainPanel.add(chartPanel);
Add your main panel to a JFrame

Code: Select all

frame.add(mainPanel);

cka
Posts: 3
Joined: Thu Jul 15, 2010 2:53 pm
antibot: No, of course not.

Re: How to make a chart appear in a GUI (not in external panel)

Post by cka » Fri Jul 16, 2010 4:10 pm

I've done it. Gives me some errors but at least it works. Will deal with the errors later.
EDIT: now it works :)

Thanks for the detailed instructions pooo.

For anyone that needs something similar:
after a veeery long time I have managed to find a great example: http://www.java2s.com/Code/Java/Chart/J ... tDemo1.htm.

Locked