addKeyListener

A free public discussion forum for the JFreeChart class library.

addKeyListener

Postby Tony Young » Thu Nov 08, 2007 9:48 am

Hi:
how can i add KeyListener to a chartPanel object?
Tony Young
 
Posts: 14
Joined: Fri Nov 02, 2007 8:21 am

Postby Taqua » Thu Nov 08, 2007 1:31 pm

chartPanel.addKeyListener(..)?

ChartPanel is a Swing-Component, so you can do what you could do to all Swing-objects.
Taqua
JFreeReport Project Leader
 
Posts: 698
Joined: Fri Mar 14, 2003 3:34 pm

Postby Tony Young » Fri Nov 09, 2007 2:40 am

Hi : Taqua
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");
          }
   }


where is the problem?
:?: :?: :?:
Tony Young
 
Posts: 14
Joined: Fri Nov 02, 2007 8:21 am

Postby Taqua » Fri Nov 09, 2007 3:53 am

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. ;)
Taqua
JFreeReport Project Leader
 
Posts: 698
Joined: Fri Mar 14, 2003 3:34 pm


Return to JFreeChart - General

Who is online

Users browsing this forum: Bing [Bot], Exabot [Bot], Google [Bot], Majestic-12 [Bot], Yahoo [Bot] and 13 guests