Is there a way to integrate Jfreechart with drop-down menu? I want to update the chart depending on the user input. What other options do we have to do this?
-
You need to specify what you mean with "integrate jfreechart with drop-down". You can display a JFreeChart chart in a JFrame, and a JFrame can have a drop down menu. It is possible to have dynamic data in a chart, you just need to update the dataset based on the user input. – Jes Aug 22 '11 at 11:15
-
There's an example in [How can I update a JFreeChart's appearance after it's been made visible?](http://stackoverflow.com/questions/5522575/how-can-i-update-a-jfreecharts-appearance-after-its-been-made-visible). – trashgod Aug 22 '11 at 15:43
1 Answers
Assuming you mean when the user selects a menu item, absolutely. You can modify your chart anytime some event happens, you just have to be listening for it. In your case it might look something like this.
http://www.java2s.com/Tutorial/Java/0240__Swing/ListeningtoJMenuItemEventswithanActionListener.htm
class MenuActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
updateMyChart();
}
}
void updateMyChart()
{
// get your plot, and add/remove/modify data series, annotations,
// markers, axes, etc. based off the state of your application.
}
If you need to do an expensive operation to refresh your chart, like a long running DB query, then you'll probably want to create and start a background thread for it to keep your UI responsive. And you'll need to be mindful about what you're doing and whether you're operating in the main event dispatching thread or the background thread. Bad things can happen if you're trying to do GUI stuff in the background thread but you should be able to safely modify the state of your chart.

- 941
- 12
- 33