Hi, i want to do a listener and this is what i have at the moment:

class MyListener extends MouseInputAdapter implements KeyListener{

        @Override
        public void mousePressed(MouseEvent e) {
            if(e.getButton()==1){
                out.println("MSE press "+e.BUTTON1_MASK);
            }else if (e.getButton()==2){
                out.println("MSE press "+e.BUTTON2_MASK);
            }else if (e.getButton()==3){
                out.println("MSE press "+e.BUTTON3_MASK);
            }
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            out.println("MSE move "+x+" "+y);
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            out.println("MSE drag "+x+" "+y);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if(e.getButton()==1){
                out.println("MSE release "+e.BUTTON1_MASK);
            }else if(e.getButton() ==2) {
                out.println("MSE release "+e.BUTTON2_MASK);
            }else if(e.getButton() ==3) {
                out.println("MSE release "+e.BUTTON3_MASK);
            }
        }
        @Override
        public void keyTyped(KeyEvent e){

        }
        @Override
        public void keyPressed(KeyEvent e){
            out.println("KEY press "+e.getKeyCode());
            System.out.println("Say it!");
        }
        @Override
        public void keyReleased(KeyEvent e){
            out.println("KEY release "+e.getKeyCode());
            System.out.println("Say it!");
        }
    }

and then i add it to a JLabel this way:

MyListener ml = new MyListener();
        label.addMouseListener(ml);
        label.addMouseMotionListener(ml);
        label.addKeyListener(ml);

My label catches the mouse events but no key events. Can this be done somehow... or should i just make a separate key listener?

You may find that this is a problem with keyboard focus - are you sure the focus is on your JLabel? Try clicking it once then pressing a key.

yes, i tried. It's not a focus problem.

JLabels don't normally get the focus. You're going to have to tell the system that it can get the focus. There's is/are method(s) to call to enable this.

Tried doing a separate keyListener and adding to it but nothing changed.

MyKeyListener mkl = new MyKeyListener();
        label.setRequestFocusEnabled(true);
        label.setFocusTraversalKeysEnabled(true);
        label.setFocusable(true);
        label.addKeyListener(mkl);

The place to look is in the API doc for the Component class. It has several methods that relate to focus. Scan the doc for 'Focusable'.

Can you post a small program that demos the problem and can be tested by us?

Hm, trying to make the demo proved to myself that it does work when calling the setFocusable method. I realized that the problem was that i forgot to add my label to the JScrollPane. I set it as a viewport only.. my bad.
Current initializations work with the keyListener:

MyListener ml = new MyListener();
        MyKeyListener mkl = new MyKeyListener();
        label.addMouseListener(ml);
        label.addMouseMotionListener(ml);

        label.setFocusable(true);
        label.addKeyListener(mkl);
        this.scrollPane.add(label);
        this.scrollPane.setViewportView(label);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.