X

Geertjan's Blog

  • October 16, 2009

The Desktop in a Mobile World

Geertjan Wielenga
Product Manager
What I find very interesting about the NetBeans Zone article Agricultural Management on the NetBeans Platform is the workflow of the FarmLinx Software Suite. First, agri-consultants walk around a farm, with a mobile phone in their hand. They record the number of sheep, or whatever, in their phone, and later they download all their data onto their laptops where further processing takes place... in a desktop application. Obviously. Since they're not necessarily on-line in the first place.

The shortsighted perspective of the web being the only place of interest for application development is proven yet again. Another scenario might be where you're polling people in the street about their prediction of who is going to win an election (in this case, below, Tom, Dick, or Harry). Afterwards, you download all your data from your phone and then compare the result in a graph for further analysis:

The above scenario enabled me to use the brilliant JFreeChart for the first time, in the context of a NetBeans Platform application.

In the application above, I have 5 modules. One module for each of the three windows you see, one module for the domain object (a "Vote" object, with the voter's name, and an int for each of "predictionForTom", "predictionForDick", and "predictionForHarry"), as well as a module that contains the two JARs from the JFreeChart distribution (jfreechart-1.0.13.jar and jcommon-1.0.16.jar).

Whenever OK is clicked in the window on the left, a "Vote" object is added to the context of the window. The other two modules are listening to that context and whenever a new "Vote" object is added there, something changes within each of those two other modules. In the case of the module providing the middle window, the JFreeChart implementation is updated (by recalculating the total prediction percentages), while the window on the right has a new voter added to the list.

Only the center window has a dependency on the JFreeChart module. Here's all the relevant code, in the middle window's definition:

public final class ChartTopComponent extends TopComponent implements LookupListener {
private static ChartTopComponent instance;
private static final String PREFERRED_ID = "ChartTopComponent";
private Result<Vote> res;
DefaultPieDataset dataSet = new DefaultPieDataset();
public ChartTopComponent() {
initComponents();
setName(NbBundle.getMessage(ChartTopComponent.class, "CTL_ChartTopComponent"));
setToolTipText(NbBundle.getMessage(ChartTopComponent.class, "HINT_ChartTopComponent"));
PieDataset group = dataSet;
JFreeChart chart = createChart(group, "Combined Likelihood of Winning");
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
add(chartPanel, BorderLayout.CENTER);
}
private JFreeChart createChart(PieDataset dataset, String title) {
JFreeChart chart = ChartFactory.createPieChart3D(
title, // chart title
dataset, // data
true, // include legend
true,
false);
PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setStartAngle(290);
plot.setDirection(Rotation.CLOCKWISE);
plot.setForegroundAlpha(0.5f);
return chart;
}
@Override
public void resultChanged(LookupEvent ev) {
Collection<? extends Vote> coll = res.allInstances();
TotalVote total = new TotalVote();
for (Vote vote : coll) {
total.setTom(vote.getTom());
total.setDick(vote.getDick());
total.setHarry(vote.getHarry());
}
dataSet.setValue("Tom", total.getTom());
dataSet.setValue("Dick", total.getDick());
dataSet.setValue("Harry", total.getHarry());
}
@Override
public void componentOpened() {
res = WindowManager.getDefault().findTopComponent("VoteTopComponent").getLookup().
lookupResult(Vote.class);
res.addLookupListener(this);
resultChanged(new LookupEvent(res));
}
...
...
...

And that's a simple example of how to integrate JFreeChart in a NetBeans Platform application!

Join the discussion

Comments ( 4 )
  • Bernd Ruehlicke Friday, October 16, 2009

    ... and if you use Visual Library you can add a JScrollPane and embed your JFreeChart Panel into a Visual Library ComponentWidget and more or less for free you have move and resize actions on you JFreeChart panel.


  • Peter Rogge Saturday, October 17, 2009

    Super, that's really easy to integrate JFreeChart in NetBeans.

    In my new program "ABC-List" (http://kenai.com/projects/abclist) I will also integrate it to compile statistics from the exercises. Thanks for the info!!


  • Andrew Perry Monday, October 19, 2009

    Thanks Geertjan. That's great.

    I have been working with JFreeChart for a few years and was trying to work out whether I knew enough to move it into the NB Platform.

    Is there any value in using the Orson package from JFreeChart and Matisse for more complex layouts or is it just as easy to put separate charts into separate TopComponents/Modules and use the Windowing API to help with complex layouts?


  • Andrew Perry Tuesday, December 1, 2009

    Just in case there are other platform beginners like me reading this.

    To get the JFreeChart chart to appear in the top component you must set the layout to Border. One way to do this is in the Navigator.

    If you don't you just get an empty window and nothing to tell you why.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha