0

Can someone please tell me why mousedragged isn't being recognized in the following code, and possibly help me fix this problem?

public class Hello extends JPanel implements KeyListener, MouseListener, MouseMotionListener{
    JPanel panel = new JPanel();
    JFrame frame = new JFrame();
    public Hello() {
        addKeyListener(this);
        addMouseListener(this);
        addMouseMotionListener(this);
        panel.addKeyListener(this);
        panel.addMouseListener(this);
        frame.addKeyListener(this);
        frame.addMouseListener(this);
    }

    public static void main(String [] args){
        Hello play = new Hello();
        play.setPanel();
    }
    public void setPanel(){
        panel.setLayout(null);
        frame.add(panel);
        frame.setLayout(null);
        panel.setBounds(0,0,100,100);
        frame.setVisible(true);
        panel.setVisible(true);
        panel.setFocusable(true);
        frame.setSize(100,100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void keyTyped(KeyEvent arg0){
        System.out.println("keytyped");
    }
    public void keyPressed(KeyEvent arg0){
        System.out.print("keypressed");
    }
    public void keyReleased(KeyEvent arg0){
        System.out.println("keyreleased");
    }
    public void mousePressed(MouseEvent arg0){
        System.out.println("mousepressed");
    }
    public void mouseReleased(MouseEvent arg0){
        System.out.println("mousereleased");
    }
    public void mouseClicked(MouseEvent arg0){
        System.out.println("mouseclicked");
    }
    public void mouseEntered(MouseEvent arg0){
        System.out.println("mousenentered");
    }
    public void mouseExited(MouseEvent arg0){
        System.out.println("mouseexited");
    }
    public void mouseDragged(MouseEvent arg0){
        System.out.println("mousedragged");
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

0

The panel has focus so you need to add the MouseMotionListener to the panel.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

You set a MouseMotionListener to the class' panel, but that panel is not in focus.

    panel.addKeyListener(this);
    panel.addMouseListener(this);
    frame.addKeyListener(this);
    frame.addMouseListener(this);

Here, you gave key and mouse listeners to frame and panel. Then down further in the code, you set frame and panel to visible. The only panel left with a MouseMOTIONListener is the invisible structure panel.

    panel.addKeyListener(this);
    panel.addMouseListener(this);
    panel.addMouseMotionListener(this);
    frame.addKeyListener(this);
    frame.addMouseListener(this);
    frame.addMouseMotionListener(this);

Give that a shot and see where it gets you. I haven't worked with swing in a long time, but I think that might help.

Eric Wich
  • 1,504
  • 10
  • 8
  • -1, the same answer was given earlier. No need to clutter the forum with duplicate answers. Plus there is no need to add the MouseMotionListener to the frame since the event is handled by the panels listener. – camickr Jun 01 '13 at 21:05