Java Swing How to - Receive mouseDragged events for my JTable








Question

We would like to know how to receive mouseDragged events for my JTable.

Answer

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/*from ww  w  .  j  av a2s  . com*/
import javax.swing.JFrame;
import javax.swing.JTable;

public class Main {

  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JTable(new Object[][] { { 1, 2, 3 }, { 4, 5, 6 } }, new Object[] {
        "one", "two", "three" }) {
      {
        addMouseMotionListener(new MouseAdapter() {
          @Override
          public void mouseDragged(MouseEvent e) {
            System.out.println("mouseDragged");
          }

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

          @Override
          public void mouseReleased(MouseEvent e) {
            System.out.println("mouseReleased");
          }
        });
      }
    });
    f.setVisible(true);
  }
}