Posts

Showing posts with the label eventhandling

How to Use KeyEventDispatcher() with Multiple JFrames Easily

Image
I have  to tell that this is the easiest trick I found so far to dealing with multiple JFrames with Keyboard inputs.(Eg:- Closing a JFrame using ESC button ) Scenario: closing the JFrame "Invoice" by pressing ESC and go to another JFrame "Main_menue". 1) First of all you have to import few things as follows import java.awt.KeyEventDispatcher; import java.awt.KeyboardFocusManager; import java.awt.event.KeyEvent;  2) Then Implement your JFrame class to KeyEventDispatcher(used class name is "Invoice") public class Invoice extends javax.swing.JFrame implements KeyEventDispatcher { 3) (Step  3 & 4 make sure to do in JFrame class u want to close)  Then you have to Override the method "public boolean dispatchKeyEvent(KeyEvent e) "  of   KeyEventDispatcher, as follows.         @Override         public boolean dispatchKeyEvent(KeyEvent e) {             ...