This issue presents tips, techniques, and sample code for the following topics:
Cut, copy, and paste
If you use GUI-based applications, you're probably familiar with the
cut/copy/paste feature. JavaTM Foundation Classes (JFC)/project Swing
components such as JTextArea and JEditorPane
support cut/copy/paste. You can copy or cut text to the system clipboard,
a storage area that is separate from applications. Once in the clipboard,
the text is available to Java or non-Java applications.
It's instructive to see how this feature is implemented. Here is an
example that shows the underlying mechanism of cut/copy/paste:
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import javax.swing.*;
public class copy {
public static void main(String args[]) {
// set up frames, panels, text areas
JFrame frame = new JFrame("Clipboard demo");
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
final JTextArea textarea = new JTextArea(10, 40);
// create buttons and set up listeners for them
JPanel buttonpanel = new JPanel();
JButton copybutton = new JButton("Copy");
JButton pastebutton = new JButton("Paste");
JButton exitbutton = new JButton("Exit");
copybutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Clipboard cb =
Toolkit.getDefaultToolkit().
getSystemClipboard();
String s = textarea.getText();
StringSelection contents =
new StringSelection(s);
cb.setContents(contents, null);
textarea.setText("");
}
});
pastebutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Clipboard cb =
Toolkit.getDefaultToolkit().
getSystemClipboard();
Transferable content =
cb.getContents(this);
try {
String s =
(String)content.
getTransferData(DataFlavor.
stringFlavor);
textarea.setText(s);
}
catch (Throwable exc) {
System.err.println(e);
}
}
});
exitbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
buttonpanel.add(copybutton);
buttonpanel.add(pastebutton);
buttonpanel.add(exitbutton);
panel.add("North", textarea);
panel.add("South", buttonpanel);
// make frame visible
frame.getContentPane().add("Center", panel);
frame.pack();
frame.setVisible(true);
}
}
This demo sets up a JTextArea and a Copy button. Copy copies
the contents of the text area to the system clipboard; it then erases the
text area. The demo also sets up a Paste button. Paste replaces the contents
of the text area with the system clipboard contents. A line of code such as:
Clipboard cb =
Toolkit.getDefaultToolkit().getSystemClipboard();
gets the system clipboard object.
To actually copy data from the text area to the clipboard, a string that
contains the content of the text area is obtained. Then a
StringSelection object is created. StringSelection
is an implementation of the java.awt.datatransfer.Transferable
interface. This is an interface that describes what functionality a class
must provide to serve as a data carrier during a transfer operation. In other
words, if text is copied to the system clipboard, there needs to be a data
transfer format of some type. Two of these formats ("data flavors")
are plain text and String class objects.
Copying data from the system clipboard is a matter of reversing the process.
The clipboard object is obtained, a Transferable object is read
from it, and then a string is retrieved from the Transferable
object.
Data copied to the clipboard can be retrieved in other applications.
For example, after running the demo above, if this C++ application
(written using Borland C++Builder 4) is run:
// Compile with:
// bcc32 paste.c $LIB/vcl.lib $LIB/cp32mt.lib
// where $LIB is the library directory for C++Builder 4
int main() {
TClipboard* cb = Clipboard();
char textbuf[1024];
cb->GetTextBuf(textbuf, sizeof textbuf);
printf("%s\n", textbuf);
return 0;
}
it will display the content that was copied to the clipboard.
Package version identification
A new feature in Java 2 is the ability to get version information
about a class package at run time. In other words, you might have
an application that loads a class, and you'd like to know what
version of the Java specification is implemented by the class's
package (such as 1.1 or 1.2).
To see how this works, consider an example that loads a class
specified on the command line:
public class loadclass {
public static void main(String args[]) {
// check argument
if (args.length != 1) {
System.err.println("missing classname");
System.exit(1);
}
// load class
Class c = null;
try {
c = Class.forName(args[0]);
}
catch (ClassNotFoundException e) {
System.err.println(e);
System.exit(1);
}
// retrieve and check package information
Package pkg = c.getPackage();
if (pkg == null) {
System.out.println("No version information");
}
else {
System.out.println(pkg);
if (pkg.isCompatibleWith("1.2"))
System.out.println("Compatible with 1.2");
}
}
}
If you run the program by entering:
$ java loadclass java.util.Vector
the output is:
package java.util, Java Platform API Specification,
version 1.2.0
Compatible with 1.2
In this example, the package java.util has specification version
1.2.0 (that is, Java 2), and is compatible with 1.2. "Compatible"
means that the version of a package is at least as high as the desired version
specified to the isCompatibleWith method call. You can use this
type of check in an application if you want a loaded class to meet a particular
version specification. Note that all classes in a package have a single
Package object, and so have the same version information.
The version information is obtained by the class loader, typically from a Jar
file manifest (see Tech Tips for October
20, 1998). The information may not be available, in which case
getPackage returns null.
You can use the method:
Package.getPackages()
to get all packages known to the class loader.
See also Tech Tips for December 15,
1998. It describes the -target option to javac .
You can use this option to generate .class files for a specific
version of the JVM (Java1 Virtual Machine).
The JDC Tech Tips are written by Glen McCluskey.
_______
1 As used on this web site, the terms
"Java virtual machine" or "JVM" mean a virtual machine for the Java platform.
|