List of usage examples for javax.swing TransferHandler getCopyAction
public static Action getCopyAction()
From source file:DigitalClock.java
public DigitalClock() { // Set default values for our properties setFormat(DateFormat.getTimeInstance(DateFormat.MEDIUM, getLocale())); setUpdateFrequency(1000); // Update once a second // Specify a Swing TransferHandler object to do the dirty work of // copy-and-paste and drag-and-drop for us. This one will transfer // the value of the "time" property. Since this property is read-only // it will allow drags but not drops. setTransferHandler(new TransferHandler("time")); // Since JLabel does not normally support drag-and-drop, we need an // event handler to detect a drag and start the transfer. addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { getTransferHandler().exportAsDrag(DigitalClock.this, e, TransferHandler.COPY); }//from w w w . j ava 2s .c om }); // Before we can have a keyboard binding for a Copy command, // the component needs to be able to accept keyboard focus. setFocusable(true); // Request focus when we're clicked on addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { requestFocus(); } }); // Use a LineBorder to indicate when we've got the keyboard focus addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { setBorder(LineBorder.createBlackLineBorder()); } public void focusLost(FocusEvent e) { setBorder(null); } }); // Now bind the Ctrl-C keystroke to a "Copy" command. InputMap im = new InputMap(); im.setParent(getInputMap(WHEN_FOCUSED)); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK), "Copy"); setInputMap(WHEN_FOCUSED, im); // And bind the "Copy" command to a pre-defined Action that performs // a copy using the TransferHandler we've installed. ActionMap am = new ActionMap(); am.setParent(getActionMap()); am.put("Copy", TransferHandler.getCopyAction()); setActionMap(am); // Create a javax.swing.Timer object that will generate ActionEvents // to tell us when to update the displayed time. Every updateFrequency // milliseconds, this timer will cause the actionPerformed() method // to be invoked. (For non-GUI applications, see java.util.Timer.) timer = new Timer(updateFrequency, new ActionListener() { public void actionPerformed(ActionEvent e) { setText(getTime()); // set label to current time string } }); timer.setInitialDelay(0); // Do the first update immediately timer.start(); // Start timing now! }
From source file:ListCutPaste.java
/** * Create an Edit menu to support cut/copy/paste. *//*from w w w. ja v a 2 s . c o m*/ public JMenuBar createMenuBar() { JMenuItem menuItem = null; JMenuBar menuBar = new JMenuBar(); JMenu mainMenu = new JMenu("Edit"); mainMenu.setMnemonic(KeyEvent.VK_E); TransferActionListener actionListener = new TransferActionListener(); menuItem = new JMenuItem("Cut"); menuItem.setActionCommand((String) TransferHandler.getCutAction().getValue(Action.NAME)); menuItem.addActionListener(actionListener); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK)); menuItem.setMnemonic(KeyEvent.VK_T); mainMenu.add(menuItem); menuItem = new JMenuItem("Copy"); menuItem.setActionCommand((String) TransferHandler.getCopyAction().getValue(Action.NAME)); menuItem.addActionListener(actionListener); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK)); menuItem.setMnemonic(KeyEvent.VK_C); mainMenu.add(menuItem); menuItem = new JMenuItem("Paste"); menuItem.setActionCommand((String) TransferHandler.getPasteAction().getValue(Action.NAME)); menuItem.addActionListener(actionListener); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK)); menuItem.setMnemonic(KeyEvent.VK_P); mainMenu.add(menuItem); menuBar.add(mainMenu); return menuBar; }
From source file:DragPictureDemo2.java
public JMenuBar createMenuBar() { JMenuItem menuItem = null;//ww w.j ava 2 s .co m JMenuBar menuBar = new JMenuBar(); JMenu mainMenu = new JMenu("Edit"); mainMenu.setMnemonic(KeyEvent.VK_E); TransferActionListener actionListener = new TransferActionListener(); menuItem = new JMenuItem("Cut"); menuItem.setActionCommand((String) TransferHandler.getCutAction().getValue(Action.NAME)); menuItem.addActionListener(actionListener); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK)); menuItem.setMnemonic(KeyEvent.VK_T); mainMenu.add(menuItem); menuItem = new JMenuItem("Copy"); menuItem.setActionCommand((String) TransferHandler.getCopyAction().getValue(Action.NAME)); menuItem.addActionListener(actionListener); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK)); menuItem.setMnemonic(KeyEvent.VK_C); mainMenu.add(menuItem); menuItem = new JMenuItem("Paste"); menuItem.setActionCommand((String) TransferHandler.getPasteAction().getValue(Action.NAME)); menuItem.addActionListener(actionListener); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK)); menuItem.setMnemonic(KeyEvent.VK_P); mainMenu.add(menuItem); menuBar.add(mainMenu); return menuBar; }
From source file:ListCutPaste.java
/** * Add the cut/copy/paste actions to the action map. *///from www . j a v a 2 s. c om private void setMappings(JList list) { ActionMap map = list.getActionMap(); map.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction()); map.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction()); map.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction()); }
From source file:DragPictureDemo2.java
public DTPicture(Image image) { super(image); addMouseMotionListener(this); //Add the cut/copy/paste key bindings to the input map. //Note that this step is redundant if you are installing //menu accelerators that cause these actions to be invoked. //DragPictureDemo does not use menu accelerators and, since //the default value of installInputMapBindings is true, //the bindings are installed. DragPictureDemo2 does use //menu accelerators and so calls setInstallInputMapBindings //with a value of false. Your program would do one or the //other, but not both. if (installInputMapBindings) { InputMap imap = this.getInputMap(); imap.put(KeyStroke.getKeyStroke("ctrl X"), TransferHandler.getCutAction().getValue(Action.NAME)); imap.put(KeyStroke.getKeyStroke("ctrl C"), TransferHandler.getCopyAction().getValue(Action.NAME)); imap.put(KeyStroke.getKeyStroke("ctrl V"), TransferHandler.getPasteAction().getValue(Action.NAME)); }// w w w . ja v a 2s . co m //Add the cut/copy/paste actions to the action map. //This step is necessary because the menu's action listener //looks for these actions to fire. ActionMap map = this.getActionMap(); map.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction()); map.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction()); map.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction()); }
From source file:it.cnr.icar.eric.client.ui.swing.graph.JBGraph.java
/** * DOCUMENT ME!//from w w w . j av a 2 s .c om * * @return DOCUMENT ME! */ public JToolBar createToolBar() { JButton button = null; JToolBar toolbar = new JToolBar(); toolbar.setFloatable(false); //TODO: SwingBoost: Localize this TreeNode tempTreeNode = new DefaultMutableTreeNode("loading object types..."); objectTypeCombo = new it.cnr.icar.eric.client.ui.swing.TreeCombo(new DefaultTreeModel(tempTreeNode)); toolbar.add(objectTypeCombo); // use a SwingWorker to get the real model, since it might not have been initialized yet final SwingWorker worker = new SwingWorker(this) { public Object doNonUILogic() { ConceptsTreeModel objectTypesTreeModel = BusinessQueryPanel.getObjectTypesTreeModel(); return objectTypesTreeModel; } public void doUIUpdateLogic() { ConceptsTreeModel objectTypesTreeModel = (ConceptsTreeModel) get(); objectTypeCombo.setModel(objectTypesTreeModel); } }; worker.start(); // Insert URL insertUrl = getClass().getClassLoader().getResource("icons/insert.gif"); ImageIcon insertIcon = new ImageIcon(insertUrl); button = toolbar.add(new AbstractAction("", insertIcon) { /** * */ private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent e) { insert(new Point(10, 10)); } }); button.setText(""); //an icon-only button button.setToolTipText("Insert"); // Toggle Connect Mode URL connectUrl = getClass().getClassLoader().getResource("icons/connecton.gif"); ImageIcon connectIcon = new ImageIcon(connectUrl); button = toolbar.add(new AbstractAction("", connectIcon) { /** * */ private static final long serialVersionUID = 657528648199915209L; public void actionPerformed(ActionEvent e) { setPortsVisible(!isPortsVisible()); URL connectUrl; if (isPortsVisible()) { connectUrl = getClass().getClassLoader().getResource("icons/connecton.gif"); } else { connectUrl = getClass().getClassLoader().getResource("icons/connectoff.gif"); } ImageIcon connectIcon = new ImageIcon(connectUrl); putValue(SMALL_ICON, connectIcon); } }); button.setText(""); //an icon-only button button.setToolTipText("Toggle Connect Mode"); // Undo toolbar.addSeparator(); URL undoUrl = getClass().getClassLoader().getResource("icons/undo.gif"); ImageIcon undoIcon = new ImageIcon(undoUrl); undo = new AbstractAction("", undoIcon) { /** * */ private static final long serialVersionUID = -740055667372297781L; public void actionPerformed(ActionEvent e) { undo(); } }; undo.setEnabled(false); button = toolbar.add(undo); button.setText(""); //an icon-only button button.setToolTipText("Undo"); // Redo URL redoUrl = getClass().getClassLoader().getResource("icons/redo.gif"); ImageIcon redoIcon = new ImageIcon(redoUrl); redo = new AbstractAction("", redoIcon) { /** * */ private static final long serialVersionUID = 5021485220988522968L; public void actionPerformed(ActionEvent e) { redo(); } }; redo.setEnabled(false); button = toolbar.add(redo); button.setText(""); //an icon-only button button.setToolTipText("Redo"); // // Edit Block // toolbar.addSeparator(); Action action; URL url; // Copy action = TransferHandler.getCopyAction(); url = getClass().getClassLoader().getResource("icons/copy.gif"); action.putValue(Action.SMALL_ICON, new ImageIcon(url)); //Commented out until we can figure out how to assign new id to copied objects //button = toolbar.add(copy = new EventRedirector(action)); button.setText(""); //an icon-only button button.setToolTipText("Copy"); // Paste action = TransferHandler.getPasteAction(); url = getClass().getClassLoader().getResource("icons/paste.gif"); action.putValue(Action.SMALL_ICON, new ImageIcon(url)); //Commented out until we can figure out how to assign new id to copied objects //button = toolbar.add(paste = new EventRedirector(action)); button.setText(""); //an icon-only button button.setToolTipText("Paste"); // Cut action = TransferHandler.getCutAction(); url = getClass().getClassLoader().getResource("icons/cut.gif"); action.putValue(Action.SMALL_ICON, new ImageIcon(url)); //Commented out until we can figure out how to assign new id to copied objects //button = toolbar.add(cut = new EventRedirector(action)); button.setText(""); //an icon-only button button.setToolTipText("Cut"); // Remove URL removeUrl = getClass().getClassLoader().getResource("icons/delete.gif"); ImageIcon removeIcon = new ImageIcon(removeUrl); remove = new AbstractAction("", removeIcon) { /** * */ private static final long serialVersionUID = 6889927067487680474L; public void actionPerformed(ActionEvent e) { if (!isSelectionEmpty()) { Object[] cells = getSelectionCells(); cells = getDescendants(cells); getModel().remove(cells); //Remove entry from map of cells on the graph for (int i = 0; i < cells.length; i++) { Object cell = cells[i]; if (cell instanceof JBGraphCell) { RegistryObject ro = ((JBGraphCell) cell).getRegistryObject(); registryObjectToCellMap.remove(ro); } } } } }; remove.setEnabled(false); button = toolbar.add(remove); button.setText(""); //an icon-only button button.setToolTipText(resourceBundle.getString("menu.graphPanel.removeFromView")); // Zoom Std toolbar.addSeparator(); URL zoomUrl = getClass().getClassLoader().getResource("icons/zoom.gif"); ImageIcon zoomIcon = new ImageIcon(zoomUrl); button = toolbar.add(new AbstractAction("", zoomIcon) { /** * */ private static final long serialVersionUID = -4404610379022823602L; public void actionPerformed(ActionEvent e) { setScale(1.0); } }); button.setText(""); //an icon-only button button.setToolTipText("Zoom"); // Zoom In URL zoomInUrl = getClass().getClassLoader().getResource("icons/zoomin.gif"); ImageIcon zoomInIcon = new ImageIcon(zoomInUrl); button = toolbar.add(new AbstractAction("", zoomInIcon) { /** * */ private static final long serialVersionUID = 6782766891458235321L; public void actionPerformed(ActionEvent e) { setScale(2 * getScale()); } }); button.setText(""); //an icon-only button button.setToolTipText("Zoom In"); // Zoom Out URL zoomOutUrl = getClass().getClassLoader().getResource("icons/zoomout.gif"); ImageIcon zoomOutIcon = new ImageIcon(zoomOutUrl); button = toolbar.add(new AbstractAction("", zoomOutIcon) { /** * */ private static final long serialVersionUID = -5480242207934335070L; public void actionPerformed(ActionEvent e) { setScale(getScale() / 2); } }); button.setText(""); //an icon-only button button.setToolTipText("Zoom Out"); // Group /* toolbar.addSeparator(); URL groupUrl = getClass().getClassLoader().getResource("icons/group.gif"); ImageIcon groupIcon = new ImageIcon(groupUrl); group = new AbstractAction("", groupIcon) { public void actionPerformed(ActionEvent e) { group(getSelectionCells()); } }; group.setEnabled(false); //button = toolbar.add(group); button.setText(""); //an icon-only button button.setToolTipText("Group"); // Ungroup URL ungroupUrl = getClass().getClassLoader().getResource("icons/ungroup.gif"); ImageIcon ungroupIcon = new ImageIcon(ungroupUrl); ungroup = new AbstractAction("", ungroupIcon) { public void actionPerformed(ActionEvent e) { ungroup(getSelectionCells()); } }; ungroup.setEnabled(false); //button = toolbar.add(ungroup); button.setText(""); //an icon-only button button.setToolTipText("Ungroup"); */ // To Front toolbar.addSeparator(); URL toFrontUrl = getClass().getClassLoader().getResource("icons/tofront.gif"); ImageIcon toFrontIcon = new ImageIcon(toFrontUrl); tofront = new AbstractAction("", toFrontIcon) { /** * */ private static final long serialVersionUID = -4901428890590828561L; public void actionPerformed(ActionEvent e) { if (!isSelectionEmpty()) { toFront(getSelectionCells()); } } }; tofront.setEnabled(false); button = toolbar.add(tofront); button.setText(""); //an icon-only button button.setToolTipText("To Front"); // To Back URL toBackUrl = getClass().getClassLoader().getResource("icons/toback.gif"); ImageIcon toBackIcon = new ImageIcon(toBackUrl); toback = new AbstractAction("", toBackIcon) { /** * */ private static final long serialVersionUID = -5942025518651424307L; public void actionPerformed(ActionEvent e) { if (!isSelectionEmpty()) { toBack(getSelectionCells()); } } }; toback.setEnabled(false); button = toolbar.add(toback); button.setText(""); //an icon-only button button.setToolTipText("To Back"); return toolbar; }
From source file:org.nuclos.client.explorer.ExplorerNode.java
/** * @param tree the tree where the action is about to take place. * @return the list of possible <code>TreeNodeAction</code>s for this node. * These may be shown in the node's context menu. * Separators are shown in the menus for <code>null</code> entries. */// w w w.j a va 2 s .c om public List<TreeNodeAction> getTreeNodeActions(JTree tree) { final List<TreeNodeAction> result = new LinkedList<TreeNodeAction>(); result.add(new RefreshAction(tree)); final ShowInOwnTabAction actShowInOwnTab = new ShowInOwnTabAction(tree); actShowInOwnTab.setEnabled(!this.getTreeNode().needsParent()); result.add(actShowInOwnTab); result.addAll(getExpandCollapseActions(tree)); final TreeNodeAction exploreractCopy = new ChainedTreeNodeAction(ACTIONCOMMAND_COPY, getSpringLocaleDelegate().getMessage("ExplorerNode.4", "Kopieren"), TransferHandler.getCopyAction(), tree); result.add(exploreractCopy); final TreeNodeAction exploreractPaste = new ChainedTreeNodeAction(ACTIONCOMMAND_PASTE, getSpringLocaleDelegate().getMessage("ExplorerNode.2", "Einf\u00fcgen"), TransferHandler.getPasteAction(), tree); result.add(exploreractPaste); // enable "copy" action according to the tree's TransferHandler: final boolean bCopyEnabled = (tree.getTransferHandler().getSourceActions(tree) & TransferHandler.COPY) != 0; exploreractCopy.setEnabled(bCopyEnabled); // enable "paste" action according to the tree's TransferHandler: // This is hard because TransferHandler.canImport is not called every time the selection changes. // Workaround: call canImport anyway to reduce bad paste actions: final DataFlavor[] aflavors = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(this) .getTransferDataFlavors(); final boolean bPasteEnabled = tree.getTransferHandler().canImport(tree, aflavors); exploreractPaste.setEnabled(bPasteEnabled); return result; }
From source file:org.rdv.datapanel.DigitalTabularDataPanel.java
private void addColumn() { if (columnGroupCount == MAX_COLUMN_GROUP_COUNT) { return;/*from w w w . j a v a 2s .c o m*/ } if (columnGroupCount != 0) { panelBox.add(Box.createHorizontalStrut(7)); } final int tableIndex = columnGroupCount; final DataTableModel tableModel = new DataTableModel(); final JTable table = new JTable(tableModel); table.setDragEnabled(true); table.setName(DigitalTabularDataPanel.class.getName() + " JTable #" + Integer.toString(columnGroupCount)); if (showThresholdCheckBoxGroup.isSelected()) { tableModel.setThresholdVisible(true); } if (showMinMaxCheckBoxGroup.isSelected()) { tableModel.setMaxMinVisible(true); table.getColumn("Min").setCellRenderer(doubleCellRenderer); table.getColumn("Max").setCellRenderer(doubleCellRenderer); } table.getColumn("Value").setCellRenderer(dataCellRenderer); tables.add(table); tableModels.add(tableModel); JScrollPane tableScrollPane = new JScrollPane(table); panelBox.add(tableScrollPane); // popup menu for panel JPopupMenu popupMenu = new JPopupMenu(); final JMenuItem copyMenuItem = new JMenuItem("Copy"); copyMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { TransferHandler.getCopyAction().actionPerformed( new ActionEvent(table, ae.getID(), ae.getActionCommand(), ae.getWhen(), ae.getModifiers())); } }); popupMenu.add(copyMenuItem); popupMenu.addSeparator(); JMenuItem printMenuItem = new JMenuItem("Print..."); printMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { table.print(JTable.PrintMode.FIT_WIDTH); } catch (PrinterException pe) { } } }); popupMenu.add(printMenuItem); popupMenu.addSeparator(); final JCheckBoxMenuItem showMaxMinMenuItem = new JCheckBoxMenuItem("Show min/max columns", false); showMaxMinMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { setMaxMinVisible(showMaxMinMenuItem.isSelected()); } }); showMinMaxCheckBoxGroup.addCheckBox(showMaxMinMenuItem); popupMenu.add(showMaxMinMenuItem); final JCheckBoxMenuItem showThresholdMenuItem = new JCheckBoxMenuItem("Show threshold columns", false); showThresholdMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { setThresholdVisible(showThresholdMenuItem.isSelected()); } }); showThresholdCheckBoxGroup.addCheckBox(showThresholdMenuItem); popupMenu.add(showThresholdMenuItem); popupMenu.addSeparator(); JMenuItem blankRowMenuItem = new JMenuItem("Insert blank row"); blankRowMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tableModel.addBlankRow(); } }); popupMenu.add(blankRowMenuItem); final JMenuItem removeSelectedRowsMenuItem = new JMenuItem("Remove selected rows"); removeSelectedRowsMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { removeSelectedRows(tableIndex); } }); popupMenu.add(removeSelectedRowsMenuItem); popupMenu.addSeparator(); JMenu numberOfColumnsMenu = new JMenu("Number of columns"); numberOfColumnsMenu.addMenuListener(new MenuListener() { public void menuSelected(MenuEvent me) { JMenu menu = (JMenu) me.getSource(); for (int j = 0; j < MAX_COLUMN_GROUP_COUNT; j++) { JMenuItem menuItem = menu.getItem(j); boolean selected = (j == (columnGroupCount - 1)); menuItem.setSelected(selected); } } public void menuDeselected(MenuEvent me) { } public void menuCanceled(MenuEvent me) { } }); for (int i = 0; i < MAX_COLUMN_GROUP_COUNT; i++) { final int number = i + 1; JRadioButtonMenuItem item = new JRadioButtonMenuItem(Integer.toString(number)); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { setNumberOfColumns(number); } }); numberOfColumnsMenu.add(item); } popupMenu.add(numberOfColumnsMenu); popupMenu.addPopupMenuListener(new PopupMenuListener() { public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) { boolean anyRowsSelected = table.getSelectedRowCount() > 0; copyMenuItem.setEnabled(anyRowsSelected); removeSelectedRowsMenuItem.setEnabled(anyRowsSelected); } public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) { } public void popupMenuCanceled(PopupMenuEvent arg0) { } }); // set component popup and mouselistener to trigger it table.setComponentPopupMenu(popupMenu); tableScrollPane.setComponentPopupMenu(popupMenu); panelBox.revalidate(); columnGroupCount++; properties.setProperty("numberOfColumns", Integer.toString(columnGroupCount)); }
From source file:us.daveread.basicquery.BasicQuery.java
/** * Copy the selected data cells to the clipboard *///w ww . ja v a 2 s.c o m private void copySelectionToClipboard() { Action objlCopy; try { objlCopy = TransferHandler.getCopyAction(); objlCopy.actionPerformed(new ActionEvent(table, ActionEvent.ACTION_PERFORMED, (String) objlCopy.getValue(Action.NAME), EventQueue.getMostRecentEventTime(), 0)); } catch (Throwable any) { LOGGER.error("Failed to copy data to clipboard", any); JOptionPane.showMessageDialog(this, Resources.getString("errClipboardCopyDataText", any.getClass().getName(), any.getMessage() != null ? any.getMessage() : ""), Resources.getString("errClipboardCopyDataTitle"), JOptionPane.ERROR_MESSAGE); } }