2

Can anyone help me to set a KeyPress action on a currently opened jInternalFrame?

I have a jDesktopPane inside a jframe, and I have multiple jInternalFrame inside the DesktopPane. I am using Netbeans to create this application.

On the jDesktopPane I have 3 buttons to open 3 jInternalFrame, and I created a Keypress on those buttons and it works fine using this code:

private void formKeyPressed(java.awt.event.KeyEvent evt) {                                
    // TODO add your handling code here:
     if(evt.getKeyCode()==KeyEvent.VK_F3){
        frmLogistics.setVisible(true);
        frmLogistics.toFront();
    }
}                               

A jInternalFrame is open and inside there's a jtoolbar with sets of buttons, one of it is a close button to close that opened jInternalFrame. I set up the code for its ActionPerform so when a user clicks that button the frame or window will be closed.

The problem now is how about a keyboard press? I want to trigger that close button inside the toolbar in a internalframe in order to close it

I tried this code:

  private void btnCloseLogisticsKeyPressed(java.awt.event.KeyEvent evt) {                                             
    // TODO add your handling code here:
    if(evt.getKeyCode()==KeyEvent.VK_F4){
         int type = JOptionPane.YES_NO_OPTION;
        int choice = JOptionPane.showConfirmDialog(this,"Do You Want to Log Out?","Exit Logistics System", type);
        if(choice == JOptionPane.YES_OPTION){
            frmLogistics.setVisible(false);
            frmLogIn.show();
            btnCashier.setEnabled(false);
            btnTrucking.setEnabled(false);
            btnAccounting.setEnabled(false);
        }
    }
}                                 

But nothing happens. I tried to put that code inside jtoolbar, jInternalFrame and still nothing happens. Maybe anyone of you could help me?

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170

2 Answers2

2

For Swing, typically use key bindings over the AWT based, lower level, KeyListener. See How to Use Key Bindings for details on how to use them.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

In the event keypress click right in the Jframe de netbeans mas naki..

private void formKeyPressed(java.awt.event.KeyEvent evt) {                                
    // TODO add your handling code here:

     if(evt.getKeyCode()==KeyEvent.VK_F4){
   dispose();

}
David
  • 208,112
  • 36
  • 198
  • 279