Example usage for java.awt.event MouseMotionAdapter MouseMotionAdapter

List of usage examples for java.awt.event MouseMotionAdapter MouseMotionAdapter

Introduction

In this page you can find the example usage for java.awt.event MouseMotionAdapter MouseMotionAdapter.

Prototype

MouseMotionAdapter

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {
    final JTabbedPane jTabbedPane = new JTabbedPane();
    jTabbedPane.addTab("Red", new JLabel("Roses"));
    jTabbedPane.addTab("Blue", new JLabel("Skies"));
    jTabbedPane.addTab("Green", new JLabel("Grass"));

    for (int i = 0; i < jTabbedPane.getTabCount(); i++) {
        final JLabel tabComponent = new JLabel(jTabbedPane.getTitleAt(i));

        tabComponent.addMouseMotionListener(new MouseMotionAdapter() {
            @Override/*from  ww  w. j ava  2s.c o  m*/
            public void mouseDragged(MouseEvent e) {
                System.out.println("tabComponent dragging");
            }
        });

        tabComponent.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                int x = tabComponent.getLocationOnScreen().x - jTabbedPane.getLocationOnScreen().x;
                int y = tabComponent.getLocationOnScreen().y - jTabbedPane.getLocationOnScreen().y;
                MouseEvent me = new MouseEvent((JLabel) e.getSource(), e.getID(), e.getWhen(), e.getModifiers(),
                        x, y, e.getLocationOnScreen().x, e.getLocationOnScreen().y, e.getClickCount(),
                        e.isPopupTrigger(), e.getButton());
                jTabbedPane.getMouseListeners()[0].mousePressed(me);
                System.out.println("tabComponent mousePressed e=" + e);
            }
        });
        jTabbedPane.setTabComponentAt(i, tabComponent);
    }
    JFrame jFrame = new JFrame();
    jFrame.add(jTabbedPane);
    jFrame.setSize(400, 500);
    jFrame.setVisible(true);
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setUndecorated(true);/*from  w  w  w  .  j a  v  a  2 s. co m*/
    frame.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            point.x = e.getX();
            point.y = e.getY();
        }
    });
    frame.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            Point p = frame.getLocation();
            frame.setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
        }
    });

    frame.setSize(300, 300);
    frame.setLocation(200, 200);
    frame.setLayout(new BorderLayout());

    frame.getContentPane().add(new JLabel("Drag to move", JLabel.CENTER), BorderLayout.CENTER);

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    menuBar.add(menu);
    JMenuItem item = new JMenuItem("Exit");
    item.addActionListener(e -> System.exit(0));
    menu.add(item);
    frame.setJMenuBar(menuBar);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame f = new JFrame(Main.class.getSimpleName());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    BufferedImage bi = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel(new ImageIcon(bi));
    panel.add(label);//from  w  w  w. j a va  2  s.c om
    MouseMotionListener doScrollRectToVisible = new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1);
            ((JPanel) e.getSource()).scrollRectToVisible(r);
        }
    };
    panel.addMouseMotionListener(doScrollRectToVisible);

    panel.setAutoscrolls(true);
    f.add(new JScrollPane(panel));
    f.pack();
    f.setSize(f.getWidth() / 2, f.getHeight() / 2);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.setUndecorated(true);/*from   w w w. j  a va2  s . co m*/
    JButton button = new JButton("Close Me");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });

    frame.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            point.x = e.getX();
            point.y = e.getY();
        }
    });
    frame.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            Point p = frame.getLocation();
            frame.setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
        }
    });

    frame.setSize(300, 300);
    frame.setLocation(200, 200);
    frame.setLayout(new BorderLayout());

    frame.getContentPane().add(button, BorderLayout.NORTH);
    frame.getContentPane().add(new JLabel("Drag Me", JLabel.CENTER), BorderLayout.CENTER);
    frame.setVisible(true);
}

From source file:ColorDrag.java

public static void main(String args[]) {
    // Create two JLabel objects
    final JLabel label1 = new JLabel("Drag here");
    JLabel label2 = new JLabel("Drop here");

    // Register TransferHandler objects on them: label1 transfers its
    // foreground color and label2 transfers its background color.
    label1.setTransferHandler(new TransferHandler("foreground"));
    label2.setTransferHandler(new TransferHandler("background"));

    // Give label1 a foreground color other than the default
    // Make label2 opaque so it displays its background color
    label1.setForeground(new Color(100, 100, 200));
    label2.setOpaque(true);//from w w w  . j av a2  s . co  m

    // Now look for drag gestures over label1. When one occurs,
    // tell the TransferHandler to begin a drag.
    // Exercise: modify this gesture recognition so that the drag doesn't
    // begin until the mouse has moved 4 pixels. This helps to keep
    // drags distinct from sloppy clicks. To do this, you'll need both
    // a MouseListener and a MouseMotionListener.
    label1.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            TransferHandler handler = label1.getTransferHandler();
            handler.exportAsDrag(label1, e, TransferHandler.COPY);
        }
    });

    // Create a window, add the labels, and make it all visible.
    JFrame f = new JFrame("ColorDrag");
    f.getContentPane().setLayout(new FlowLayout());
    f.getContentPane().add(label1);
    f.getContentPane().add(label2);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 400);//from w  w  w. j  a v a  2  s.  c o m

    JTextArea textArea = new JTextArea("drag it...");
    textArea.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            System.out.println("Mouse Dragged...");
        }

        public void mouseMoved(MouseEvent e) {
            System.out.println("Mouse Moved...");
        }
    });

    getContentPane().add(textArea);
}

From source file:XORModePaintWithMouse.java

XORModePaintWithMouse() {
    addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseMoved(MouseEvent me) {
            int x = me.getX();
            int y = me.getY();
            chsX = x - 10;//  ww w . jav a  2  s .c o  m
            chsY = y - 10;
            repaint();
        }
    });
}

From source file:MainClass.java

public MouseMotionAnonymous() {
    setBackground(Color.white);/* w w w . ja  v a2  s. co  m*/
    addMouseListener(new MouseAdapter() {
        public void mouseReleased(MouseEvent me) {
            setBackground(Color.white);
            repaint();
        }
    });
    addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent me) {
            setBackground(Color.cyan);
            repaint();
        }
    });
}

From source file:Main.java

public Main(JFrame frame) {
    this.frame = frame;
    cursor = new Point();
    this.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseMoved(MouseEvent evt) {
            cursor = new Point(evt.getPoint());
            Main.this.repaint();
        }/*from   w  ww .j  a v a  2  s  .  co  m*/
    });
    this.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            Main.this.setVisible(false);
        }
    });
}

From source file:Highlights.java

public Highlights() {
    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent me) {
            firstHit = textLayout.hitTestChar(me.getX() - x, me.getY() - y);
            secondHit = null;//  ww w  . j  ava 2 s  .  c om
        }

        public void mouseReleased(MouseEvent me) {
            secondHit = textLayout.hitTestChar(me.getX() - x, me.getY() - y);
            repaint();
        }
    });
    addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent me) {
            secondHit = textLayout.hitTestChar(me.getX() - x, me.getY() - y);
            repaint();
        }
    });

}