Foro de elhacker.net

Programación => Java => Mensaje iniciado por: mapers en 27 Abril 2014, 08:05 am



Título: KeyListener Jpanel
Publicado por: mapers en 27 Abril 2014, 08:05 am
Buen señores no se porque no captura el evento del teclado tengo mi codigo asi :

Código
  1.    private JPanel createToolbar() {
  2.        final JPanel panel = new JPanel();
  3.        panel.setBackground(Color.GREEN);
  4.        panel.setLayout(new FlowLayout());
  5.        panel.addKeyListener(new KeyListener() {
  6.  
  7.         @Override
  8.         public void keyTyped(KeyEvent e) {
  9.         System.out.println("Pressed " + e.getKeyChar());
  10.         }
  11.  
  12.         @Override
  13.         public void keyReleased(KeyEvent e) {
  14.         System.out.println("Pressed " + e.getKeyChar());
  15.         }
  16.  
  17.         @Override
  18.         public void keyPressed(KeyEvent e) {
  19.         // startCapture();
  20.         System.out.println("Pressed " + e.getKeyChar());
  21.         }
  22.         });
  23.  
  24.         panel.setFocusable(true);
  25.         panel.requestFocusInWindow();
  26.  
  27.        //KeyListener listener = new MyKeyListener();
  28.        //panel.addKeyListener(listener);
  29.        //panel.setFocusable(true);
  30.  
  31.        // panel.KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this)
  32.        //KeyboardExampl keyboardExample = new KeyboardExample();
  33.        //panel.add(keyboardExample);
  34.     //   capturing = false;
  35.    //    btnStartStop = new Button("Compartir Recorte");
  36.  
  37.      //  btnStartStop.addActionListener(new ActionListener() {
  38.        //    @Override
  39.         //   public void actionPerformed(ActionEvent e) {
  40.                // if (capturing) {
  41.                // capturing = false;
  42.                // btnStartStop.setLabel("Start Capture");
  43.                // stopCapture();
  44.                // } else {
  45.                // capturing = true;
  46.                // btnStartStop.setLabel("Stop Capture");
  47.           //     System.out.println("Capturando ");
  48.            //    startCapture();
  49.                // }
  50.           // }
  51.       // });
  52.       // Detener_frame = new Button("Detener Recorte");
  53.       // Detener_frame.addActionListener(new ActionListener() {
  54.         //   @Override
  55.          //  public void actionPerformed(ActionEvent e) {
  56.            //    startCapture();
  57.             //   stopCapture();
  58.           // }
  59.       // });
  60.        //btnStartStop.setFocusable(true);
  61.       // panel.add(btnStartStop);
  62.       // panel.add(Detener_frame);
  63.  
  64.        panel.setFocusable(true);
  65.        //  panel.setVisible(false);
  66.        return panel;
  67.    }
  68.  


Título: Re: KeyListener Jpanel
Publicado por: ThinkByYourself en 28 Abril 2014, 01:26 am
No estoy seguro, pero como nota, a lo mejor es por lo de "final" en el JPanel, que no permite añadir más cambios en panel, aunque no lo sé, creo que si fuera así ya debería indicarte error antes el IDE. Ni idea, sólo como nota...