Received: from SOUTH-STATION-ANNEX.MIT.EDU by po10.MIT.EDU (5.61/4.7) id AA27599; Wed, 14 Apr 99 06:07:16 EDT
Received: from hermes.javasoft.com by MIT.EDU with SMTP
	id AA00474; Wed, 14 Apr 99 06:06:50 EDT
Received: by hermes.java.sun.com (SMI-8.6/SMI-SVR4)
	id KAA02499; Wed, 14 Apr 1999 10:29:12 GMT
Date: Wed, 14 Apr 1999 10:29:12 GMT
Message-Id: <199904141029.KAA02499@hermes.java.sun.com>
X-Mailing: 102
From: JDCTechTips@sun.com
Subject: JDC Tech Tips No.
To: JDCMember@sun.com
Reply-To: JDCTechTips@sun.com
Errors-To: JDCMailErrors@sun.com
Precedence: junk
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Mailer: Beyond Email 2.1


 J  D  C    T  E  C  H    T  I  P  S

                      TIPS, TECHNIQUES, AND SAMPLE CODE


WELCOME to the Java Developer Connection(sm) Tech Tips, Vol. 2 No. 9.
This issue covers:
                      * Cut, copy, and paste
                      * Package version identification


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


T I P S ,  T E C H N I Q U E S ,   A N D   S A M P L E   C O D E



CUT, COPY, AND PASTE

If you use GUI-based applications, you're probably familiar with  
the cut/copy/paste feature. Java (tm) 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

    #include <stdio.h>
    #include <vcl/clipbrd.hpp>

    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 No. 15, 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 No. 17 (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 (Java Virtual 
Machine). 
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

- NOTE
The names on the JDC mailing list are used for internal Sun
Microsystems(tm) purposes only.  To remove your name from the list,
see Subscribe/Unsubscribe below.


- FEEDBACK
Comments?  Send your feedback on the JDC Tech Tips to:

JDCTechTips@Sun.com


- SUBSCRIBE/UNSUBSCRIBE
The JDC Tech Tips are sent to you because you elected to subscribe
when you registered as a JDC member.  To unsubscribe from JDC Email,
go to the following address and enter the email address you wish to
remove from the mailing list:

http://developer.java.sun.com/unsubscribe.html


To become a JDC member and subscribe to this newsletter go to:

http://java.sun.com/jdc/


- ARCHIVES
You'll find the JDC Tech Tips archives at:

http://developer.java.sun.com/developer/javaInDepth/TechTips/index.html


- COPYRIGHT
Copyright 1999 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.

This document is protected by copyright.  For more information, see:

http://developer.java.sun.com/developer/copyright.html


The JDC Tech Tips are written by Glen McCluskey.

JDC Tech Tips Vol. 2 No. 9
April 13, 1999





