How to Paste a Java GUI

How to Paste a Java GUI thumbnail
Java works efficiently with both GUI and non-GUI components.

When it comes to dealing with graphical user interfaces (GUIs) and and adding resonant graphics functionality and integration to Java applications, Java Foundation Classes, or JFC, get the job done. JFC has many advanced and efficient features that directly mesh with GUI components, such as Swing and Java 2D. These application programming interfaces, or APIs, have CCP (cut, copy and paste) controls that can work with text and non-text or GUI elements.

Things You'll Need

  • Swing API
  • JList
Show More

Instructions

    • 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.

Tips & Warnings

  • There are various free, online tutorials for Java that you can practice on to get acquainted with this advanced application. You can also subscribe to discussion forums that deal with Java and seek advice from experienced users to learn more about this technology.

Related Searches:

References

Resources

  • Photo Credit Hemera Technologies/AbleStock.com/Getty Images

Comments

Related Ads

Featured