List of usage examples for java.awt.event MouseEvent getSource
public Object getSource()
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); }/*from w w w. j av a 2s. c om*/ }; label.addMouseListener(listener); frame.add(new JScrollPane(label), BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { String[] items = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" }; JList<String> myJList = new JList(items) { @Override//from w w w . ja v a 2s.com protected void processMouseEvent(MouseEvent e) { int modifiers = e.getModifiers() | InputEvent.CTRL_MASK; int modifiersEx = e.getModifiersEx() | InputEvent.CTRL_MASK; MouseEvent myME = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), modifiers, e.getX(), e.getY(), e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(), e.isPopupTrigger(), e.getButton()); super.processMouseEvent(myME); } }; JFrame f = new JFrame(); f.add(new JScrollPane(myJList)); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setLocationRelativeTo(null); f.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); }// w w w.j av a 2 s . c o 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:MenuDemo1.java
public static void main(String[] args) { // Create a window for this demo JFrame frame = new JFrame("Menu Demo"); JPanel panel = new JPanel(); frame.getContentPane().add(panel, "Center"); // Create an action listener for the menu items we will create // The MenuItemActionListener class is defined below ActionListener listener = new MenuItemActionListener(panel); // Create some menu panes, and fill them with menu items // The menuItem() method is important. It is defined below. JMenu file = new JMenu("File"); file.setMnemonic('F'); file.add(menuItem("New", listener, "new", 'N', KeyEvent.VK_N)); file.add(menuItem("Open...", listener, "open", 'O', KeyEvent.VK_O)); file.add(menuItem("Save", listener, "save", 'S', KeyEvent.VK_S)); file.add(menuItem("Save As...", listener, "saveas", 'A', KeyEvent.VK_A)); JMenu edit = new JMenu("Edit"); edit.setMnemonic('E'); edit.add(menuItem("Cut", listener, "cut", 0, KeyEvent.VK_X)); edit.add(menuItem("Copy", listener, "copy", 'C', KeyEvent.VK_C)); edit.add(menuItem("Paste", listener, "paste", 0, KeyEvent.VK_V)); // Create a menubar and add these panes to it. JMenuBar menubar = new JMenuBar(); menubar.add(file);/*from w w w .j a v a 2 s .c o m*/ menubar.add(edit); // Add menubar to the main window. Note special method to add menubars frame.setJMenuBar(menubar); // Now create a popup menu and add the some stuff to it final JPopupMenu popup = new JPopupMenu(); popup.add(menuItem("Open...", listener, "open", 0, 0)); popup.addSeparator(); // Add a separator between items JMenu colors = new JMenu("Colors"); // Create a submenu popup.add(colors); // and add it to the popup menu // Now fill the submenu with mutually-exclusive radio buttons ButtonGroup colorgroup = new ButtonGroup(); colors.add(radioItem("Red", listener, "color(red)", colorgroup)); colors.add(radioItem("Green", listener, "color(green)", colorgroup)); colors.add(radioItem("Blue", listener, "color(blue)", colorgroup)); // Arrange to display the popup menu when the user clicks in the window panel.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { // Check whether this is the right type of event to pop up a popup // menu on this platform. Usually checks for right button down. if (e.isPopupTrigger()) popup.show((Component) e.getSource(), e.getX(), e.getY()); } }); // Finally, make our main window appear frame.setSize(450, 300); 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// w ww .jav a 2 s. co 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
private static AttributeSet getAttributes(MouseEvent e) { JTextPane text = (JTextPane) e.getSource(); Point mouseLocation = new Point(e.getX(), e.getY()); int pos = text.viewToModel(mouseLocation); if (pos >= 0) { try {//www.j a v a 2 s . c o m Rectangle rect = text.modelToView(pos); int lowerCorner = rect.y + rect.height; if (e.getX() < rect.x && e.getY() < lowerCorner && pos > 0) { pos--; } } catch (BadLocationException ex) { ex.printStackTrace(); } StyledDocument doc = text.getStyledDocument(); Element element = doc.getCharacterElement(pos); return element.getAttributes(); } return null; }
From source file:Main.java
public static void addDoubleClickEvent(JList list) { list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { JList source = (JList) e.getSource(); if (e.getClickCount() == 2) { ListSelectionListener[] listeners = source.getListSelectionListeners(); for (int i = 0; i < listeners.length; i++) { listeners[i].valueChanged(new ListSelectionEvent(source, source.getSelectedIndex(), source.getSelectedIndex(), false)); }/*from w w w. j ava 2 s . co m*/ } } }); }
From source file:org.bitbucket.mlopatkin.android.logviewer.widgets.UiHelper.java
public static void addDoubleClickAction(JComponent component, final Action action) { component.addMouseListener(new MouseAdapter() { @Override/*from www. jav a 2 s.c o m*/ public void mouseClicked(MouseEvent e) { if (e.getClickCount() == DOUBLE_CLICK_COUNT && e.getButton() == MouseEvent.BUTTON1) { action.actionPerformed(new ActionEvent(e.getSource(), ActionEvent.ACTION_PERFORMED, (String) action.getValue(Action.ACTION_COMMAND_KEY), e.getWhen(), e.getModifiers())); } } }); }
From source file:DragMouseAdapter.java
public void mousePressed(MouseEvent e) { JComponent c = (JComponent) e.getSource(); TransferHandler handler = c.getTransferHandler(); handler.exportAsDrag(c, e, TransferHandler.COPY); }
From source file:Main.java
public void mouseClicked(MouseEvent evt) { JTable table = ((JTableHeader) evt.getSource()).getTable(); TableColumnModel colModel = table.getColumnModel(); int index = colModel.getColumnIndexAtX(evt.getX()); if (index == -1) { return;//from w w w . ja va 2 s .com } Rectangle headerRect = table.getTableHeader().getHeaderRect(index); if (index == 0) { headerRect.width -= 10; } else { headerRect.grow(-10, 0); } if (!headerRect.contains(evt.getX(), evt.getY())) { int vLeftColIndex = index; if (evt.getX() < headerRect.x) { vLeftColIndex--; } } }