Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

how can i use keyevent instead of textfield getsource in a java awt program priv

ID: 3709495 • Letter: H

Question

how can i use keyevent instead of textfield getsource in a java awt program

private class InputListener implements ActionListener { @override public void actionPerformed (ActionEvent e) [ if (e.getActionCommand().equals("New game")) if (e.getActionCommand().equals("Exit")) System.exit(0); // All the 9*9 JTextFileds invoke this handler. We need to determine // which JTextField (which row and column) is the source for this invocation int rowSelected-1; int co!Selected -1; // Get the source object that fired the event TextField source= (JTextField) e.getSource(): Scan JTextFileds match with the source object boolean found false; for (int row-e; row

Explanation / Answer

Events:

User-->Actom-->Events.

> Every events in java is in the form of class.Whenever user takes an action an object of this class is created.

> Listners: Every listner in java is in the form of an interface.

> Handlers: Handlers are the method declared by (method) listner interface

NOTE: Event in java uses proper case

Event(class)->Listner(interface)->Handlers(methods):

KeyEvent -> KeyListener-> 1.public void keypressed(keyEvent m1);

                                            2.public void keyTyped(keyEvent m2);

                                            3.public void keyReleased(keyEvent m3);

example:

      KeyStroke k1=KeyStroke.getKeyStroke ( KeyEvent.VK_O, Event.CTRL_MASK );

      mi1.setAccelerator(k1);          // mi1 is MenuItem1

NOTE: All event related classes and interfaces comes from java.awt.events package.

Exit Action:

public void actionPerformed (ActionEvent e)

{

System.exit(0);

ExitBt.addActionListerner(this);              // ExitBt is JButton

}

(OR )

obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);