Newer SWT implementations are beginning to provide very rough wrappers around a java.awt.Panel that allow older AWT/Swing components to be reused inside of SWT applications. Currently only Win32 platforms are supported, although I imagine that other ports aren't far off.
Here's a quick and dirty example of how to use JFreeChart "natively" (without converting Images, etc.) inside of your Win32 SWT application. This has been tested on the Eclipse 3.0M1 SWT drop (build 3007). This WILL NOT work with older 2.x SWT drops.
0) Take a look at
Code: Select all
org.eclipse.swt.internal.SWT_AWT
Code: Select all
new_Panel
1) Create a simple SWT Composite that will contain your AWT/Swing components (in this case a ChartPanel). Align the Composite using your usual SWT layout managers.
2) Create a JFreeChart based on your data using the usual ChartFactory methods.
3) Create a new java.awt.Panel that is backed by your SWT Composite. Optionally you may wish to set the background color to that of the composite and add a layout manager if you want to have several JFreeChart's in the same Composite, or you want the chart to resize itself based on the size of the SWT Composite.
4) Create ChartPanel's and add them to your java.awt.Panel that you obtained from SWT_AWT in step 3.
5) Sit back and enjoy!
Sounds easy, doesn't it? Here's the code (tested using SWT 3.0M1, JFreeChart 0.9.8, JCommon 0.8.3, J2SDK 1.4.2 on Win32 [Win2k]).
Code: Select all
/// STEP 1 ////////////////////////////////////////////////
// create a composite suitable for displaying all of our chart data
Composite chartComposite = new Composite(/* options */);
chartComposite.setLayoutData(/* set the layout options */);
/// STEP 2 ////////////////////////////////////////////////
// create the dataset for the pie chart of the distribution
DefaultPieDataset pieData = new DefaultPieDataset();
/* populate the pieData here */
JFreeChart chart = ChartFactory.createPie3DChart(/* some options */);
// OPTIONAL: tinkering with plot options here
/// STEP 3 ////////////////////////////////////////////////
// Grab the background color from the SWT Composite
// so our AWT panel "matches" the SWT Composite
Color backgroundColor = chartComposite.getBackground();
// create the panel
Panel chartPanel = SWT_AWT.new_Panel(chartComposite);
// set the panel's background (defaults to pure white)
chartPanel.setBackground(new java.awt.Color(backgroundColor.getRed(),
backgroundColor.getGreen(),
backgroundColor.getBlue()));
// set the AWT layout manager
chartPanel.setLayout(/* some AWT/Swing layout manager like BoxLayout */);
/// STEP 4 ////////////////////////////////////////////////
ChartPanel jfreeChartPanel = new ChartPanel(chart);
chartPanel.add(jfreeChartPanel);
gashalot <at> enkia.com