1 Reply Latest reply: Mar 29, 2013 5:46 AM by user260user RSS

    Event handling the keyboard touch of a button

    user260user
      Hi,

      I would like to do an event handler to press the button on the keyboard.
      If you press the "F1" will be the

      engine = new JButton("Runing enginea ");

      pressed (set to):

      engine.setEnabled (false);

      I have for now:
      // class car

      package symulator;

      import java.awt.Container;
      import javax.swing.JFrame;

      public class Car {

           public static void main(String[] args) {
                // TODO Auto-generated method stub

                javax.swing.SwingUtilities.invokeLater( new Runnable() {
                     public void run() {
                     Window frame = new Window();
                          frame.setVisible(true);
                     }
                });
           }
      }

      class Window extends JFrame {

           public Window() {
                
                Container container = getContentPane();
                Component1 panel = new Component1();
      container.add(panel);

           }

      }

      // class


      import java.awt.*;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.KeyEvent;
      import java.awt.event.KeyListener;
      import javax.swing.*;

      public class Component1 extends JPanel implements ActionListener, KeyListener {
           protected JButton engine, run1;
           boolean msg = true;

           public Component1() {
           
                setLayout((LayoutManager) new BoxLayout(this, BoxLayout.Y_AXIS));

                engine = new JButton("Running engine ");
                engine.setActionCommand("msg");
                engine.addActionListener(this);
                engine.setEnabled(msg);

                run1 = new JButton("Running run1u ");
                // run1.setActionCommand("enable");
                run1.setEnabled(msg);
                
                

                add(engine);
                add(run1);


           }

           public void init() {
                addKeyListener( this );
                requestFocus();
           }

           @Override
           public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                if ("msg".equals(e.getActionCommand())) {
                     engine.setEnabled(msg);               

                } else {
                     engine.setEnabled(msg);          

                }
           }

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

                switch (key) {
                case KeyEvent.VK_F1:
                     msg = false;
                     engine.setEnabled(msg);
                     break;

                }
                repaint();
           }

           public void showStatus(String string) {
                // TODO Auto-generated method stub

           }

           @Override
           public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub

           }

           @Override
           public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub

           }

      }

      Pretty please help
      Performed by pressing the button event handler

      thank you very much for your help