Uday A. Navapara Uday A. Navapara - 1 year ago 225
Java Question

Disable Escape key on JDialog Box

i am beginner in java.

i have create on JDialog box which must wait for the reply

for tha the following code is write

final Object[] options = {"Yes!! I am woking", "No!! was not working"};

final JOptionPane optionPane =
new JOptionPane("IT look like are you busy \n with Something else!!! \n Are you working?",
JOptionPane.YES_NO_OPTION, null, options);

JDialog dialog = optionPane.createDialog("Are you working?");

now the problem is that when this dialog is display the user can close this dialog by pressing the escape key now i want stop the dialog to close on pressing escape key. can any one help me?

i have refer the following link which is not use full for me

JDialog: How to disable the ESC key of my Modal dialog?

Answer Source

you must have to set the following property true for add listener on JDialog.


then by adding key listener you can disable the or avoid the escape key by following code

    jDialog.addKeyListener(new KeyListener() {
     public void keyPressed(KeyEvent e) {
                   write any code related to kdy
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download