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?

nebula
  • 3,932
  • 13
  • 53
  • 82
  • please are you `tired` or `tried` – mKorbel Oct 31 '11 at 14:26
  • hehehe you questions talks about 1st. from options :-), thanks for edit – mKorbel Oct 31 '11 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 http://stackoverflow.com/questions/7966287/dynamically-change-jbutton-icon/7966850#7966850 – mKorbel Nov 01 '11 at 16:07

3 Answers3

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.

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I don't have problem handling the events. I just want to add the listener to the image somehow. – nebula Oct 31 '11 at 14:35
  • I updated the answer to make the difference between a KeyListener and a KeyBinding more obvious. – Aaron Digulla Oct 31 '11 at 14:41
  • +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. – camickr Oct 31 '11 at 14:43
  • 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? – nebula Nov 01 '11 at 09:26
2

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

camickr
  • 321,443
  • 19
  • 166
  • 288
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.

nebula
  • 3,932
  • 13
  • 53
  • 82