This program demonstrates the transfer of text between a Java application and the system clipboard.
/*
This program is a part of the companion code for Core Java 8th ed.
(http://horstmann.com/corejava)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.awt.BorderLayout;
import java.awt.EventQueue;
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.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* This program demonstrates the transfer of text between a Java application and the system
* clipboard.
* @version 1.13 2007-08-16
* @author Cay Horstmann
*/
public class TextTransferTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new TextTransferFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
/**
* This frame has a text area and buttons for copying and pasting text.
*/
class TextTransferFrame extends JFrame
{
public TextTransferFrame()
{
setTitle("TextTransferTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
textArea = new JTextArea();
add(new JScrollPane(textArea), BorderLayout.CENTER);
JPanel panel = new JPanel();
JButton copyButton = new JButton("Copy");
panel.add(copyButton);
copyButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
copy();
}
});
JButton pasteButton = new JButton("Paste");
panel.add(pasteButton);
pasteButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
paste();
}
});
add(panel, BorderLayout.SOUTH);
}
/**
* Copies the selected text to the system clipboard.
*/
private void copy()
{
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
String text = textArea.getSelectedText();
if (text == null) text = textArea.getText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
}
/**
* Pastes the text from the system clipboard into the text area.
*/
private void paste()
{
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
DataFlavor flavor = DataFlavor.stringFlavor;
if (clipboard.isDataFlavorAvailable(flavor))
{
try
{
String text = (String) clipboard.getData(flavor);
textArea.replaceSelection(text);
}
catch (UnsupportedFlavorException e)
{
JOptionPane.showMessageDialog(this, e);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, e);
}
}
}
private JTextArea textArea;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 300;
}
Related examples in the same category