Borneq
Borneq

Reputation: 281

Why MouseMotionListener does not work?

I implement MouseMotionListener, also implement MouseListener. I added in constructor newContentPane.addMouseListener(this) and

newContentPane.addMouseMotionListener(this);

It is not enough? Methods like mouseDragged(MouseEvent e), mouseMoved(MouseEvent e) are not called.

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;

public class BorderFrame extends JFrame implements MouseListener,MouseMotionListener{
    private static final long serialVersionUID = 1L;
    private JButton northButton;
    private JButton southButton;
    private JButton westButton;
    private JButton eastButton;
    private JButton centerButton1;
    private JButton centerButton2;
    private JPanel newContentPane;
    private JSplitPane splitPane1;
    private JSplitPane splitPane2;

    public BorderFrame(String title) {
        super(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        newContentPane = new JPanel(new BorderLayout());
        northButton = new JButton("North");
        southButton = new JButton("South");
        westButton = new JButton("West");
        eastButton = new JButton("East");
        centerButton1 = new JButton("Center1");
        centerButton2 = new JButton("Center2");
        newContentPane.add(northButton, "North");
        newContentPane.add(southButton, BorderLayout.SOUTH);
        newContentPane.add(westButton, BorderLayout.WEST);
        newContentPane.add(eastButton, BorderLayout.EAST);
        // newContentPane.add(centerButton, BorderLayout.CENTER);
        splitPane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, centerButton1,
                centerButton2);
        splitPane1.setResizeWeight(0.5);
        splitPane1.setOneTouchExpandable(true);
        splitPane1.setContinuousLayout(true);
        newContentPane.add(splitPane1, BorderLayout.CENTER);

        add(newContentPane);
        newContentPane.addMouseListener(this);
        newContentPane.addMouseMotionListener(this);
    }

    protected static void createAndShowGUI() {
        final BorderFrame borderFrame = new BorderFrame("Test BorderLayout");
        borderFrame.pack();
        borderFrame.setLocationRelativeTo(null);
        borderFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }


//AUTOGENERATED methods
....
}

Upvotes: 0

Views: 2890

Answers (3)

trashgod
trashgod

Reputation: 205885

Your layout is completely filled with buttons that intercept mouse events. Replace one with a label to see the difference.

newContentPane.add(new JLabel("North"), BorderLayout.NORTH);

Also consider using MouseAdapter and overriding just the methods you want to use.

MouseHandler handler = new MouseHandler();
newContentPane.addMouseListener(handler);
newContentPane.addMouseMotionListener(handler);
…
private static class MouseHandler extends MouseAdapter {

    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println(e);
    }
}

Upvotes: 1

Iuri Sitinschi
Iuri Sitinschi

Reputation: 21

if your frame contains only newContentPane (which contains other components) you can try just to add listeners to frame:

...
add(newContentPane);
addMouseListener(this);
addMouseMotionListener(this);
}
...

Upvotes: 0

Vallabh Patade
Vallabh Patade

Reputation: 5110

You need to implement the method of MouseMotionListener and MouseListener interfaces. Tere will be total 7 methods from both these interfaces that you need to override. Write code in those method which events you want to handle. But you need to override all these methods.

public void mouseMoved(MouseEvent e) {
   //Code what you want to do
}

public void mouseDragged(MouseEvent e) {
   //Code what you want to do
}

 public void mousePressed(MouseEvent e) {
   //Code what you want to do
}

public void mouseReleased(MouseEvent e) {
   //Code what you want to do
}

public void mouseEntered(MouseEvent e) {
   //Code what you want to do
}

public void mouseExited(MouseEvent e) {
   //Code what you want to do
}

public void mouseClicked(MouseEvent e) {
   //Code what you want to do
}

Upvotes: 0

Related Questions