The CUT (CTRL+X) command is used to cut selected text and save it to the clipboard, We can paste the text as per our requirements.
Approach
Below are the steps followed to perform the application of the clipboard.
- Open any Java IDE, Here we will be using Spring Tool Suite(STS) Java IDE.
- Go to FILE, After opening Java IDE
- Go to NEW i.e. press ALT+SHIFT+N
- Give the Project Name and fill in other needed fields accordingly.
- After filling in all the needed fields, click on Finish. Go to the src folder of the project that you have created. Right-click on the src file and create a package. Right-click on the package created above. Create a Java Class. The code is given below.
Java
package Java_Project_Package; import java.awt.datatransfer.*; import java.awt.event.*; import javax.swing.*; @SuppressWarnings ( "serial" ) public class ClipboardJavaExample extends JFrame { JMenuBar MenuBar = new JMenuBar(); JMenu File = new JMenu( "FILE" ); JMenu edit = new JMenu( "EDIT" ); JMenuItem cut = new JMenuItem( "CUT" ), copy = new JMenuItem( "COPY" ), paste = new JMenuItem( "PASTE" ); JTextArea TextArea = new JTextArea( 20 , 20 ); Clipboard Cboard = getToolkit().getSystemClipboard(); public ClipboardJavaExample() { cut.addActionListener( new Cut()); copy.addActionListener( new Copy()); paste.addActionListener( new Paste()); edit.add(cut); edit.add(copy); edit.add(paste); MenuBar.add(File); MenuBar.add(edit); setJMenuBar(MenuBar); getContentPane().add(TextArea); } class Copy implements ActionListener { public void actionPerformed(ActionEvent e) { String selection = TextArea.getSelectedText(); if (selection == null ) return ; StringSelection clipString = new StringSelection(selection); Cboard.setContents(clipString, clipString); } } class Cut implements ActionListener { public void actionPerformed(ActionEvent e) { String selection = TextArea.getSelectedText(); if (selection == null ) return ; StringSelection clipString = new StringSelection(selection); Cboard.setContents(clipString, clipString); TextArea.replaceRange( "" , TextArea.getSelectionStart(), TextArea.getSelectionEnd()); } } class Paste implements ActionListener { public void actionPerformed(ActionEvent e) { Transferable clipData = Cboard.getContents( ClipboardJavaExample. this ); try { String clipString = (String)clipData.getTransferData( DataFlavor.stringFlavor); TextArea.replaceRange( clipString, TextArea.getSelectionStart(), TextArea.getSelectionEnd()); } catch (Exception ex) { System.err.println( "NOT WORKING PROPERLY" ); } } } public static void main(String[] args) { JFrame frame = new ClipboardJavaExample(); frame.setTitle( "Application of Clipboard In Java Programming!" ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); frame.setSize( 320 , 200 ); frame.setVisible( true ); } } // This code is contributed by Rahul Chauhan |
- After writing the code, it will show errors in the written source code.
- IMPORT NEEDED PACKAGES, CLASSES, AND LIBRARIES.
- After importing and resolving all the errors, RUN the Java Project. Observe the events, A popup window will show. Write the content that you want to copy inside the appeared window.
- Go to EDIT option:
- As we can see clearly from the above image, EDIT is A drop-down menu, We can select CUT (CTRL+X), COPY( CTRL+C), OR PASTE( CTRL+V) as per our choice to check whether it is working properly or not.