Example usage for javax.swing JLabel addMouseListener

List of usage examples for javax.swing JLabel addMouseListener

Introduction

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

Prototype

public synchronized void addMouseListener(MouseListener l) 

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JLabel label = new JLabel("Label Text");

    final String propertyName = "text";
    label.setTransferHandler(new TransferHandler(propertyName));

    label.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent evt) {
            JComponent comp = (JComponent) evt.getSource();
            TransferHandler th = comp.getTransferHandler();

            th.exportAsDrag(comp, evt, TransferHandler.COPY);
        }//w  ww.  j  a va 2s  .c  o m
    });
}

From source file:DragMouseAdapter.java

public static void main(String[] args) {
    JFrame f = new JFrame("Icon Drag & Drop");
    ImageIcon icon1 = new ImageIcon("a.png");
    ImageIcon icon2 = new ImageIcon("b.png");
    ImageIcon icon3 = new ImageIcon("c.png");

    JButton button = new JButton(icon2);

    JLabel label1 = new JLabel(icon1, JLabel.CENTER);
    JLabel label2 = new JLabel(icon3, JLabel.CENTER);

    MouseListener listener = new DragMouseAdapter();
    label1.addMouseListener(listener);
    label2.addMouseListener(listener);//w  ww .  ja va  2s.  c  om

    label1.setTransferHandler(new TransferHandler("icon"));
    button.setTransferHandler(new TransferHandler("icon"));
    label2.setTransferHandler(new TransferHandler("icon"));

    f.setLayout(new FlowLayout());
    f.add(label1);
    f.add(button);
    f.add(label2);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    f.setVisible(true);
}

From source file:tk.mystudio.ocr.OCRTest.java

public static void main(String[] args) {
    JFrame frame = new JFrame("??");

    final JLabel label = new JLabel(new ImageIcon(), JLabel.CENTER);
    byte[] image = client.getCodeByte(Constants.LOGIN_CODE_URL);
    String randCodeByRob = OCR.read(image);
    label.setIcon(new ImageIcon(image));
    label.setText(randCodeByRob);/*from  ww  w .ja v  a  2 s  .c om*/
    label.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            byte[] image = client.getCodeByte(Constants.LOGIN_CODE_URL);
            String randCodeByRob = OCR.read(image);
            label.setIcon(new ImageIcon(image));
            label.setText(randCodeByRob);
            System.out.println(randCodeByRob);
        }
    });
    frame.add(label);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 200);
    frame.setLocationRelativeTo(null);
    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 ww  .ja  v  a2  s .  com
            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:DragLabel.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Drag Label");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label = new JLabel("Hello, World");
    label.setTransferHandler(new TransferHandler("foreground"));
    MouseListener listener = new MouseAdapter() {
        public void mousePressed(MouseEvent me) {
            JComponent comp = (JComponent) me.getSource();
            TransferHandler handler = comp.getTransferHandler();
            handler.exportAsDrag(comp, me, TransferHandler.COPY);
        }/*from   w ww.ja  va  2 s .  c o m*/
    };
    label.addMouseListener(listener);
    frame.add(label, BorderLayout.SOUTH);

    JTextField text = new JTextField();
    frame.add(text, BorderLayout.NORTH);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Drag Image");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Icon icon = new ImageIcon("image.jpg");
    JLabel label = new JLabel(icon);
    label.setTransferHandler(new ImageSelection());
    MouseListener listener = new MouseAdapter() {
        public void mousePressed(MouseEvent me) {
            JComponent comp = (JComponent) me.getSource();
            TransferHandler handler = comp.getTransferHandler();
            handler.exportAsDrag(comp, me, TransferHandler.COPY);
        }//from  w ww .j  a va2  s.  c  om
    };
    label.addMouseListener(listener);
    frame.add(new JScrollPane(label), BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:ImageSelection.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Drag Image");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Icon icon = new ImageIcon("yourFile.gif");
    JLabel label = new JLabel(icon);
    label.setTransferHandler(new ImageSelection());
    MouseListener listener = new MouseAdapter() {
        public void mousePressed(MouseEvent me) {
            JComponent comp = (JComponent) me.getSource();
            TransferHandler handler = comp.getTransferHandler();
            handler.exportAsDrag(comp, me, TransferHandler.COPY);
        }//  ww  w.j  a va2s . c  om
    };
    label.addMouseListener(listener);
    frame.add(new JScrollPane(label), BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:DragImage.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Clip Image");
    Container contentPane = frame.getContentPane();

    final Clipboard clipboard = frame.getToolkit().getSystemClipboard();

    Icon icon = new ImageIcon("jaeger.jpg");
    final JLabel label = new JLabel(icon);
    label.setTransferHandler(new ImageSelection());

    MouseListener mouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            JComponent comp = (JComponent) e.getSource();
            TransferHandler handler = comp.getTransferHandler();
            handler.exportAsDrag(comp, e, TransferHandler.COPY);
        }//from  w  ww  .java  2 s.  co m
    };
    label.addMouseListener(mouseListener);

    JScrollPane pane = new JScrollPane(label);
    contentPane.add(pane, BorderLayout.CENTER);

    JButton copy = new JButton("Copy");
    copy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TransferHandler handler = label.getTransferHandler();
            handler.exportToClipboard(label, clipboard, TransferHandler.COPY);
        }
    });

    JButton clear = new JButton("Clear");
    clear.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            label.setIcon(null);
        }
    });
    clear.setTransferHandler(new TransferHandler("text"));
    mouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            JComponent comp = (JComponent) e.getSource();
            TransferHandler handler = comp.getTransferHandler();
            handler.exportAsDrag(comp, e, TransferHandler.COPY);
        }
    };
    clear.addMouseListener(mouseListener);

    JButton paste = new JButton("Paste");
    paste.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Transferable clipData = clipboard.getContents(clipboard);
            if (clipData != null) {
                if (clipData.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                    TransferHandler handler = label.getTransferHandler();
                    handler.importData(label, clipData);
                }
            }
        }
    });

    JPanel p = new JPanel();
    p.add(copy);
    p.add(clear);
    p.add(paste);
    contentPane.add(p, BorderLayout.SOUTH);

    JTextField tf = new JTextField();
    tf.setDragEnabled(true);
    contentPane.add(tf, BorderLayout.NORTH);
    frame.setSize(300, 300);
    frame.show();
}

From source file:Main.java

/**
 * Create a HTML hyperlink in JLabel component
 *
 * @param label// ww  w  . jav  a2  s  . co  m
 * @param url
 * @param text
 */
public static void createHyperLink(JLabel label, final String url, String text) {
    label.setToolTipText(url);
    label.setText("<html><a href=\"\">" + text + "</a></html>");
    label.setCursor(new Cursor(Cursor.HAND_CURSOR));
    label.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            try {
                Desktop.getDesktop().browse(new URI(url));
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            } catch (URISyntaxException ex) {
                throw new RuntimeException(ex);
            }
        }
    });
}

From source file:Main.java

private void goWebsite(JLabel website) {
    website.addMouseListener(new MouseAdapter() {
        @Override/*from  ww  w .j av a 2s  .com*/
        public void mouseClicked(MouseEvent e) {
            try {
                try {
                    Desktop.getDesktop().browse(new URI("http://www.java2s.com"));
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (URISyntaxException ex) {

            }
        }
    });
}