Is there a way to integrate Jfreechart with drop-down menu? I want to upd开发者_如何学编程ate the chart depending on the user input. What other options do we have to do this?
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.
精彩评论