Question

I tried simple code with JFreeChart and when I pressed "zoom" button, I want to cancel zoom with "escape" from keyboard, but KeyListener not works. Where is the problem? I also tried to add KeyListener to chartPanel and panel and it also not works.

package test1;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.*;
import org.jfree.data.xy.*;

public class Test1 {

    public static final String Title = "JGrapher";
    static JFrame frame;
    static double minX = -0.1, maxX = 1.1, minY, maxY;
    static XYSeriesCollection dataset;
    static ChartPanel chartPanel;
    static XYPlot xyplot; 

    private static void display(){  
        XYSeries serie = new XYSeries("1");
        serie.add(1, 1);
        serie.add(8, 8);
        dataset = new XYSeriesCollection();
        dataset.addSeries(serie);
        NumberAxis domain = new NumberAxis("\u03C1");
        NumberAxis range = new NumberAxis("g(\u03C1)");
        XYSplineRenderer r = new XYSplineRenderer(20);
        xyplot = new XYPlot(dataset, domain, range, r);

        final JFreeChart chart = new JFreeChart(xyplot);

        ValueAxis xAxis = xyplot.getDomainAxis();
        xAxis.setRange(-10, 10);
        ValueAxis yAxis = xyplot.getRangeAxis();
        yAxis.setRange(-10, 10);

        chartPanel = new ChartPanel(chart){
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };

        chartPanel.setDomainZoomable(false);
        chartPanel.setRangeZoomable(false);
        frame = new JFrame(Title);
        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) {}
        });

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER)); //buttons
        final JButton zoomFree = new JButton("Zoom");
        panel.add(zoomFree);

        zoomFree.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                chartPanel.setDomainZoomable(true);
                chartPanel.setRangeZoomable(true);
                zoomFree.setEnabled(false);
            }
        });

        frame.add(panel, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                display();
            }
        });
    }
}

Yes, I tried simple system output to detect working of keylistener

Was it helpful?

Solution

I want to cancel zoom with "escape" from keyboard,

Then use Key Bindings, not a KeyListener. Swing was designed to be used with Key Bindings.

Read the section from the Swing tutorial on How to Use Key Bindings for more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top