A browser with JavaScript enabled is required for this page to operate properly.
Documentation

The Java™ Tutorials
Trail: Creating a GUI With Swing
Lesson: Drag and Drop and Data Transfer
« PreviousTrailNext »

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
See Dev.java for updated tutorials taking advantage of the latest releases.
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

CCP in a Text Component

If you are implementing cut, copy and paste using one of the Swing text components (text field, password field, formatted text field, or text area) your work is very straightforward. These text components utilize the DefaultEditorKit which provides built-in actions for cut, copy and paste. The default editor kit also handles the work of remembering which component last had the focus. This means that if the user initiates one of these actions using the menu or a keyboard equivalent, the correct component receives the action — no additional code is required.

The following demo, TextCutPaste, contains three text fields. As you can see in the screen shot, you can cut, copy, and paste to or from any of these text fields. They also support drag and drop.

[画像:A snapshot of the TextCutPaste demo.]

Try this:
  1. Click the Launch button to run TextCutPaste using Java™ Web Start (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult the example index.Launches the TextCutPaste example
  2. Select text in one of the text fields. Use the Edit menu or the keyboard equivalent to cut or copy the text from the source.
  3. Position the caret where you want the text to be pasted.
  4. Paste the text using the menu or the keyboard equivalent.
  5. Perform the same operation using drag and drop.

Here is the code that creates the Edit menu by hooking up the built-in cut, copy, and paste actions defined in DefaultEditorKit to the menu items. This works with any component that descends from JComponent:

 /**
 * Create an Edit menu to support cut/copy/paste.
 */
 public JMenuBar createMenuBar () {
 JMenuItem menuItem = null;
 JMenuBar menuBar = new JMenuBar();
 JMenu mainMenu = new JMenu("Edit");
 mainMenu.setMnemonic(KeyEvent.VK_E);
 menuItem = new JMenuItem(new DefaultEditorKit.CutAction());
 menuItem.setText("Cut");
 menuItem.setMnemonic(KeyEvent.VK_T);
 mainMenu.add(menuItem);
 menuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
 menuItem.setText("Copy");
 menuItem.setMnemonic(KeyEvent.VK_C);
 mainMenu.add(menuItem);
 menuItem = new JMenuItem(new DefaultEditorKit.PasteAction());
 menuItem.setText("Paste");
 menuItem.setMnemonic(KeyEvent.VK_P);
 mainMenu.add(menuItem);
 menuBar.add(mainMenu);
 return menuBar;
 }

Next we will look at how to accomplish the same functionality using a component that does not have the built-in support of the DefaultEditorKit.

« PreviousTrailNext »

Previous page: Adding Cut, Copy and Paste (CCP)
Next page: CCP in a non-Text Component

AltStyle によって変換されたページ (->オリジナル) /