addKeyListener

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Tony Young
Posts: 14
Joined: Fri Nov 02, 2007 8:21 am

addKeyListener

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

Hi:
how can i add KeyListener to a chartPanel object?

Taqua
JFreeReport Project Leader
Posts: 698
Joined: Fri Mar 14, 2003 3:34 pm
Contact:

Post by 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.

Tony Young
Posts: 14
Joined: Fri Nov 02, 2007 8:21 am

Post by 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?
:?: :?: :?:

Taqua
JFreeReport Project Leader
Posts: 698
Joined: Fri Mar 14, 2003 3:34 pm
Contact:

Post by 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. ;)

Locked