Example usage for javax.swing JLabel addMouseMotionListener

List of usage examples for javax.swing JLabel addMouseMotionListener

Introduction

In this page you can find the example usage for javax.swing JLabel addMouseMotionListener.

Prototype

public synchronized void addMouseMotionListener(MouseMotionListener l) 

Source Link

Document

Adds the specified mouse motion listener to receive mouse motion events from this component.

Usage

From source file:CustomListener.java

public static void main(String[] a) {
    JFrame frame = new JFrame("Popup JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label = new JLabel();

    label.addMouseMotionListener(new CustomListener() {
    });/* w  ww . j a v  a 2 s .  c om*/

    frame.add(label);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

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 w w  w .  j  av  a2  s.  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: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  ww . ja v  a  2s .c o 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

ScreenCaptureRectangle(final BufferedImage screen) {
    BufferedImage screenCopy = new BufferedImage(screen.getWidth(), screen.getHeight(), screen.getType());
    JLabel screenLabel = new JLabel(new ImageIcon(screenCopy));
    JScrollPane screenScroll = new JScrollPane(screenLabel);

    screenScroll.setPreferredSize(new Dimension(300, 300));

    repaint(screen, screenCopy);/*from  w w  w.  j a  va  2s .co  m*/
    screenLabel.repaint();

    screenLabel.addMouseMotionListener(new MouseMotionAdapter() {
        Point start = new Point();

        @Override
        public void mouseMoved(MouseEvent me) {
            start = me.getPoint();
            repaint(screen, screenCopy);
            screenLabel.repaint();
        }

        @Override
        public void mouseDragged(MouseEvent me) {
            Point end = me.getPoint();
            captureRect = new Rectangle(start, new Dimension(end.x - start.x, end.y - start.y));
            repaint(screen, screenCopy);
            screenLabel.repaint();
        }
    });
    JOptionPane.showMessageDialog(null, screenScroll);
}

From source file:Main.java

public Main() {
    mainPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    mainPanel.setLayout(null);//from  w  ww  .j  a v  a  2s.c o m

    MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
    for (int i = 0; i < LABEL_STRINGS.length; i++) {
        JLabel label = new JLabel(LABEL_STRINGS[i], SwingConstants.CENTER);
        label.setSize(new Dimension(LBL_WIDTH, LBL_HEIGHT));
        label.setOpaque(true);
        Random random = new Random();
        label.setLocation(random.nextInt(WIDTH - LBL_WIDTH), random.nextInt(HEIGHT - LBL_HEIGHT));
        label.setBackground(
                new Color(150 + random.nextInt(105), 150 + random.nextInt(105), 150 + random.nextInt(105)));
        label.addMouseListener(myMouseAdapter);
        label.addMouseMotionListener(myMouseAdapter);

        mainPanel.add(label);
    }
}