adding shortcut keys to chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Fred
Posts: 38
Joined: Sat Dec 12, 2015 4:57 pm
antibot: No, of course not.

adding shortcut keys to chart

Post by Fred » Mon Jan 04, 2016 5:08 am

Hello,

I want to be able to hit a key (after identifying the dataset/series/item) from which the key
will make invisible/visible the dataset/series/item (dependent upon the key).

I found some code that extends the ChartComposite class but I'm having trouble with the SWT
package (version 14) that should at least indicate which key has been pressed but I'm having issues
with the package. First the code below

Code: Select all

import java.awt.event.KeyListener;

import org.jfree.chart.JFreeChart;
import org.jfree.experimental.chart.swt.ChartComposite;

public class MyChartComposite extends ChartComposite implements KeyListener
{

    public MyChartComposite(Composite comp, int style, JFreeChart chart, 
                                                      boolean useBuffer){
        super(comp, style, chart, useBuffer);
        addSWTListener(this);
    }   

    @Override
    public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
        System.out.print("Key released: " + e.character);

    }

    @Override
    public void keyReleased(org.eclipse.swt.events.KeyEvent e) {
        System.out.print("Key pressed: " + e.character);
    }

At the class definition, the "ChartComposite" is in error because
The type org.eclipse.swt.events.PaintListener cannot be resolved. It is indirectly referenced from required .class files.
I have the jfreechart-1.0.14-swt.jar included on the Java Build Path (Eclipse Luna).

Fred
Posts: 38
Joined: Sat Dec 12, 2015 4:57 pm
antibot: No, of course not.

Re: adding shortcut keys to chart

Post by Fred » Mon Jan 04, 2016 6:38 pm

Perhaps a better question or focus should be on the "ChartComposite" class and what package/location it is now in.
Does anyone know the package from which I can download it?

Fred
Posts: 38
Joined: Sat Dec 12, 2015 4:57 pm
antibot: No, of course not.

Re: adding shortcut keys to chart

Post by Fred » Mon Jan 04, 2016 7:17 pm

I have my solution.
For those looking to add a "key listener" to your chart here is how I did it:

Code: Select all

        frame = new JFrame("TESTING keyListener");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(chartPanel);

        frame.addKeyListener(new KeyListener() { // KEYLISTENER NOW WORKS :(
            @Override
            public void keyTyped(KeyEvent ke) {}

            @Override
            public void keyPressed(KeyEvent ke) {
                System.out.println("You pressed " + ke.getKeyChar());
            }

            @Override
            public void keyReleased(KeyEvent ke) {
            	System.out.println(ke.getKeyChar()+" released..");
            }
        });
        
        frame.add(chartPanel, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);


Locked