Example usage for javax.swing JPanel addKeyListener

List of usage examples for javax.swing JPanel addKeyListener

Introduction

In this page you can find the example usage for javax.swing JPanel addKeyListener.

Prototype

public synchronized void addKeyListener(KeyListener l) 

Source Link

Document

Adds the specified key listener to receive key events from this component.

Usage

From source file:com.net2plan.gui.GUINet2Plan.java

private static JPanel showAbout() {
    final JPanel aboutPanel = new JPanel();

    ImageIcon image = new ImageIcon(
            ImageUtils.readImageFromURL(GUINet2Plan.class.getResource("/resources/gui/logo.png")));
    JLabel label = new JLabel("", image, JLabel.CENTER);

    aboutPanel.setLayout(new MigLayout("insets 0 0 0 0", "[grow]", "[grow][grow]"));
    aboutPanel.add(label, "alignx center, aligny bottom, wrap");
    aboutPanel.add(new JLabel(ABOUT_TEXT), "alignx center, aligny top");
    aboutPanel.setFocusable(true);/*from   w  w  w .j  a  v a  2 s .c o  m*/
    aboutPanel.requestFocusInWindow();

    aboutPanel.addKeyListener(new KeyAdapter() {
        private final int[] sequence = new int[] { KeyEvent.VK_UP, KeyEvent.VK_UP, KeyEvent.VK_DOWN,
                KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT,
                KeyEvent.VK_A, KeyEvent.VK_B };
        private int currentButton = 0;

        @Override
        public void keyPressed(KeyEvent e) {
            int keyPressed = e.getKeyCode();

            if (keyPressed == sequence[currentButton]) {
                currentButton++;

                if (currentButton == sequence.length) {
                    ErrorHandling.setDebug(true);
                    aboutPanel.removeKeyListener(this);
                }
            } else {
                currentButton = 0;
            }
        }
    });

    return aboutPanel;
}

From source file:op.FrmMain.java

public void setBlocked(boolean blocked) {
    if (blocked) {
        lblWait.setVisible(true);/*from   w  w w .j a v  a 2s . c om*/
        JPanel glass = new JPanel();
        glass.addMouseListener(new MouseAdapter() {
        });
        glass.addMouseMotionListener(new MouseMotionAdapter() {
        });
        glass.addKeyListener(new KeyAdapter() {
        });
        glass.setOpaque(false);
        setGlassPane(glass);
        getGlassPane().setVisible(true);
        ((CardLayout) pnlCard.getLayout()).show(pnlCard, "cardWait");
    } else {
        getGlassPane().setVisible(false);
        setGlassPane(new JPanel());
        ((CardLayout) pnlCard.getLayout()).show(pnlCard, "cardContent");
    }
}

From source file:org.onebusaway.phone.client.SimplePhoneClient.java

private static void setupGui(AgiClientScriptImpl script) {

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    KeyPressHandler handler = new KeyPressHandler(script);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.addKeyListener(handler);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(4, 3));
    panel.add(buttonPanel, BorderLayout.CENTER);

    String buttons = "123456789*0#";

    for (int i = 0; i < buttons.length(); i++)
        addButton(buttonPanel, script, handler, buttons.charAt(i));

    Document document = script.getDocument();

    final JTextArea textArea = new JTextArea(document);
    textArea.setEditable(false);//from   w ww. j  a  va2s.  c  om
    textArea.addKeyListener(handler);
    document.addDocumentListener(new ScrollDocumentToEnd(textArea));

    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setPreferredSize(new Dimension(300, 100));
    scrollPane.addKeyListener(handler);
    panel.add(scrollPane, BorderLayout.SOUTH);

    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
}