Hi:
how can i add KeyListener to a chartPanel object?
addKeyListener
-
- Posts: 14
- Joined: Fri Nov 02, 2007 8:21 am
Hi : Taqua
Thanks for your help. I've tried several times, but it didn't work. Here is my codes:
where is the problem?

Thanks for your help. I've tried several times, but it didn't work. Here is my codes:
Code: Select all
public Wheeltest(String s)
{
super(s);
OHLCDataset ohlcdataset = createDataset();
JFreeChart jfreechart = createChart(ohlcdataset);
jfreechart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);
ChartPanelMe chartpanel = new ChartPanelMe(jfreechart);
chartpanel.requestFocus();//set focus
chartpanel.addKeyListener(new KeyHandle());//add listener
chartpanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartpanel);
}
private class KeyHandle implements KeyListener{
public void keyPressed(KeyEvent e){
System.out.println("keyPressed");
}
public void keyReleased(KeyEvent e) {
System.out.println("keyReleased");
}
public void keyTyped(KeyEvent e) {
System.out.println("keyTyped");
}
}



Well, Swing and AWT will not post KeyEvents to your ChartPanel if the ChartPanel does not have the focus. I'm quite sure the ChartPanel is not able to receive the focus bydefault.
I would suggest you go to the bookstore next door, grab a Swing/Java book, read it, and this way get a feeling on how Swing and AWT work. It will greatly help you to work with JFreeChart or any other Swing application.
I would suggest you go to the bookstore next door, grab a Swing/Java book, read it, and this way get a feeling on how Swing and AWT work. It will greatly help you to work with JFreeChart or any other Swing application.
