• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How To Update Clipboard Content In Java

January 28, 2014 //  by Krishna Srinivasan

If you are working on the tools development or similar product developments , then system level programming or OS interaction operations are necessary to access the native data. One of the most frequently used operation is the clipboard data. In particular, if you write program to develop own editors like notepad, clipboard operation is very common. Java provides Toolkit class which defines the static resources to access the various API to perform operation. getSystemClipboard method in Toolkit class would help you to update the content to clipboard.

This example program would store the simple to text to clipboard and then display from the clipboard. Lets look at the example.

ClipboardExample.java

package javabeat.net.core;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class ClipboardExample {
	public static void main(String[] args) {
		String str = "Java Clipboard Example Text!!";

		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Clipboard clipboard = toolkit.getSystemClipboard();
		StringSelection strSel = new StringSelection(str);
		clipboard.setContents(strSel, null);
		String result = null;
		try{
			result = (String) clipboard.getData(DataFlavor.stringFlavor);
		}catch (IOException exception){
			exception.printStackTrace();
		}catch (UnsupportedFlavorException exception){
			exception.printStackTrace();
		}
		System.out.println("Clipboard Text :" + result);
	}
}

Output…

Clipboard Text :Java Clipboard Example Text!!

Category: JavaTag: Java System Programming

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How To Display Currency For A Locale In Java
Next Post: OCMJEA Mock Exam -1 »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Implement getActiveCount() Method of ThreadPoolExeceutor in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact