0

I tried to add key and mouse listener to JFrame, it didn't work, also i find out that JFrame can't get focus. I have a function which loads the buffered image where i want to add the mouse and key listener in entire image dimension.

  public void imageloader(BufferedImage image) throws InterruptedException {

        // frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Dimension dimension = new Dimension(image.getWidth(), image.getHeight());
        setSize(200, 100);
        setVisible(true);
        label.removeAll(); //label is Jlabel
        label.setIcon(new ImageIcon(image));
        frame.setSize(dimension);
        label.revalidate();
        JScrollPane pane = new JScrollPane(label,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        frame.getContentPane().add(pane);
        frame.setSize(dimension);
       // frame.setVisible(true);


    }

How can be mouse and key listener be added to this image frame without moving parts from this function?

3
  • please are you tired or tried Commented Oct 31, 2011 at 14:26
  • hehehe you questions talks about 1st. from options :-), thanks for edit Commented Oct 31, 2011 at 14:31
  • hmmm maybe, not sure because you can't post required code, only code snipped, to try to wrap that into invokeLater() as discused here stackoverflow.com/questions/7966287/… Commented Nov 1, 2011 at 16:07

3 Answers 3

2

Use KeyBindings instead of KeyListener, example for something similair is here

The reason for this is how events are handled by Swing. You need a widget with focus for KeyListener to work - without focus, any KeyListeners are ignored (otherwise, typing would add to all TextFields in your UI instead of only the one with the focus).

The raw KeyBindings API doesn't care about focus - if no child window processes the event, the listener will be called.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't have problem handling the events. I just want to add the listener to the image somehow.
I updated the answer to make the difference between a KeyListener and a KeyBinding more obvious.
+1, for the key bindings. @aneal yes you will have a problem because labels don't get focus so a key event won't be generated.
I think its hard to understand. If i am not wrong KeyBinding is used for individuals key, but i am trying to get keycode of each pressed key irrespective of what key is pressed. So how can i do that with KeyBinding?
2

Add the MouseListener to the label. Use Key Bindings for the key events.

Comments

0

I solved the problem using Layered panes. I used one pane to display buffered image and next pane to listen Mouse and Keyboard events.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.