Copy data from one JTextField to another JTextField

In this example, you will learn how to copy the data from one JTextField into another JTextField.

Copy data from one JTextField to another JTextField

Copy data from one JTextField to another JTextField

     

In this example, you will learn how to copy the data from one JTextField into another JTextField. When you run this program it starts with two JTextField. Enter some data into first JTextfield and select the entered text, right click, copy and then paste into another JTextField.

JTextField is used in the swing application to develop user input box. It is a lightweight component that allows the user to enter and edit the data in a single line. 

You can use JTextField into your swing applications to develop data entry forms.

Output:

When you run the program following output is displayed:

Enter some data into first text field (cell1). Highlight all the text in first cell. After selecting you right click the mouse button and copy the text of cell 1.

And go cell 2 paste the copied text to cell 2.

Here is the code of Program:

import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.table.*;

public class TCPopupEventQueue extends EventQueue {
  public JPopupMenu popup;
   JTable table;
  public BasicAction cut, copy, paste, selectAll;
  
  public TCPopupEventQueue() {
    //createPopupMenu();
  }
  
  public void createPopupMenu(JTextComponent text){
    cut = new CutAction("Cut",null);
    copy = new CopyAction("Copy", null);
    paste = new PasteAction("Paste",null);
    selectAll = new SelectAllAction("Select All",null);
    cut.setTextComponent(text);
    copy.setTextComponent(text);
    paste.setTextComponent(text);
    selectAll.setTextComponent(text);
    
    popup = new JPopupMenu();
    popup.add( cut );
    popup.add( copy );
    popup.add( paste );
    popup.addSeparator();
    popup.add( selectAll );
  }
  
  public void showPopup(Component parent, MouseEvent me){
    popup.validate();
    popup.show(parent, me.getX(), me.getY());
  }
  
  protected void dispatchEvent(AWTEvent event){
    super.dispatchEvent(event);
    if(!(event instanceof MouseEvent)){
      return;
    }
    MouseEvent me = (MouseEvent)event;
    if(!me.isPopupTrigger()) {
      return;
    }
    if( !(me.getSource() instanceof Component) ) {
      return;
    }
    Component comp = SwingUtilities.getDeepestComponentAt((Component)
me.getSource(),me.getX(), me.getY());
    if( !(comp instanceof JTextComponent)){
      return;
    }
    if(MenuSelectionManager.defaultManager().getSelectedPath().length > 0){
      return;
    }
    createPopupMenu((JTextComponent)comp);
    showPopup((Component)me.getSource(), me);
  }
  public abstract class BasicAction extends AbstractAction{
    JTextComponent comp;
    
    public BasicAction(String text, Icon icon) {
      super(text, icon);
      putValue(Action.SHORT_DESCRIPTION, text);
    }
    public void setTextComponent(JTextComponent comp){
      this.comp = comp;
    }
    public abstract void actionPerformed(ActionEvent e);
  }
  public class CutAction extends BasicAction {
    public CutAction(String text, Icon icon) {
      super(text, icon);
      putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl X"));
    }
    public void actionPerformed(ActionEvent e){
      comp.cut();
    }
    public boolean isEnabled(){
      return comp != null && comp.isEditable() && comp.getSelectedText() != null;
    }
  }
  public class CopyAction extends BasicAction{
    public CopyAction(String text, Icon icon){
      super(text,icon);
      putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl C"));
    }
    public void actionPerformed(ActionEvent e) {
      comp.copy();
    }
    public boolean isEnabled() {
      return comp != null && comp.getSelectedText() != null;
    }
  }
  public class PasteAction extends BasicAction{
    public PasteAction(String text, Icon icon){
      super(text,icon);
      putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl V"));
    }
    public void actionPerformed(ActionEvent e) {
      comp.paste();
    }
    public boolean isEnabled() {
      Transferable content = Toolkit.getDefaultToolkit().getSystemClipboard()
.getContents(null);
      return comp != null && comp.isEnabled() && comp.isEditable() 
            && content.isDataFlavorSupported(DataFlavor.stringFlavor);
    }
  }

  public class SelectAllAction extends BasicAction{
    public SelectAllAction(String text, Icon icon){
      super(text,icon);
      putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl A"));
    }
    public void actionPerformed(ActionEvent e){
      comp.selectAll();
    }
    public boolean isEnabled() {
      return comp != null && comp.isEnabled() && comp.getText().length() > 0 
          && (comp.getSelectedText() == null || 
            comp.getSelectedText().length() < comp.getText().length());
    }
  }
  public static void main(String[] args) {
    Toolkit.getDefaultToolkit().getSystemEventQueue().push( new TCPopupEventQueue());
    JTextField field = new JTextField(20);
    JTextField field1 = new JTextField(20);
    JPanel center = new JPanel( new FlowLayout(FlowLayout.LEFT) );

    center.add(new JLabel("cell1:"));
    center.add(field);
    center.add(new JLabel("cell2:"));
    center.add(field1);
    JPanel content = new JPanel( new FlowLayout(FlowLayout.LEFT) );
    content.add( center, BorderLayout.SOUTH );
    JFrame frame = new JFrame("cell copy past program");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(content);
    frame.setSize(550,100);
    frame.setVisible(true);
  }
}

Download the code discussed here.

Tutorials

  1. Display Image in Java
  2. Show Coordinates
  3. What is Java Swing?
  4. Creating a Frame
  5. Setting an Icon for a Frame in Java
  6. Show Dialog Box in Java
  7. Show message and confirm dialog box
  8. Show input dialog box
  9. Changing the Label of a JButton Component in Java
  10. Setting Multi-Line label on the Button
  11. Setting icon on the button in Java
  12. Making a Frame Non Resizable in Java
  13. Remove Minimize and Maximize Button of Frame in Java
  14. Removing the Title Bar of a Frame
  15. Setting Bounds for a maximized frame
  16. Iconifying and Maximizing a frame in Java
  17. Making a component drag gable in java
  18. Create a ToolBar in Java
  19. Animating Images in Java Application
  20. Drawing with Color in Java
  21. Drawing with Gradient Color in Java
  22. Adding a Rollover and Pressed Icon to a JButton Component in Java
  23. Creating Check Box in Java Swing
  24. Customize the Icon in a JCheckBox Component of Java Swing
  25. Create a JComboBox Component in Java
  26. Combo Box operation in Java Swing
  27. Create a JRadioButton Component in Java
  28. Selecting a Radio Button component in Java
  29. Create a JList Component in Java
  30. Setting Tool Tip Text for items in a JList Component
  31. Setting the Dimensions of an Item in a JList Component in Java
  32. Create a JSpinner Component in Java
  33. Show Time in JSpinner Component
  34. Disabling Keyboard Editing in a JSpinner Component
  35. Limiting the Values in a Number JSpinner Component
  36. JSlider Component of Java Swing
  37. Progress Bar in Java Swing
  38. Create menus and submenus in Java
  39. Crate a Popup Menu in Java
  40. Create a Popup Menus with Nested Menus in Java