Technology Programming

How to Paste a Java GUI

    • 1). Insert the following code in the action map to install cut, copy and paste actions of the Java Swing components:

      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());

    • 2). Add the following code snippet to set up the CCP bindings to your project's input map:

      // only required if you have not set the menu accelerators

      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));

      Alternatively, insert the following code to enable cutting and copy actions of Java Swing GUI components:

      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);

    • 3). Add the following class code called TransferActionListener to any part of your Swing application to designate which component should receive the CCP action:

      public class TransferActionListener implements ActionListener,

      PropertyChangeListener {

      private JComponent focusOwner = null;

      public TransferActionListener() {

      KeyboardFocusManager manager = KeyboardFocusManager.

      getCurrentKeyboardFocusManager();

      manager.addPropertyChangeListener("permanentFocusOwner", this);

      }

      public void propertyChange(PropertyChangeEvent e) {

      Object o = e.getNewValue();

      if (o instanceof JComponent) {

      focusOwner = (JComponent)o;

      } else {

      focusOwner = null;

      }

      }

      public void actionPerformed(ActionEvent e) {

      if (focusOwner == null)

      return;

      String action = (String)e.getActionCommand();

      Action a = focusOwner.getActionMap().get(action);

      if (a != null) {

      a.actionPerformed(new ActionEvent(focusOwner,

      ActionEvent.ACTION_PERFORMED,

      null));

      }

      }

      }

    • 4). Download the Java Development Kit 6 from the Oracle website and install it on your machine. Click the "Launch" button to run ListCutPaste from the main menu. Choose an item from the displayed lists and use the "Edit" menu or the keyboard to cut or copy from the source the list item you prefer. Choose the list item where you want to paste your GUI object. Paste it using either Edit menu or its keyboard equivalent. This action basically performs the same drag and drop operation.

Related posts "Technology : Programming"

Differences Between Byte Array Vs. String

Programming

Web Design Company UK/

Programming

Develop A Quality Plan With These Self Help Tips

Programming

TMediaPlayer: What track am I on?

Programming

How You Can Prove Your Expertise

Programming

Furniture Advice And Tips And Also Hardwearing. Property Searching Wonderful

Programming

Web Design Fort Lauderdale Business

Programming

Hire Web Designer and Diminish Development Expenditure

Programming

Web Masters.

Programming

Leave a Comment