0

I created one JFrame with JDesktopPane, in which I am calling JInternalFrame. Now I want to close that internal frame by pressing escape key.

I tried 2-3 ways, but no output.

  1. I did that by using code given below:

    public static void closeWindow(JInternalFrame ji){
        ActionListener close=New ActionListener(){
    
        public void actionPerformed(ActionEvent e){
            ji.dispose();
        }
    };
    

    When I called above method from my intern frame class constructor by supplying its object , I was able to close it. But when there I write some other lines of code to the constructor. The above method call doesn't work. Please help me. I unable to find the problem in the code.

  2. Also I tried to add KeyListener to internal frame, so I able to work with key strokes,but it also doesn't work.
  3. Again I tried to setMnemonic to button as escape as below:

    jButton1.setMnemonic(KeyEvent.VK_ESCAPE);
    

    But also gives no output.

DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
user6725738
  • 13
  • 1
  • 4
  • 1
    Your 2nd approach seems like the way to go. You may have added the KeyListener incorrectly. – byxor Aug 22 '16 at 14:47
  • 1
    Don't (try to) post screenshots of your code! Just post the code itself. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Aug 22 '16 at 14:54
  • That photo of your screen with code is totally unreadable. – Jesper Aug 22 '16 at 14:55
  • @BrandonIbbotson Thank you. – user6725738 Aug 22 '16 at 14:55
  • @Jesper the image code is same as written in approach 1. – user6725738 Aug 22 '16 at 14:57
  • @BrandonIbbotson Thank you. Will you please tell me correct way to add keylistener to JInternalFrame. I called add listener method in JInternalFrame class constructor on this object. But no output. – user6725738 Aug 22 '16 at 15:03
  • @user6725738 try implementing the `KeyListener` class, and in the constructor of the frame try `this.addKeyListener( this );` – Orin Aug 22 '16 at 15:07

2 Answers2

0

You need to implement the KeyListener interface, or add one that is Anonymous. In this example, I just implemented it.

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class JInternalFrame extends JFrame implements KeyListener {

    public JInternalFrame() 
    {
        super();


        // other stuff to add to frame
        this.setSize(400, 400);
        this.setVisible(true);

        this.addKeyListener( this );
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // Don't need to implement this


    }

    @Override
    public void keyPressed(KeyEvent e) {
        if( e.getKeyCode() == KeyEvent.VK_ESCAPE ) {
            System.exit(0); //Change this to dispose or whatever you want to do with the frame
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        //Dont need to implement anything here

    }

    public static void main(String[] args)
    {
        JInternalFrame frame = new JInternalFrame();
    }

}

Now if this is an internal jframe as mentioned, it is probably better to implement the keylistener in the JDesktopPane and call the dispose method on the JInternalFrame after pressing escape instead of implementing keylistener in this frame. It all depends on which GUI component has focus of input.

Orin
  • 920
  • 5
  • 16
  • Thank you. This solution works fine for jinternalframe only if there are no any components present in it. When I add some components like jlabel,jtextfield,jbutton to jinternalframe, the keylistener is not working. I think some other components present in jinternalframe gets the focus, so it's not able to detect the keypressed event for jinternalframe. Can you please help me to solve this problem? – user6725738 Aug 22 '16 at 17:32
0

This issue is old now but I recently got stuck on a similar problem. Adding the key listener to the content pane of the internal frame instead of the internal frame itself did the job for me.

this.getContentPane().addKeyListener(this);
Stoony
  • 1