(Java) Pure Java 2
(Java) Pure Java 2
All Books
Search
Pure Java 2 is a substantial and focused reference for professional Java programmers. This book begins with an accelerated introduction to Java 2 so that you can quickly understand the new concepts and begin developing your own applications. Professional programmers prefer to learn by examining code, so Pure Java 2 also contains hundreds of programming techniques, complete with well-commented code examples that you can immediately use in your own Java programs.
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing 2002, O'Reilly & Associates, Inc.
All Books
Search
PURE Java 2
Copyright 2000 by Sams Publishing
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. Library of Congress Catalog Card Number: 99-63646 Printed in the United States of America First Printing: December 1999 02 01 00 99 4 3 2 1
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Java is a trademark of Sun Microsystems, Inc.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an "as is" basis. The authors and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book.
Dedication
To my wife Amanda, who gave me time to write this book and who proofread my work till all hours of the night, and to Matthew, Wesley, and Daniel, who waited anxiously for Dad to finish "the book."
Delivered for Maurice ling Swap Option Available: 7/15/2002 2002, O'Reilly & Associates, Inc. Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing
All Books
Search
PURE Java 2
Acknowledgments About the Author Tell Us What You Think! Introduction The Audience for This Book How This Book Is Organized Features of This Book I: CONCEPTUAL REFERENCE 1. What Is Java? Java Overview The Java Virtual Machine Garbage Collector The Java 2 SDK 2. New Features in Java 2 Swing/Java Foundation Classes The Policy-Based Security Model RMI Enhancements JDBC 2.0 Enhancements 3. Java Program Features Basic Layout Names Punctuation Required Methods 4. Data Types Primitives Classes How Objects Are Created by the JVM Initialization of Data Members Abstract Classes Final Classes Some Important Common Classes 5. Methods Method Signatures Parameter Names this Modifiers Access Modifiers Other Modifiers
Overloading Methods Polymorphism: Overriding Methods 6. Interfaces An Example from GUI Event-Handling Interfaces Interface Contents 7. Syntax Punctuation Keywords Operators Control Flow Modifying Control Flow: break and continue II: TECHNIQUES REFERENCE 8. GUI Programming I: Applications Basic GUI Application Programming Layout Managers Absolute Positioning Leaving Positioning to Layout Managers Java Foundation Classes Using Delegation Event Handling Adapter Classes Using Drag and Drop in Java 9. GUI Programming II: Applets An Applet Overview Applet Security Considerations Main Applet Methods Running Applets Applet Programming 10. Exceptions The Throwable Class 11. Multithreaded Programming in Java Thread States Creating Threads Primary Thread Methods Using Synchronization Avoiding Deadlocks with wait() and notify() 12. I/O in Java A Quick Overview of Stream An Overview of the Main Stream, Reader, and Writer Classes Using Files in Java Performing I/O in Java 13. Reflection Discovering the Name and Attributes of a Class Getting Constructor and Method Information with Reflection Discovering Interface Information Modifying Data Members and Calling Methods 14. Java Network Programming Core Networking Concepts
Working with Host Names and IP Addresses Creating a TCP Client and Server Application Using Datagram Packets and UDP Sockets Working with URLs in Java 15. RMI RMI Architecture Overview Parameters for Remote Methods Finding Remote Objects Calling a Remote Object's Methods Calling a Remote Object with a Remote Object Parameter: Code Examples Creating a Remote Object from a Client: Remote Activation Code Examples 16. Java Security Using Policy-Based Security Creating a Policy File Sample Code for Digital Signing Procedure for Digitally Signing Code Creating Custom Permissions Message Digests 17. JDBC: Java Database Programming Creating a Database Connection Using Callable Statements JDBC 2.0 Techniques Other Important Points to Remember 18. Text Basic Internationalization in a Java Application Formatting Text Formatting Numeric Data Formatting Dates and Times 19. Utility Classes The Collection Framework Collection Interfaces Collection Implementations GregorianCalendar Using Properties III: SYNTAX REFERENCE 20. java.applet Package Name: java.applet Interface and Class Details 21. java.awt Package Name: java.awt Package Name: java.awt.datatransfer Package Name: java.awt.dnd Package Name: java.awt.event Package Name: java.awt.font Package Name: java.awt.geom Package Name: java.awt.print java.awt Interface and Class Details java.awt.datatransfer Interface and Class Details java.awt.dnd Interface and Class Details
java.awt.event Interface and Class Details java.awt.print Interface and Class Details 22. java.beans Package Name: java.beans Package Name: java.beans.beancontext java.beans Interface and Class Details 23. java.io Package Name: java.io Interface and Class Details 24. java.lang Inter faces Clas ses Excepti ons Errors Interface and Class Details 25. java.lang.reflect Inter faces Clas ses Interface and Class Details 26. java.math Clas ses Class Details 27. java.net Inter faces Clas ses Exce ptions Interface and Class Details 28. java.rmi Inter faces Clas ses Excep tions Interface and Class Details 29. java.rmi.server and java.rmi.activation Package Name: java.rmi.activation Package Name: java.rmi.server java.rmi.server Interface and Class Details java.rmi.activation Class Details 30. java.security Package Name: java.security Package Name: java.security.cert java.security.cert Interface and Class Details java.security.cert Class Details 31. java.sql Inter faces Clas ses Excep tions Interface and Class Details
32. java.text Interfaces Classes Exception Class Details 33. java.util Inter faces Clas ses Excep tions Interface and Class Details 34. java.util.jar Clas ses Excep tions Class Details
Delivered for Maurice ling Swap Option Available: 7/15/2002 2002, O'Reilly & Associates, Inc. Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing
All Books
Search
CONTINUE >
Acknowledgments
I want to acknowledge the many people who have helped me with coding issues and technical details. They include my colleagues at Sun Educational Services, especially Simon Roberts and more than one JavaSoft engineer, without whose help this book would not have been possible. I would also like to mention the help I have gotten from numerous individuals on the RMI-Users mailing list run by Sun. Of course, I also want to acknowledge my editors at Sams, who have borne with my often slow progress and individual perspective. I appreciate Macmillan for giving me the opportunity to improve my own Java knowledge and publish a book at the same time. Finally, this book would not have seen daylight without the help of my supportive wife, Amanda. She has been my proofreader, laughing with me through countless bizarre or amusing typos.
Delivered for Maurice ling Swap Option Available: 7/15/2002 Make Note | Bookmark 2002, O'Reilly & Associates, Inc. Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008180098047171073173049
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008180098047174160235095
To download the code in this book, visit our Web site at http://www.samspublishing.com/product_support, and then enter this book's ISBN: 0672316544.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark 2002, O'Reilly & Associates, Inc. Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008180098046010035140098
Introduction
The latest version of Java, renamed from JDK 1.2 to Java 2 SDK Standard Edition Version 1.2, is a significant enhancement to the Java programming language. This version has many new features, such as Swing for coding cool-looking GUIs; a new Collection Framework, which adds support for dealing with collections of objects; and the Extension mechanism for adding your own classes to Java's SDK classes. Java now offers new JDBC features such as scrollable result sets, remote activation of RMI objects, and improved performance. There are many reasons to move from JDK 1.0 or 1.1 to Java 2. This book is designed to help you make this transition. If you are new to Java, you'll find here a rich API set to accomplish almost any programming task you need. This book is designed to help you put the features of Java 2 to optimal use.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark 2002, O'Reilly & Associates, Inc. Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > Introduction > The Audience for This Book < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021092067008180098046008050044088
How an external event-handling class can communicate back to the GUI that registered the listener object How RMI performs better, as a rule, if you pass remote object references rather than passing objects by value How you can use remote objects as parameters in an RMI remote method invocation How to get data from the dialog box back to the main GUI screen How to use resource bundles How to format data when displaying it
q q q q
Among techniques that are important to know, I've provided examples for doing RMI remote activation and examples of using MediaTracker to monitor the downloading of multiple images for an applet. Along with these and other techniques, I've provided ample notes, tips, and cautions useful to all Java programmers who want to write better code and avoid common pitfalls. For the most part, you'll be able to pick up this book, find the subject you want to write code for, and find an example of how to do it, along with any advice I think is impor tant. You will also find many tips and suggestions relating to object-oriented design issues. I'm quite keen on designing classes and applications properly first, and only later optimizing them if appropriate. Therefore, many sections in this book address design questions. You will find that the better you get as a Java programmer, the more you'll be able to stop focusing on syntax and focus instead on design. You'll find several suggestions here to help you design better classes and applications. My belief is that the syntax is easy. Writing truly
object-oriented code that will be relatively easy to maintain is more challenging, and that's why I've put this material in the book.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark 2002, O'Reilly & Associates, Inc. Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > Introduction > How This Book Is Organized < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021092067008180098046013236073021
All Books
Search
Java > PURE Java 2 > Introduction > Features of This Book < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008180098046003218074094
Guidance and examples to help you write your code as cleanly, elegantly, and simply as possible. Object-oriented design suggestions to help you create better classes. Knowing syntax and writing good, maintainable code are two different things. This book helps you accomplish both. The use of design patterns. I think these strategies for solving design problems can be helpful in organizing a program.
Note The source code for the examples given in this book can be found on the Macmillan Web site. To download the code, go to http://www.mcp.com/product_support/. Enter this book's ISBN0672316544in the Book Information and Downloads text field, and click Search.
UML, the Unified Modeling Language, is used to show class hierarchies, especially in Part III. This notation, developed by the Three AmigosBooch, Rumbuagh, and Jacobsenat Rational has become a de facto standard for creating artifacts for object-oriented analysis and design. I wrote this book with real programmers in mind. I have had to write silly little "Hello, World" programs in more languages than I care to remember. Such programs are not very useful, and certainly no one would pay you to write such an application. Instead, my approach is to write real code that can easily be expanded into a meaningful application. With rare exceptions, I have used the same basic problem domain throughout the
book: a graduate school library (where I spend much of my time working on my doctoral dissertation). The continuity of one problem domain should prove helpful. The examples focus on library cards, books, and book catalog searches, rather than on a myriad of different things, as some books do. I hope you will find this book a valuable resource that you reach for regularly while you work. If you have questions about its contents, you can email me at [email protected], and I'll send you a reply in due time. May you always brew hot, robust Java. Kenneth D. Litwak Milpitas, California September 1999
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark 2002, O'Reilly & Associates, Inc. Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008180098045139175052177
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008180098045137145213106
Java Overview
Java is three things in one:
q
Java is a programming language. It is similar, in various ways, to many other programming languages, with heavy influence from C (basic syntax and operators) and Smalltalk (object-oriented features).
Java is an environment. In the following sections, I will talk about the Java virtual machine (JVM). A key part of Java is a run-time environment that supports Java programs. There are many features, such as garbage collection, multithreading, and resource management, provided by the Java environment. It is this runtime environment, including the Java interpreter, class loader, bytecode verifier, and so forth, that shields your application or Applet code from the operating system. The environment schedules your threads, loads your code, manages memory for you, and makes calls to native OS routines as needed. If the JVM cannot accomplish its task without a native OS call, it will make such a call. Your code won't be aware of this call.
Java is the equivalent of a huge class library. This library is similar to one you might buy for C++. There are classes for dealing with URLs, doing GUI programming, doing distributed object programming with RMI or CORBA, and so forth. Unlike many languages (C, for instance), Java comes with support for doing most of the things you need to do, already coded, from basics such as collection classes to complex distributed objects such as a Java wrapper object for a legacy COBOL program.
Java Is Platform-Neutral This is where the "write once, run anywhere" motto for Java comes in. How does this work? First, you write a Java source file and compile it. When you compile Java source code, the result is a .class file that contains bytecode. The bytecode is the same no matter what platform you are on, provided that you are using a Sun-Compliant JVM. There is essentially nothing in the byte code values that is platform-specific. When you execute the class file using a given JVM that is Sun-compliant, the JVM deals with platform dependencies. Therefore, you can take the same compiled .class file and run it on any system with a Sun-compliant JVM. So if I write a Java program on Solaris, I can run it on a Mac or Windows 98 or OS/2 without having to recompile. There are a few small items that are not platform-neutral, such as the difference between / and \ in directory names, or the number of mouse buttons. You can plan for these differences depending upon what your application does. This platform independence is a very important feature in Java. It means that you can develop on one
system and run on another without doing very much porting of the code. It means that you can have diverse client systems, but only one version of the client code. You can have multiple systems in the middle-tier, but only one version of the Java middle-tier business logic code. Java can reduce desktop maintenance costs by allowing for only one version of the client regardless of the OS on the client. It also provides for scalability on the server-side because you can run the same middle-tier business logic on any number of systems. Java, through the JVM and the use of interpreted bytecode, shields your program from any differences that appear on different platforms. As Chapter 8, "GUI Programming I: Applications," describes, your object's request to create a Java button or some other OS-dependent entity (for example, a thread) will be handled by a routine that is specific to the platform. Your Java code, however, has nothing in it specific to the platform. In cases where something specific to the platform is needed, Java provides various means for dealing with the dependency, such as system properties for file and path separators. Note Bytecode refers to specific opcodes generated by the Javac compiler (or by other means) as the JVM-specific equivalent of your source code. Opcodes represent the instruction set that Java supports at a low level, such as an add two floats together opcode or a create new int array opcode. The opcodes are published. You can write a program using only bytecode. No knowledge of bytecode is needed to program in Java, but it is an important concept to understand. You must know the difference between using the Java interpreter and a JIT (just-in- time compiler) or HotSpot.
Java Is Object-Oriented Like Smalltalk, but unlike C++, Java was designed from the start to be an object- oriented language. So Java supports inheritance, encapsulation, and polymorphism, the three major OO constructs that distinguish a language as object-oriented. Java supports the object model. All true objects have
q
Identity Objects can be referred to by some kind of identifier. In Java, you can use a human-readable name, like okButton. Java internally refers to objects by an object reference.
State Java uses data members (also known as member variables) to define state. Generically, these are an object's attributes. This might include the background color of a Label object or the number of elements in a Vector object(which is part of its state, and not merely a calculated value).
Behavior Object behavior is defined in Java by methods. They follow in large part the Smalltalk model in which a method call is similar to sending a message to an object. A well-designed object-oriented system or program consists of multiple, distinct, cooperating objects, working together to accomplish some task, such as processing an e-commerce order. To help support this concept, Java provides the framework for the three essentials of a proper object-oriented program. These three elements separate true object-oriented languages from procedural languages like C or a quasi-object languages such as Ada.
Encapsulation
Encapsulation represents the notion of attributes and behavior forcing one complete entity, in which the state and behavior are tightly integrated. There is no close connection semantically between a C struct and a C function, which uses fields in the struct. In Java, data members and methods go together into one class. That class defines what an object that has both certain attributes and behaviors is like. This is a powerful idiom that enables a developer to define a class for an object that creates itself, provides a means for other objects to act upon it or make requests of it, and internally takes care of its own state. This will be explained more thoroughly in Chapter 3, "Java Program Features." Note A Java program that reflects proper encapsulation is far superior to a program that merely has data and code in the same source file. In a typical procedural program, any code can do whatever it wants to the data, and there is nothing to semantically create a relationship between the data and the code. Many non-object-oriented programs allow code to put lots of invalid values into data. Nothing is responsible for safeguarding the state of the data.
q q
Inheritance Java supports the capability of one class to derive data members and methods from a pre-existing class by stating that the former class "extends" (inherits) from the latter. The power of this mechanism will be seen throughout Part II, "Technique Reference." For example, all the GUI components inherit a huge number of methods and data members from the parent class, Component, as do user-defined GUI components, which generally subclass (inherit from) Component. Like Smalltalk, but unlike C++, Java only permits single inheritance in order to prevent bugs that can result from improper multiple inheritance. This will be covered more in Chapter 4, "Data Types." Polymorphism Java provides the capability to call a given method defined in multiple classes. It enables the system to figure out which class's method should to be called. This is both a compile-time and runtime process. Let me illustrate this using the well-known Smalltalk example: You create an Animal class and two subclassesDog and Parrot. Each of these three classes has a talk() method. You then create an object of type Dog, and you call its talk() method as follows:
fido.talk(); Because the class Dog inherits a talk() method from Animal but overrides it, Java needs to determine the correct version of the talk() method to execute. It will call the method defined in the Dog class. Because of the superclass/ subclass relationship, you can also use a superclass name and substitute a subclass instance for it. Here's a short example:
This illustrates the idea of substitution; it enables me to call the wash() method and provide a parameter of type Dog. Java knows that a Dog is not an Animal per se, but it also knows that Dog is a subclass of Animal and so allows a Dog object for a parameter. Then, inside the method, the parameter can be tested to determine its actual type. Polymorphism is discussed more in Chapter 4. Java Is Network-Ready Java comes with many classes, especially those in the package java.net, to provide for easily using sockets, server sockets, reading URLs, encoding and decoding URLs, and so forth. Many of these functions can require a lot of code (in C, for example). Java masks a lot of the complexity by providing classes for you that already implement the hard parts, such as creating socket connections, reading the contents of a URL as a file, and translating back and forth between domain names and IP addresses. Java Is Extensible You can easily add classes and plug in new methods to classes, creating new classes through subclasses. This makes Java very easy to augment with your own classes or even to modify. Java 2, as I'll discuss later in the chapter, even supports a feature called extensions that enable you to create user-defined classes. The JVM treats these as core Java 2 classes. Java Is a Better C The Java designers set out to make a language that would be easy for C developers to learn. It would C syntax in general, but would avoid many problems associated with C:
q
Java does not allow you to use pointers. The biggest difficulty in C programming has to do with memory allocationthat is, pointer errors. You run into these errors because C allows you to perform so many tricks with pointers. Java avoids these memory allocation problems by not allowing you to use any pointers. Java does have object references, but you are not allowed to modify the values in them. (An exception is getting a new reference to a new object, reusing the same object reference variable.)
Java does not allow multiple inheritance. The designers of Java deemed multiple inheritance a feature that programmers either didn't understand very well or abused too often. The results were program errors and hard-to-maintain code.
Java primitives are always represented in the same way, regardless of platform. This is another thing that makes Java safer to use than C. This is also one of the features that make it platform-neutralprimitives are always represented the same way, regardless of the OS the JVM is running on. So, for example, an int is always a 32-bit signed number in Java, and bits are always represented in big-endian format. There is never a problem with an int being two bytes on one system and four bytes on another. It doesn't matter what the OS does because an int is always the same on every JVM.
Java doesn't allow you to pass parameters by reference. All parameters are passed by value. That means that you can't do sneaky things with a parameter in a method. Even object references are passed by value, so changing an object reference in a called method doesn't change the real object reference. You can use the value of
the object reference in a called method to change its data members or call the object's methods, but you can't change which object the object reference points to. There's no spoofing the runtime system. Note Java has also been spared templates, which are part of C++. They are a fairly complex construct, and there's a lot to know before you can use templates correctly. It's easy to get them wrong. That is why Java does not provide them.
Java Is Secure Security is a major feature of Java. You don't have to buy separate software to get security for Java, as you would for other programming and scripting languages. Java provides security through
q
Protection domains Java is designed from the ground up to be secure. That means, among other things, that an applet running in a browser can't merrily thrash your client system as an ActiveX control might do. This is because the protection domain of downloaded applet code does not normally have permission to do nasty things to your system. Java provides for fine-grained, policy-based permissions for given codebases or code signers.
Access control lists An alternative facility exists in Java to define users and groups as having specific access privileges.
Support for authentication and privacy Although encryption and decryption are not part of Java 2 as such, Java does support them and provides for authentication through digitally signing code and documents; creating digital certificates; key pairs; andas part of the Java 2 SDKmessage digits for privacy, authentication, and verification of message contents.
Bytecode verification for distrusted code Java 2, unlike previous versions, only truly trusts code that is in the SDK. All distrusted code is checked to be sure the bytecode symbols are valid. In this way, the JVM can determine if a class file contains something that is illegal or meaningless in Java. A hacker cannot put a Trojan horse or virus in the middle of a class file. In the event of foul play, the bytecode verifier will see opcodes that don't match anything valid and refuse to load the class definition. This is done under the control of a classloader that understands protection domains. Java has a number of features built in to prevent distrusted code from doing bad things to your system. When your applet class is downloaded from a Web server, it cannot run in the browser until the bytecode verifier has inspected the class file. The verifier validates the applet's format and verifies that all the checksums are correct, that the class file starts with the magic number cafebabe, and that all the byte codes are valid. This means that you can't take any file, such as a virus program, rename it to a class file, and expect it to be run by the browser's JVM. If a class file does not start with the letters cafebabe, the bytecode verifier will refuse to load the class.
Note A "magic number" is a special character or characters or set of bytes that appears at or near the beginning of a file to tell a program the file's type. For example, there is a magic number used in all Windows .exe files to indicate that they represent an executable program for a Microsoft Windows system. Java's magic number tells the bytecode verifier that the file being examined is supposed to be a class file and is not some virus or Trojan horse merely renamed to have the .class suffix.
These elements are elaborated in Chapter 16, "Java Security." The Java 2 SDK provides a new policy-based security model, described in Chapter 2, "New Features in Java 2." In this model, you can specify at a fine-grained level that a given class can read directory c:\temp but not write to it, and that this same class can have read/write privileges on C:\Accounts\ExpenseReports. Java Is Multithreaded Being able to run multiple threads in the same application is a powerful feature. It's very rare that a computer's CPU is utilized 100% of the time. Multithreading enables your program to do multiple tasks concurrently, and thus get more processing done in less time than it could with just one thread. Java can use multiple threads. In Java, using multithreading is natural because the JVM is already using multiple threads. This contrasts sharply with multithreading in C or C++, which can be quite complex. You might use multithreading, for example, if you have an applet that displays a user interface and, while the user is looking at the screen, runs a MediaTracker object on a separate thread to download images for animation.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
pointer extensions Java identity objects inheritance multiple objects Java C programming language comparing 2nd class library environment extensions JVM (Java virtual machine) multithreading 2nd networks object-oriented language 2nd 3rd platform neutral 2nd programming language security 2nd 3rd Java virtual machine. Javac compiler JVM (Java virtual machine) languages, object-oriented. libraries class Java magic numbers methods talk() wash() multiple inheritance multithreading Java 2nd networks Java numbers magic object-oriented languages Java 2nd 3rd objects behavior characteristics 2nd 3rd 4th encapsulation 2nd identity inheritance polymorphism 2nd state opcodes parameters passing passing parameters platforms Java 2nd
pointer errors polymorphism objects 2nd primitives programming languages. security Java 2nd 3rd state objects structs talk() method templates wash() method
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 1. What Is Java? > The Java Virtual Machine < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021092067008180098045130027153252
It is the JVM that makes it possible for the same class file to run on any platform. The JVM takes your platform-neutral byte code and interprets it to run platform-specific machine code. It can also compile it into native code with a JIT (a just-in-time compiler that compiles and caches your code, usually one method at a time). Thus, it is in the JVM where your code results, if needed, in native OS calls. In the JVM, your platform-neutral threading code gets turned into platform-specific threading code. In the JVM, your Java I/O methods result in platform-dependent I/O calls. This happens for you automatically. Java allocates threads as needed for your application. The JVM manages the memory of your program. So, when I create a JTable object, or an AudioClip, or a plain old float, Java allocates memory for both objects and primitives. Java determines when these items are no longer referenced, and, therefore, can have their memories reclaimed. The JVM, without any prompting from the user, runs the Garbage Collector thread (when possible or required) to reclaim used, unreferenced memory.
All Books
Search
Java > PURE Java 2 > 1. What Is Java? > Garbage Collector < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008180098044214227136226
Garbage Collector
For those with experience in C++, Java's constructors will seem pretty familiar, although there are some differences. You'll look in vain, for example, for a destructor in Java. The JVM not only frees you of the need to call destructors to release memory, you can't do so even if you want to. This is all under the control of the JVM. There is a separate thread in the JVM for garbage collection, similar to that in Smalltalk. There are two important aspects of this garbage collection thread. First, you can request, but not force, garbage collection, by a call to System.gc(). Second, because the garbage collector thread is a low-priority thread, if there are other, higher priority threads hogging the CPU, the garbage collector thread might never get scheduled to run unless or until the JVM completely runs out of memory and calls the garbage collector to free up some memory. The garbage collector performs an asynchronous task. That means that, basically, if it's running, nothing else in the JVM runs. As currently implemented, the garbage collector uses a "mark and sweep" algorithm for reclaiming memory. This is, however, implementation-dependent, and it's wholly possible that someone will implement a JVM that uses a different algorithm for garbage collection. If you really want to use a different approach to garbage collection, you can write your own JVM, a nontrivial task.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 1. What Is Java? > The Java 2 SDK < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008180098043136023221121
Jar Files
Because they will be referred to so many times, let's talk about what a jar file is. If you've used Microsoft Windows, you are probably familiar with zip files, developed by PKWare. Zip files, however, don't work correctly in Solaris. That is, there's no support in Solaris (or most versions of UNIX) for a .zip file. The UNIX tar command cannot work with a .zip file, and tools to unzip a .zip file, such as WinZip or the more basic pkunzip, do not run on UNIX. Jar files are a Java-specific, platform- neutral form of a zip file that can be read or written on any platform that has a Sun-compliant JRE. This means that, because the jar utility can extract the contents of a jar file, the jar utility can also be used to unzip a zip file on Solaris or Windows, even if the zip file has nothing to do with Java. Prior to Java 2, the class files for Java were put in a file called classes.zip. Now they
are divided into multiple jar files, such as rt.jar. Caution It is critically important that you never unzip the 1.0 or 1.1 classes.zip files or unjar the jar files for the Java 2 SDK. If you do, your Java environment will be broken, and you'll have to delete everything and start installation of the Java 2 SDK from scratch.
Jar files are now pretty much the de facto way to package and deploy your code. JavaBeans go in jars. Extensions go in jars. Applets should be put in jars to improve download performance. Enterprise JavaBeans require a .jar file. The jar utility is very easy to use, so there's no reason not to use it. Another important tool is the javadoc utility. You can use javadoc to produce documentation about your classes. The Java API documentation was produced using javadoc. It lists class, methods, and data members. You can use additional javadoc tags to specify author, date, and so forth. There's even a deprecated tag so you can mark code you wrote previously as deprecated. This means that when the code is compiled, you'll get a deprecation warning for your own code. You might do this, for example, if it's 1:00 a.m., and you code a hack just to get the product out the door the next morning. You might mark that hack as deprecated to remind yourself to go back and do it right when you have spare time. There is a minimalist debugging tool called jdb, that I won't cover because it's being replaced and because it's so unintuitive and hard to use. If you need to debug code, you should use an IDE with an integrated debugger. The javap utility can provide information about a class, such as its data members and methods. Versions There have been three main versions of the JDK: 1.0, 1.1 and Java 2. All the version numbers that have a more specific number, like JDK 1.02, represent bug fix versions. The latest 1.0 version is JDK 1.02. The last 1.1 version is JDK 1.1.7. What was called JDK 1.2, however, is now part of what is called the Java 2 SDK Standard Edition Version 1.2, or more simply, Java 2. You might see the latest Java version referred to by the longer official name, as Java 2 or as JDK 1.2 (including most Java software documentation as of August 1999). The difference then between, say, JDK 1.1.4 and 1.1.7 is bug fixes, although there have been some significant changes between versions. JDK 1.02 has about 220 classes. JDK 1.1 has about 500 classes. The Java 2 SDK has over 1600 classes. Even with this book that is meant to help you with the most important or difficult methods in many of these classes, you'll still need to become familiar with the Java 2 SDK API documentation. Because of the differences between versions, you need to be careful about your choice of version. For example, because all the major Web browsers support JDK 1.02, but don't all fully support JDK 1.1, if you want to write an Applet that runs without any special support on all major browsers, as of this writing (August 1999), you need to code it with JDK 1.02 APIs. There are ways to get around this situation, such as using the Java plug-in. Or, you can install your Applet with an appropriate JRE on the user's machine. The latest version of Netscape provides JDK 1.1 support. Internet Explorer 5 supports some flavor of Java, although it is not, legally speaking, Sun-compliant. In fact, you can never expect to see Netscape or Microsoft provide support for Java 2. The only way this will be
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=18 (2 of 4) [6/2/2002 9:43:54 PM]
available, it appears, is with the Java plug-in, available as a separate download from Sun. With this plug-in installed, you will be able to run Java 2. Note Because of important changes in the security model of Java 2, signed applets that worked under JDK 1.1 will not be allowed out of the sandbox unless they also have a policy file that supports the added functions.
Apart from a solution like one of those just described, or unless you dictate the version of the browser that users can run with your Applet, you can't expect to use the Java 2 SDK features, such as Swing, in your applet.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
jdb SDK (Software Development Kit) 2nd unjarring jar files unzipping zip files utilities javadoc versions SDK (Software Development Kit) 2nd zip files unzipping
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008180098042086222052094
All Books
Search
Java > PURE Java 2 > 2. New Features in Java 2 > Swing/Java Foundation Classes < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021092067008180098042092176181214
Swing Pluggable Look and Feel (PLaF) Accessibility Java 2D classes MVC (Model-View-Controller) Drag and Drop
The topics in the list above are explored in the following sections. There will be additional coverage of these topics in Chapter 8, "GUI Programming I: Applications." Swing Swing is a GUI framework that adds significant new functionality to Java GUIs and is intended to be a complete replacement for the AWT. One of the most important innovations in Swing is support for numerous components that are not platform-dependent as the AWT components are. Every AWT component has a native peer. That is, for every Java GUI object you instantiate, a native C function is called to create the corresponding native component on the specific operating system you are using. Such components in Java are called heavyweight. Swing, on the other hand, except for a few heavyweight containers, makes all its components
lightweight. A lightweight component does not have a native peer, but borrows its system resources from its heavyweight container. Because Swing components do not depend upon native peers, they can look however they want to look. This means that Swing offers to developers a whole new set of components that are not available through the AWT. These include a tree control (JTree), a multicolumn list box (JTable), a tabbed Panel (JTabbedPane), slider bars, progress bars, ToolTips, toolbars, and several other components. Pluggable Look and Feel Swing has the capability to make your components look like those of the platform they are running on. So, your JButton will look like a Win32 button on Windows NT, but the JButton will look like a Motif button on Solaris. Or, you can decide that you want your application's user interface to look the same no matter what platform you are using. To accomplish this, you select one look and feel that will always be used. Swing comes with a default look and feel, called Metal. (The Metal look and feel is going to be renamed the Java look and feel at some point in the future.) In addition, you might decide that you want a consistent look and feel across platforms (which I think is a great feature), but you do not wish to use Metal. If that is the case, you can use Swing to create your own look and feel. This is not, however, a task for the faint of heart. After you create your own look and feel, you'll probably use it in all your applications, just as you would use Metal. Note The first JFC class appeared in JDK 1.1. It is the ScrollPane, a really handy component that saves developers from all the nastiness involved in using scrollbars. It is an especially important component because scrollbars are buggy and do not work correctly in the Win32 versions of JDK 1.0 and, to a lesser extent, 1.1.
Accessibility This feature provides APIs to enable a user to run an application without a mouse or even a keyboard. After a developer has added accessibility to an application, a user can hover the mouse over a component and hear, on a speech synthesizer, information like "Click this button to submit your order." Also, when you have added accessibility support to your application, a user can attach a microphone and a speech synthesizer and instead of having to type or use the mouse, he can say "File, Open." Tip It is a federal requirement that if two products offer comparable features, and only one of them has accessibility provided, the government must purchase the product that offers accessibility. There are many European countries with similar regulations. Because Javasoft does most of the work for you, and you can get accessibility into your application with very little work, there's little reason not to add it. Physically challenged individuals, especially, will be able to use your application more easily and appreciate your attention to their needs.
Java 2D Classes The Java 2D APIs are designed to provide for advanced drawing techniques (including support for Affine transforms and other specialized drawing routines), graphics, and imaging. The classes cover text, images, and line art and are part of the java.awt and java.awt.image packages, rather than being in their own package. The APIs include a rendering model which covers composites, fills and strokes, coordinate systems, transforms, and so forth. Although the old Graphics class provides for drawing some basic shapes, like rectangles and ovals, the new Java 2D Graphics APIs provide for drawing any shape. Geometric shapes are now implementations of the Shape interface. There are new fill and pen styles provided through the Paint and Stroke interfaces. The AffineTransform provides for linear transformations of 2D coordinates, such as scale, rotate, and shear. There are now multiple coordinate systems, divided into two groups: user space, which is device-independent; and device space, which is platform-dependent. Implementations of the Composite interface, such as AlphaComposite, provide for color composition. Fonts are defined by a series of Glyphs, which are a series of Shapes. Incidentally, Java 2 provides new facilities for creating and installing fonts for Java to use, and the classes for dealing with fonts have changed significantly. The Java 2D APIs provide many features, but are fairly specialized. The reader will need to consult a book more specific to the 2D APIs to get more details, although the basic APIs are documented in Part II (Techniques Reference) of this book. The Java 2D packages also include a new printing model that provides additional support for higher resolution printing and better manipulation of multipage documents. The new model also supports the concept of a "book." That is, you can ask to have multiple "pageable" (multi-page) objects joined together in the printed output with appropriate page numbering. Caution As of this writing there are still some problems with Java printing, even in the Java 2 SDK version. Because the new printing model seeks to be platform-independent, its current performance is quite slow on some platforms, and it produces very large PostScript files when trying to print very small amounts of text. The Java team is aware of these issues and hopes to improve printing in the near future.
Swing Design Philosophy: MVC Swing components are implemented with the model-view-controller design pattern taken over from Smalltalk. This means that you can change the data model for a component without changing its appearance, and vice versa. For example, if your data model for a JTable contains a Boolean value, true/false or yes/no, you can change the view of this data so that the viewable column in the JTable has a JCheckBox that can be checked for yes and unchecked for no. You don't need to modify the data to change the view to a JCheckBox. Tip The model-view-controller pattern is quite powerful, and I recommend its use highly, especially for n-tier applications with a presentation layer and a data layer. The reader should
consult a design pattern book to learn more, for example, Design Patterns: Elements of Reusable Object-Oriented Software, written by the "Gang of Four," or Mark Grand's Patterns in Java.
In general, every Swing component has two classes associated with it, a UI class that contains the view and controller code and a model class that contains the data model. For more detailed information on Swing, see Pure JFC Swing by Satyaraj Pantham (also published by Sams). Drag and Drop The drag-and-drop facility enables you to define drag sources and drop targets. Currently, this really only works on plain text, but it is slated to support other data types.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
Affine transforms federal requirements for accessibility grahica Affine transforms graphics APIs (Application Programming Interfaces) 2nd 3rd heavyweight components Java IFC (Internet Foundation Classes) Internet Foundation Classes (IFC) Java components heavyweight heavyweight components Java 2D classes new features 2nd 3rd Java Foundation Classes. javax.swing package JFC (Java Foundation Classes) javax.swing package new features 2nd packages javax.swing lightweight components Swing Model-View-Controller. MVC (Model-View-Controller) design pattern 2nd Netscape IFC (Internet Foundation Classes) new features APIs accessibility 2nd graphics 2nd 3rd drag and drop Java 2D classes 2nd 3rd JFC (Java Foundation Classes) 2nd Swing 2nd 3rd 4th MVC (Model-View-Controller) design pattern 2nd PLaF (Pluggable Look and Feel) 2nd packages javax.swing PLaF (Pluggable Look and Feel) 2nd Swing 2nd Pluggable Look and Feel. printing problems ScrollPane class Swing AWT comparing 2nd components
lightweight lightweight components new features 2nd 3rd 4th MVC (Model-View-Controller) design pattern 2nd PLaF (Pluggable Look and Feel) 2nd
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 2. New Features in Java 2 > The Policy-Based Security Model < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021092067008181172232042056177143
The new policy-based security model of Java 2, the Java 2 SDK, provides for much finer-grained security. Now it is possible to create a policy file, using the policytool utility, which specifies in detail the rights of any class, user, or application to access system resources. So, you can specify that class AccountsReceivable has the right to read from the C:\Generalledger directory, but not to write to it. One of the frustrations for applet writers prior to the Java 2 SDK has been that an applet could not directly access a local printer. Your applet had to send data to the Web server, and the Web server had to send email or a custom HTML page back to the client in order for the end user to print it. Now, you can use RuntimePermissions to let an applet print, but you can still restrict it in every other way. These policy files enable you to set much better control over specific classes. They are more restrictive where it's needed, and less restrictive where it's not. This applies to both applications and applets. The SecurityManager class is still present for backwards compatibility, so you can still use it if you wish; but the new policy-based security model will probably be more satisfactory.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 2. New Features in Java 2 > RMI Enhancements < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021092067008181172232036180201040
RMI Enhancements
There have been important changes made to Remote Method Invocation, Java's mechanism for distributed object programming in an all-Java environment. Here are three new features worth talking about:
q
RMI now has support for using IIOP, Internet Inter-ORB Protocol. IIOP is used by CORBA. Prior to the Java 2 SDK, RMI used only JRMP, Java Remote Method Protocol. With the capability to use IIOP, RMI applications can now talk with CORBA applications. As of February 1999, this support for RMI to use CORBA is only in beta form, but I expect it to be finalized soon. This does not mean that RMI is going to be replaced slowly by CORBA using JavaIDL. It does mean that Javasoft is providing the means for interoperability between RMI and CORBA applications.
Note This interoperability will enable an architecture that uses a CORBA client to talk to an Enterprise JavaBean. It helps facilitate using a CORBA base on which to build an EJB server. Lots of new architectural possibilities open up through the use of IIOP with RMI. You no longer have to make an either/or choice between CORBA and RMI in a system architecture. Each can be used where most appropriate.
RMI provides an activation framework. Prior to Java 2, an RMI client could only communicate with an existing re-mote object if that remote object already existed, instantiated by a server-side application. If you created the client first, the client failed to find the remote object and got an exception. Now that RMI provides you with an activation framework, you can create and use a remote object from the client system on-the-fly, before any program on the server has created it. This is a very helpful feature. To do this, you use a new kind of remote object called an Activatable.
RMI enables you to use different kinds of sockets. Prior to Java 2, the only kind of socket you could use for RMI was a standard TCP socket. It is now possible to create a custom socket factory and use different kinds of sockets. For example, you might create your own sockets for encryption or perhaps compression. The code to create a custom socket factory to provide these custom sockets for you is fairly straightforward. The only hard part is defining the socket class itself.
All Books
Search
Java > PURE Java 2 > 2. New Features in Java 2 > JDBC 2.0 Enhancements < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021092067008181172235186052146017
ResultSet rs = aStatementObject.executeQuery("select * from BookList"); (whilte(rs.next() == true) { // Process the columns in the row in here } The ResultSet method next() causes the cursor to advance one row. When you first begin processing the ResultSet, the cursor is sitting above row one, so to get to the first row, row one, you
need to call the next() method. Prior to JDBC 2.0, you could only do one thing with a cursor: move forward through a ResultSet. This sometimes caused difficulties. For example, you might have needed to step through the rows to count them or examine them before actually processing them. In order to process the data, you had to repeat the query because you could not make the cursor back up, nor you could you process a row in the ResultSet unless the cursor was pointed at it. JDBC 2.0 addresses this problem by providing support for scrollable cursors. A scrollable cursor can not only move forward, but it can also move backwards. In fact, there are methods to move forward, backward, go to the first row, go to the last row, go to a specific row, and so forth. This way, you can step through your ResultSet to count the rows, and then go back to the start of the ResultSet and process each row. Prior to Java 2, in order to modify a table within a database, you had to explicitly code a SQL statement and execute it with JDBC. This means essentially speaking or coding in two different languages, alternating between Java and SQL. JDBC 2.0 provides the capability to change a database table by changing the ResultSet, using Java methods. For example, you can insert a row into the ResultSet. This causes a row to be inserted in the underlying database table. You can also change a value in a row in the ResultSet. This will cause that value to be changed in the table in the database. Again, of course, you have to have a JDBC 2.0-compliant driver for this to work. This feature allows most of your database code, after you've gotten a ResultSet, to be all in Java rather than a mix of Java and SQL. Your code stays object oriented this way. Package Versioning Given that source code changes frequently, due to enhancements or bug fixes, it's possible for a situation to arise in which the client code depends upon a particular version of the sever-side code. In another case, an applet that needs several classes from multiple packages might have dependencies upon a particular version of the package. Anyone who has worked with multiple versions of Swing has probably run into this. Swing methods that worked with Swing 0.5 don't work with Swing 0.7. Code that compiled fine with Swing in the Java 2 SDK beta2 doesn't compile with Swing classes in the Java 2 SDK FCS. So how can you manage this problem of needing a specific version of code? Java 2 provides support for package versioning. That is, you are able to provide information as part of your package that tells its version number. Within certain parameters, the version number scheme is up to the developer. The version information is specified in a manifest file in the jar file in which the package is contained. There are a set of versioning APIs available so that a class can interrogate the version of a package and refuse to run if the version number is not acceptable. Reference Objects Prior to Java 2, the only time you might get to interact with the JVM when an object was about to be cleaned up by the garbage collector was during finalization. Java 2 provides a new approach to dealing with objects. These reference objects, as they are called, provide for "weak references," that is, they provide a way to keep a reference to another object that is, otherwise, no longer referenced by any meaningful object. This allows a reference object to do limited processing when the garbage collector determines that an object is eligible to have its memory reclaimed. This approach provides a mechanism for performing tasks relating to an object that was just cleaned up by the garbage collector. Reference objects permit an application to keep a reference to an object even though its memory allocation is reclaimed by the garbage collector. This can speed up processing. Extensions When dealing with packages and calls to methods in classes in various packages, the Java runtime system needs to be able to find the class or package. In general, this is done by specifying the class's or package's location with the CLASSPATH environment variable or with the -classpath command line option.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=24 (2 of 5) [6/2/2002 9:44:32 PM]
Suppose, however, that you have a class or classes that many applications need to use, such as a logging class on a server machine. You now have another choice besides setting the CLASSPATH. You can make the class an extension. By making the class an extension, Java views it as part of the core Java 2 classes, and Java can always find it. There are two ways to do an extension. Before you can use either approach, you have to put the class file(s) into a jar file. The first way to make an extension is to install the class as an extension. To do this, all you have to do is put the jar file in the jre/lib/ext (\jre\lib\ext on Win32 platforms) subdirectory under the directory for the JDK. If you put the jar file there, the classes are automatically treated as core JDK classes, and you don't have to set the CLASSPATH to point to these classes. The Java runtime knows where the JDK classes are. The second way is through a downloaded extension. To get a downloaded extension, you need to put a manifest file in the jar that wants to use the class(es) in the extension. The manifest file needs to refer to the jar file that contains the desired classes. Suppose, for example, you need classes that are in the Logging.jar file in an applet. The applet's jar file would contain a manifest file that includes the Class-Path header, which follows:
Class-Path: Logging.jar In both cases, you do not need to set the location of the classes through the CLASSPATH. The use of extensions is, of course, completely optional, but you may find it useful. It enables you to install classes, especially third-party packages, and not have to worry about customizing the CLASSPATH variable anywhere. Collections Prior to Java 2, the support for collections has been minimal, and developers have frequently created their own or used a third party's collection classes, such as those of the KL Group. Previous versions of the JDK had arrays, vectors, and enumerations. These have been superseded by a whole new set of collection classes in Java 2, a collection framework that provides a unified way to deal with groups of objects. The collection framework is built around six interfaces, including Collection, Set, List, SortedSet, Map, and SortedMap. Collections can be modifiable or unmodifiable, mutable or immutable, and fixed-size or variable-size. Some collection implementations limit the kind of object they can contain and restrict objects in other ways, such as preventing duplicate elements. The Enumeration interface has been replaced with an iterator object that will be more familiar to developers who have used collections in other languages. There are implementations provided for the interfaces. For example, the Set interface is implemented by the HashSet and TreeSet classes. The List interface is implemented by ArrayList and LinkedList. The Map interface is implemented by HashMap and TreeMap. Like many of the Swing classes, the Collection framework provides several abstract classes that contain minimal implementations, such as AbstractSet and AbstractList. One key advantage of these new classes is that you no longer need to use a third party's class library to get Collection functionality, but can get it directly from the JDK core classes. The new collection framework provides the basis for easily defining new collec- tion data types. It expands the previously available set of collections significantly and provides for developing algorithms to work with multiple kinds of collections. Sound Prior to Java 2, playing a sound file in an applet was easy. You could just do a getAudioClip(), or even just use the play() method, pointing to a sound file. The file did, however, have to be of type .au. Occasionally, someone on the Internet would ask why the .wav file he had converted to an .au file did not work in his applet. Playing sound in a Java application, however, has been quite challenging, requiring a developer to use sun. packages rather than Java. packages, which were not guaranteed to
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=24 (3 of 5) [6/2/2002 9:44:32 PM]
stay the same from JDK version to JDK version. In other words, sound in applications was basically unsupported. The Java 2 SDK changes this with a new sound engine. This engine provides support for playing sound in applications because the engine supports additional file types. In Java 2, you can now play .AIFF, .AU, and .WAV files. In addition, there is support for MIDI file formats, such as MIDI TYPE 0, MIDI TYPE 1, and RMF. The new Java Sound APIs support 9-bit and 16-bit mono and stereo sound. Through a new method, both applets and applications can play sound without requiring an AppletContext by calling
public static final AudioClip newAudioClip(URL url) This new feature will assist developers who want to put sound in their applications, confident that it will be supported. Other Enhancements There have also been other enhancements. JavaIDL has been made part of the JDK, instead of being a separate set of APIs. Jar files have been enhanced, both in terms of command line options and in methods to read to and write from jar files. There have been enhancements to object serialization, internationalization, math, networking, reflection, and resources. Support has been added for a new JVM debugging API (in response, no doubt, to the complaints of developers about the almost nonexistent Java debugger that came with previous JDKs). There is also support for a new JVM profiler. Finally, much work has been done to enhance performance. Sun sees this version of Java as the one that is really ready for full enterprise computing deploymentready for prime time.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
extensions 2nd package versioning 2nd reference objects 2nd sound 2nd Object Query Language (OQL) objects reference JDBC 2 2nd ResultSet 2nd OQL (Object Query Language) package versioning JDBC 2 2nd reference objects JDBC 2 2nd ResultSet object 2nd scrollable cursors sound JDBC 2 2nd SQL (Structured Query Language) Structured Query Language (SQL)
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008181172234203153059091
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
All Books
Search
Java > PURE Java 2 > 3. Java Program Features > Basic Layout < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008181172234201035030117
Basic Layout
Java code is placed in a file that must end in .java. There is a required structure for the file's contents, although some of the elements are optional. The list below shows the order of required and optional elements.
q q q q
<Comments may be placed anywhere in the file> <Package statement> <Import statement(s)> One or more class or interface definitions
At a minimum, a source file must contain one class or interface definition, as shown in the following code:
class LibraryCard { } There is an important requirement for the file name. It must match the name of a class or interface defined in the .java file. Note Java, like UNIX, is strictly case sensitive. That means that a Date object called Today is totally different and unrelated to a Date object called today.
Class Definition A class definition is bounded by curly braces ({ }) and has no other punctuation (unlike C++). This class definition contains no methods or attributes. In order for classes outside the current package (a specific directory path) to access the class or interface defined in a .java file, the class or interface is generally defined as public, as shown in the following code:
public LibraryCard { } For any .java file, there may be only one public, non-static, outermost class. The keyword static is explained in Chapter 4, "Data Types," and Chapter 5, "Methods." An outermost class is simply a class that does not contain an inner class. Package Statement A package statement identifies the directory path for a given class. The following shows a typical .java file with a
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=27 (1 of 4) [6/2/2002 9:44:44 PM]
package statement.
package gradschool.library; public class LibraryCard { } A package statement, which ends with a semicolon (;), specifies a relative path to the class file. In this example, the compiler will look in the directory library, which is a subdirectory of gradschool, which is a directory relative to some starting point. This starting point might be either the current working directory or its parent directory, gradschool, which would normally be listed in the CLASSPATH environment variables or specified in a command line switch -classpath. It is not necessary to specify a package. If no package is specified, the class goes into a default package created by the compiler. All classes must be in packages. It is unnecessary for you to know that this package exists for compiling or running the code. Import Statements When compiling a class, Java might need to find other classes that are named (used) in the class definition. The compiler must know where to look for these classes that are named in the import statement. For example, if you have this code
Vector v = new Vector() you need an import statement that points to java.util so that the compiler can find the class definition of Vector. This enables the compiler to validate that this is a real class and that the methods you call are defined for Vector. There are two ways to specify this. The most common way is to use import statements. An import statement specifies a package name and, potentially, a file specifier that tell the compiler where to find the class or interface file. Listing 3.1 shows import statements used in a .java file.
Listing 3.1 A Source File with Import Statements
import java.awt.*;
// Look through all the classes in the // java.awt directory if necessary // to find a class definition.
Import java.sql.*; import javax.servlet.*; import java.awt.event.*; Public class LibraryCard { ResultSet rs; Panel pan1; AwtEvent ae; }
// Each directory level must be explicitly specified. // The "*" refers only to files, not directories.
This class has multiple import statements. When the javac compiler hits the statement
ResultSet rs; the compiler searches through the directories of the packages java.awt.java.sql, javax.servlet, and java.awt.event until it finds the ResultSet.class file or it runs out of directories to search. It is also possible, although less common, to specify the package name as a prefix to a class name. This avoids ambiguity if a class definition contains two classes (from different packages) that have the same name. An example is
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=27 (2 of 4) [6/2/2002 9:44:44 PM]
public class LibraryCard { java.util.GregorianCalendar expirationDate; java.lan.String cardholderName; } Note Because the package java.lang holds essential core classes, such as java.lang.Objectthe parent class of all other classes, the compiler automatically provides an import statement for java.lang.*;.
Note A Java import statement is not at all like a C header include. You can have as many import statements as you wish in a program, and these statements do not increase the size of the .class file. They merely provide a means for Java to find the classes at compile time. In the compiled .class file, all class names are prefixed with their package names. An import statement is only used at compile time. These statements provide a pointer to enable the compiler to find classes. They don't bring any code into your class definition.
//* some comment */ This is a javadoc comment. As in the preceding forms, this comment may be one or more lines long. A javadoc comment is a comment processed and turned into HTML text by the javadoc utility, which is used to generate the Java API doc from javadoc comments in the class source files of the Java 2 SDK.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008181172237104071209170
Names
In addition to the requirement that a class name exactly match the name of the .java file, there are three things to bear in mind about naming classes, interfaces, variables, and methods.
q
Avoid names that are the same as keywords. So, for example, do not use default as the name of anything, because default is a Java keyword. By convention, class and interface names begin with a capital letter. Variables that serve as constants are all capitals with underscores between words. A capital letter starts each word in the name of the class, interface, method, or variable. Everything else begins with a lowercase letter. A variable may start with a letter, an underscore, or a $.
public class LibraryCard // A class name starts with a capital letter { public static final int STUDENT_CARD = 1; // Class Constants public static final int FACULTY_CARD = 2; public static fianl int ALUMNI_CARD = 3; String borrowerName; // Variable starting with a lowercase letter String $borrowerID; // variable starting with a $ float _currentFines; // Variable starting with an underscore public boolean computerCurrentFines() // method name starting with lowercase letter { // Code to compute a fine goes here } } Note You can, of course, ignore the naming conventions (but not the rules). It will, however, make your code harder for others to understand.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 3. Java Program Features > Punctuation < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021092067008181172237108117066224
Punctuation
Punctuation in Java is fairly simple:
q q q
Statements, as shown above, end with a semicolon. Classes, interfaces, and methods use curly braces to enclose their definitions. Java ignores whitespace when compiling.
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
All Books
Search
Java > PURE Java 2 > 3. Java Program Features > Required Methods < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021092067008181172237097019114014
Required Methods
There are three kinds of Java programs: applications, Applets, and Servlets. Servlets are outside the scope of this book. Applets will be considered in Chapter 9, "GUI Programming II: Applets." Here, I will focus only on applications. Every Java applicationbut not classmust have a method that looks like the one in listing 3.3.
Listing 3.3 Source File with the main Method Defined
public class LibraryCard { String borrowerName; float fines; public static void main(String[] args) { System.exit(0); // The proper (but optional) method // to exit a Java application } } The only required method is main. It has several important parts to its signature:
q
publicThis part of its signature grants access to this method from anywhere. This is necessary for the JVM to call the method from outside of the application. static This method is not associated with an instance of a class. If it were, the method could not be called until the class was instantiated, which cannot be done until the main method is entered. The main method must be static so it can be entered before there are any instances of this class. void Every method must have a return type, which must immediately precede the method name. The return type void means that the method returns nothing. Unlike C, this cannot be changed to any other return type. main The mandatory name of the method must be part of its signature. The main method has one parameter args. The parameter name may be whatever you want to use, as long as it is defined as an array of Strings. Unlike C, there is only one parameter. You can use the length attribute to find the number of elements in the array. Also, unlike C, the number of elements in the array does not include the name of the program being run. So, if you type the command line
the result would be an args array that contains two elements, Hot and Java. Tip Although many books put lots of code in the main method, I suggest that you treat the main method, and the class that contains the main method, as driver code. That is, the main method should only tell other classes to do work, not do any work itself.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008181172236154204199025
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008181172236158217065244
Primitives
There are eight kinds of primitives. There are two numeric groups, floating point and integer, plus boolean. These could have been created as classes (as in Smalltalk), but not doing so was an optimization made for better performance. Because primitives are used so often, making them objects would greatly degrade performance. The primitive data types are shown in Table 4.1.
Table 4.1. Java's Primitive Data Types Description One byte long, signed value. Two-byte long, signed value. Four-byte long, signed value. This is Java's main primitive for doing integral arithmetic. Eight-byte long, signed value. Four-byte long, signed floating point value. Eight-byte long, signed floating point value. Two-byte long, Unicode character. Non-numeric value of true or false.
Class Primitives { byte b; int i = 5; float f = 1.41414F; double d = 34.5; char c = 'K'; short s = 6; long l = 334L; boolean aBool = true; public boolean computeSomeStuff() { b = 5; // Set a value for a previously-defined variable. int x = I * 5; // Create a local variable and // give it a computed value // using an instance variable defined above. aBool = false; // Modify the value of an instance variable. return aBool; // return a value from the method // matching the return type } } // End class definition Primitives boolean
Note that the word boolean begins with a lowercase b. The boolean primitive data type has two possible values: true and false. These are also both lowercase words. If statements and while loops require a testable condition that will resolve to a boolean value of true or false. A statement such as
if(x) // WRONG. won't compile. So, if you're accustomed to writing C code that tests for a value, such as 0 equals false and 1 equals true, forget that here. Java doesn't make that translation for you. You must have a statement like this one:
if(x == 7) The compiler can look at this statement and see that its result will be true or false. Caution Be sure not to use an uppercase letter at the start of a primitive type. Doing so will cause Java to think you want a wrapper class of that name. Thus, Float is a wrapper class, not a float primitive. Failure to observe this distinction will cause all sorts of grief for the compiler.
char Java comes with support for internationalization built in. A big part of this support comes through the use of Unicode characters. The char data type is based on sixteen-bit Unicode characters. Unicode enables you to specify all the characters of most character sets for the world's languages. The trick here is to have a font that supports the characters you want to display. If your font doesn't support, for example, \u5567, that character will not display properly. Java always uses Unicode internally. Note Although Java can support the entire Unicode character set, a given computer system may not have a font that can support that character. For example, if your program is designed to display Russian or Hebrew, it is important to verify that a font is installed that supports the appropriate Unicode character ranges for those languages.
Unlike C, Java does not support signed characters. The Java designers felt that signed characters and unsigned numbers were sources for program bugs. Caution Although shorts and chars are the same size, and you could theoretically use a char for a numeric value, this should be avoided. Because a char is unsigned, the only way to get the compiler to accept the assignment of a char to a short is to cast it. Otherwise, the compiler will give an error. Again, the Java designers felt that unsigned numbers were a bad thing, so it is best to use chars only for characters, and not for numbers.
byte The smallest of the integral data types is the byte. Note that this primitive type, like all the primitive types, starts with a lowercase letter. The uppercase version refers to a class, not a primitive. Because all numeric types are signed, a byte is signed. A byte is eight bits long. The numeric range of a byte is -2-727-1. Java's basic numeric integral type is an int. The significance of that causes the following:
//WRONG.
What happens here is that Java converts b1 and b2 to ints before it multiplies them. Because you can't put an int (32 bits long) into a byte(eight bits long) without losing a lot of bits, Java won't do this unless you force it to do so by casting. Casting is a method for telling the system to treat one data type as if it is another data type. This is done in Java by placing the type you want in parentheses before the name of a variable of some other type. Java requires that the cast makes sense semantically. To do the above, you need to code either
byte b1 = 1; byte b2 = 2; int i1 = b1 * b2; If you use the equivalent in Java of a constant, such as
public static final byte b1 =5; the compiler will automatically convert the value to the right size and type for computation. short A short is sixteen bits long. Because it is also a signed number, its numeric range is -2-15215-1. Because a short is signed, and a char is not signed, a char is numerically larger than a short, so you cannot assign a char to a short. You might run into a situation in which unsigned integers, written by a C program, must be read with Java. This is not a problem because Java can read unsigned bytes with an InputStream object into signed ints. int An int is a four-byte numberthirty-two bits. Its numeric value is -2-31231-1. Again, this is the main integral data type, to which Java promotes smaller numbers when it does computations. As I mentioned in Chapter 1, "What Is Java?," Java accomplishes being platform-neutral (in part) by always making an int 32 bits in every JVM, a short always 16 bits, a long always 64 bits, and so forth. This saves Java from the problems that C programmers encounter when porting code between platforms. For example, an int in a C program can be two bytes on one OS and four bytes on another. long A long is sixty-four bits longeight bytes. Its numeric value range is -2-63263-1. This is the largest integral type. Because the default integral type is an int, if you want to indicate that a literal number, such as 456, is a long, you need to append an l or L to it in this way: 456l or 456L. Caution You should be cautious about using a long because most systems only support 32-bit numbers without using emulation. The amount of code required to emulate a long is considerable from a performance point of view. Using
float A float is the first of two floating point primitives. A float is four bytes or 32 bits long. It complies with the IEEE 754 specification for this data type. The default type of a floating point number is a double, like this:
double d = 15.6; // With no other indication, // this is interpreted as a double value Therefore, to set a float primitive to a literal value, you need to tell the compiler it is a float literal, not a double literal, by using f or F, in this way: 34.6f or 5566.2334F. You can code
float f1 = 442.6f; but to initialize a double you would use the following:
double d1 = 445.332; double A double is an eight-byte (or sixty-four bit) number that is also a floating point value. You can also use exponential notation with floats and doubles. A double complies with the IEEE 754 specification for this data type. Because most systems can only do 64-bit emulation, don't use a double when a float would do. Generally, the performance hit for emulation is significant. Caution In compliance with IEEE 754, floating point arithmetic supports additional, special values, such as NaN, which means "not a number." This is the result of floating-point division by zero in Java. In integral arithmetic, Java would throw an exception for a program attempting to divide by zero. This means that, when using floating-point numbers, you must be more careful in checking operands or results.
That's it for primitives. Because all the numeric primitives are signed, the only way to move a value from a large primitive (like a long) to a smaller primitive (like an int) is to use casting. The compiler will not permit you to lose precision unless you tell the compiler, "Hey, I'm a programmer. Trust me."
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008186051228097038206148
Classes
Classes define what an object will be like, its state and behavior. If I create a primitive and initialize it as in
int x = 6; there is a memory location set aside that contains the number 6 as an int. x refers directly to that memory location. If, however, I code
JFrame mainFrame = new Frame(); mainFrame is not the name of the JFrame object, but an object reference to the JFrame object. This has important ramifications. Consider this little class definition:
public class Book { boolean reservedStatus; int checkOutPeriod; String title; } When another class does
Book b = new Book(); memory is allocated for the data members reservedStatus and checkOutPeriod. Furthermore, that memory is given default, initial values, such as false for a boolean and 0 for an int. The data member title, however, being an object reference, is treated differently. No memory is allocated for the String title yet. An object reference named title is reserved. It gets the default initial value of null; I don't mean it gets a String spelled "null"; I mean it has no value. That's like a C pointer with a value of 0. It does not point to any object at all. There's no memory allocated for the title String object. This leads to perhaps the most common Java exception. It occurs when a developer declares an object reference such as title, but does not instantiate the object. Instantiating the object allocates memory and returns an object reference value that is stored in the object reference title. Object references may be pointers, but they don't have to be. On Win32 systems, object references contain handles to the objects rather than pointers. Another (perhaps obvious) difference between objects and primitives is that objects can contain other objects and primitives. Note There might be times, such as when creating Panel objects for a GUI, when it is unimportant to specifically store the object reference. It is perfectly legal to create an object but not store its object reference. The object still exists. It
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=34 (1 of 9) [6/2/2002 9:45:14 PM]
Defining Classes Use the class keyword to define a class, in this way:
class CheckOutDialog There are keywords that you can use with the class keyword. These include an access level like public. It may also include other keywords like abstract. The class definition consists of the class signature, as shown previously, plus matching curly braces:
class CheckOutDialog { } This is the minimal code required to define a class. Of course, this dialog isn't very useful, but it illustrates the point. Unlike C++, Java does not end definitions with a semicolon. In addition, unlike C++, Java does not allow you to define a class's methods outside the class definition. Everything that is part of a class definition must be inside those two curly braces. Remember that a class definition does not create an object. It is simply a blueprint for an object. You still need to create an object using the class definition. Inheritance: Extends The next option is to specify the relationship between your class and another class. There are two main types of relationships between objects: IsA and HasA. An IsA relationship is one in which one class is a subtype, or specialized form of the other class. For example, a Boeing 747 is a special kind of plane. A Boeing 747 is a plane. Note that this relationship is only unidirectional. That is, a Boeing 747 is a plane, but a plane is not necessarily a Boeing 747. A HasA relationship is a relationship in which one object contains or uses another object. For example, a plane has an engine, but a plane is not an engine. So a plane has a HasA relationship with an engine, but a plane does not have an IsA relationship with an engine. In Java, the way to specify an IsA relationship is by using the extends keyword:
class Monograph extends Book { } In Java, every object, regardless of its class, inherits, extends, or has an IsA relationship with the class Object. Object is the top class in the class hierarchy. Everything that is true of the class Object is true of every other class that you can define in Java, whether it's part of the Java 2 SDK or a user-defined class. Everything inherits from or subclasses Object. Because a class inherits the methods and data members of its superclasses, and Object is the superclass of all classes, you get every method that Object has in every other class. Because Object has a toString() method to represent the object as a String, every other class inherits this toString() method. Let's look at another very impor tant example. The Component class hierarchy is a good illustration of the benefits of inheritance. It is shown in Figure 4.1.
Figure 4.1 The Component class hierarchy.
In Figure 4.1, you can see that Component inherits from Object. What's more important is what inherits from Component. A Component has many methods and data members that it shares with all its subclasses. All AWT and JFC components subclass the Component class. Because a Button is a Component, it gets all Component's methods and data members for free, just because it extends Component:
public class Button extends Component The Container class also extends Component, so that everything a Component is, a Container is also. Because a Component can be added to a Container, a Container, as a Component, can be added to another Container. That's how I can add a Panel to a Frame or another Panel. Everything that a Container is, a Window is. A Window object, however, is not merely a rectangular-shaped object that can contain other Components. It is a visible Component that you can display on the screen. A Frame has an IsA relationship with a Window. You hardly ever, however, display a Window because it has no title bar, no maximize or minimize buttons, or anything else. It's just a visual rectangle to which you can add Component objects. As a Window, a Frame has all this and more. A Frame also has a title bar, a title bar icon, and maximize, minimize, and close buttons. You can set the background and foreground color of a Frame on the screen, but the Frame gets these methods from the Component class. It is not a special feature of just Frame objects. Instead, it inherits the methods for setting the foreground and background and a Color property for the foreground and background color from the Component class. From the Container class, the Frame class inherits the add method for adding a Component. From the Window class, Frame gets addWindowListener. The Frame class gets all kinds of methods and data members for free by extend- ing Window. The Window class gets all kinds of methods and data members by extending Container, which gets all kinds of methods and data members by extending Component, which extends Object. Inheritance through extending a superclass is a very important feature of Java.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=34 (3 of 9) [6/2/2002 9:45:14 PM]
Creating Instances of Classes: Constructors The primary way to instantiate an instance of a class is to invoke a constructor for that class. A constructor must be called as part of the process of creating an object. Unlike C++, Java has no matching destructor because garbage collection is the JVM's job. Here's a class with a user-defined constructor:
class Book // Simple Book class definition { String title; public Book(String bookName) { title = bookName; System.out.println("New book added to the collection: " + title); // Print a message to the command line } } // End Book class definition Here's the constructor's signature for this class:
public Book(String bookName) Like a method signature, it has an access modifier, public. It has a method name, Book, which must match the name of the class. It has a parameter list, consisting of one parameter, an object of type String called bookName. There's one really important thing missing, however, compared to a normal method signature. There is no return type for the constructor. In fact, if you code a constructor with a return type
public Book Book(String bookName) based on the theory that a Book constructor returns a Book object, or
public void Book(String bookName) based on the theory that a constructor returns nothing, Java will look at your method signature and say, "This is a normal method, not a constructor." Don't give a constructor a return type. Otherwise, all the code you wrote to build an object in a constructor will be ignored when you try to call your constructor. If you call a constructor that has a return type, but also has a parameter list instead of empty parentheses, (), you'll get a compile error. Java won't be able to find a constructor with that parameter list.
Constructor Signature
You can code a constructor that takes one or more arguments, or no arguments at all. It's generally a good idea to code both. You also have to decide about the appropriate access modifier. If, as in most JDK classes, you want to be able to instantiate the class from anywhere, you should give the constructor a public access modifier. If you want to be able to make an instance in any other class in the package, but not outside the package, use package default access. If you want to be able to create an instance from the package or from subclasses in other packages, use protected. Finally, you might want to make it impossible for any other class to instantiate your class directly. In that case, you should give it a private constructor. In order to call this constructor, you'll have to come up with a static method in the class that returns a reference to the object and optionally executes the private constructor. Some classes, such as JDBC drivers, call their own private constructor from a static initializer, and a
devel oper cannot get an instance of the class directly at all. In part, that's how these classes get to be singletons. Because singletons must call their own constructors, they can check to see whether there is already an instance and reject any request to make another one.
Default Constructors
Generally, it is not necessary to code a constructor for your class. When you need a constructor, the compiler creates a constructor for you if you don't supply one. The constructor that the compiler creates for you is called the default constructor. Although the compiler supplies a constructor for you, the one it supplies doesn't do much. If I had not coded a constructor for the Book class, the compiler would have made one like this:
public Book() { } There is no code in this constructor. It doesn't initialize anything. It will, however, call its parent's constructor by trying to call the parent's zero-argument constructor. In addition, during construction of the object, the JVM will perform default initialization, such as setting object references to null. So if Book subclasses Object directly, the default constructor in Book is the following:
class Book { String title public Book() { Object(); // It calls Object's zero-argument constructor. This is // not the actual constructor name, but it conveys // the correct idea. } } Every constructor has to call its parent's constructor, explicitly or implicitly, or a constructor of the same class. Because Java does this, it is a good idea to provide your own zero-argument constructor. The compiler will insert a call for that form of the constructor into any other constructor that you code, if it doesn't explicitly call its parent's constructor. It does this because of how the JVM creates objects (which is discussed below). Here are points to remember:
q q q q q
Java uses constructors in the process of creating an object instance. Every constructor (except Object's) explicitly or implicitly calls its parent class's constructor. The implicit constructor call will be to a zero-argument constructor. If you do not code a constructor for your class, Java will create a zero-argument, empty constructor for you. If you provide a constructor that takes at least one parameter, Java will not create a zero-argument constructor for you.
In general, it is a good idea to code a zero-argument constructor. By doing so, you make your class much easier to use when it is subclassed. If you provide a zero-argument constructor, or provide no constructor at all, a subclass of your class will compile perfectly when Java inserts a call to the zero-argument form of the constructor for your class. Most JDK classes provide a zero-argument constructor. Note Although most JDK classes provide a zero-argument constructor, one notable exception is the Dialog class. This
class does not have a zero-argument constructor. It does, however, have multiple constructors that take one or more parameters. Therefore, when you try to subclass Dialog, you must explicitly call a Dialog constructor with one or more parameters. This can be a bit inconvenient until you get used to this requirement.
This is, however, only a convenience item for other developers who use your class. If your class is never subclassed, it doesn't matter whether you have a zero-argument constructor or not.
Overloading Constructors
class Book { String title; String author; public Book(String aTitle, String anAuthor) { title = aTitle; author = anAuthor; } public Book() { this("No subject", "nobody"); // Call the constructor for the Book class // that accepts two String parameters // and supply default values, since the // caller didn't provide any values } public Book(String anAuthor) { this("No subject", anAuthor); // Call the Book constructor that takes // two parameters, supplying one // default value and one supplied by // the caller } I'll talk more about this later, but for now, suffice it to say that coding
this(<zero or more parameters>); calls the constructor for the class this, which is coded so that it has a matching parameter list. So, you might code a constructor that takes no arguments, and one or more constructors that take one or more arguments. The only real requirement when coding two or more constructors is that they cannot have the same parameter list.
Invoking Parent Constructors
this(<zero or more parameters>) calls a constructor from the same class as this, so you can call the constructor of the class that your class
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=34 (6 of 9) [6/2/2002 9:45:14 PM]
super(<zero or more arguments>) You call this for your own class, and super for its parent's class. In case you are wondering, there is no way to skip classes and call the constructor two classes up. You must call the constructor of the immediate parent class. Let's see an example using the Dialog class, which has no zero-argument constructor:
public class BookCheckOutDialog extends Dialog { public BookCheckOutDialog(Frame a Frame, String title, boolean mode) { super(aFrame, title, mode); } } The class definition says that BookCheckOutDialog extends, or subclasses, Dialog. That means that Dialog is the immediate superclass of BookCheckOutDialog. So the call to super(aFrame, title, mode) in the BookCheckOutDialog constructor calls the constructor of the parent class Dialog. It is possible to explicitly call a zero-argument constructor of the parent class as well. For example, I could code
public class Journal extends Periodical { public Journal() { super(); // Call the Periodical class zero-argument constructor // explicitly. Catalog.addJournalVolume(); // A static method in the // Catalog class to // add a journal to the library catalog // system } } If you don't explicitly call this constructor, the compiler will insert such a call for you. It is a requirement that if you code an explicit call to the superclass's constructor, this call to super() must be the very first line in your constructor. It may not go anywhere else.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
toString() Object class objects Frame HasA relationship IsA relationship Window overloading constructors 2nd parent constructors invoking 2nd signatures constructors 2nd superclasses toString() method Window object
2002, O'Reilly & Associates, Inc.
Safari | PURE Java 2 -> How Objects Are Created by the JVM
All Books
Search
Java > PURE Java 2 > 4. Data Types > How Objects Are Created by the JVM < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021092067008186051229104046230166
Safari | PURE Java 2 -> How Objects Are Created by the JVM
After all these constructors get called, Java performs default initialization of data members for your class. Next, Java performs explicit initialization and then executes the rest of your constructor. Finally, if appropriate, Java returns an object reference value to your program, in this way:
Frame f = new Frame(); This constructor called on the Frame class causes the previously mentioned steps to be executed, culminating in an object reference to a Frame object being assigned to the object reference variable f.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 4. Data Types > Initialization of Data Members < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021092067008186051229110148219015
public class LibraryCard { String patronID; float outstandingFine; int booksCheckedOut = 0; } This class has three data members. The first two, patronID and outstandingFine, have no values assigned to them. The third, booksCheckedOut, is set to zero. When an instance of the LibraryCard class is created, the value of booksCheckedOut starts off as zero. The other two data members are assigned values by the system. The value a data member gets depends on the data type of data member. That is, all data members are initialized to default values if they are not explicitly initialized. Here's a list of what you get for a given data type: float is set to 0.0f double is set to 0.0 long is set to 0L int is set to 0 short is set to 0 byte is set to 0 boolean is set to false char is set to '\u0000' All object references are set to null Because all object references are set to null by default, it is critical that you instantiate the related objects before using them, or your program will fail. The good news here is that if this happens, Java will always throw a NullPointerException at a specific location, instead of simply attempting to use a null pointer. C or C++ would make the attempt, with the resultant unpredictable results. You can also use explicit initialization. Explicit initialization sets data members to the values
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=36 (1 of 2) [6/2/2002 9:45:25 PM]
you specify. So, if you set a String to "Litwak", this is the value it will be given when the object is instantiated. There is, however, an issue of where to put the code for instantiating and explicitly initializing data members. Note Because there was a JDK 1.0 feature that potentially required it, I learned to declare data members outside of any method. However, I did instantiate them and give them a value inside the class's constructor, unless it was a primitive. In that case, I felt comfortable initializing outside of the constructor. This issue does exist in Java 2.
In some cases, you can only instantiate and initialize an object inside a method. Although objects of many classes can be instantiated outside any method, I suggest that you don't mix the two approaches. Because some things really need to be initialized inside a constructor or method, I suggest that you declare data members outside of any method or constructor (the minimum requirement for them to be data members and not local variables) and then instantiate them. Set their value in a constructor or method appropriately. Caution It is very important to note that if you declare a variable inside a method, which makes it a local variable, Java will do nothing to initialize it, not even give it a null value for an object. You must initialize local variables to some value before using them, even if that value is zero.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008186051230250147168084
Abstract Classes
There are two kinds of classes in Java: concrete classes and abstract classes. A concrete class is a class that you can use to make an instance. An abstract class, on the other hand, cannot be instantiated. Syntactically, an abstract class is any class that contains at least one abstract method. Consider a class Airplane:
public class Airplane { int numberofWings; String engineType; public boolean startEngine() { // logic to start an engine goes here } } This class specifies a generic airplane. Although it is syntactically correct, there are a couple of potential problems with this class definition that give rise to the need for abstract classes. First, there's no such thing as a generic airplane. There are Boeing 747s, there are F15s, there are Gypsy Moth biplanes, but there is no such thing as a generic airplane. Instead, airplane represents an abstract concept. Because it is only a concept and there is no such thing as a generic airplane, it would be inappropriate to instantiate it. The second problem is that various airplanes do the same thing, but in very different ways. So, a jet airliner starts its engines in a different manner from a Sopwith Camel triplane. A jet fighter starts its engines in a different manner from a Cessna propeller-driven plane. That means that there's no code that can be put into the startEngine() method that will cover every kind of plane that might be subclassed from Airplane. Still, every kind of motor-driven plane should have a method for starting its engine(s). To define a base class Airplane that deals with these two problems, two changes are necessary, both using the keyword abstract:
// Declare two data members, // one for the number of // wings the plane has String engineType; // and one for the number of // engines the plane has // These should be properties common to // all subclasses. So you wouldn't necessarily want // to specify the number of wheels as sea planes // may not have any wheels public abstract boolean startEngine(); } // End class definition for Airplane
First, I've declared that the class is abstract. This means it is impossible to make an instance of the class
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=37 (1 of 3) [6/2/2002 9:45:29 PM]
Airplane. Second, I've turned startEngine() into an abstract method. Notice that besides the keyword abstract, the method ends with a semicolon and has no curly braces at all. All the methods in the book up until now have been concrete methods. They all were executable. An abstract method cannot be executed. In addition to the abstract keyword and semicolon, this method could have parameters. Like a C++ virtual function, this abstract method is meant to be overridden in a subclass. It has no code intentionally. Technically speaking, an abstract class is any class that contains at least one abstract method. So adding an abstract method to a class makes that class abstract, and it cannot be instantiated. By itself, this method is not very useful. Its purpose can only be understood in the context of its use in an abstract base class with concrete subclasses. It's very important to understand that an abstract class can have many concrete methods, with lots of code in them; but if there is at least one abstract method, the class is an abstract class. A good example of this kind of class is the java.awt.Component class. It contains several concrete methods and data members, but you cannot instantiate a Component. You cannot instantiate any class that is abstract. You can only instantiate subclasses of Component, like Button or JFrame. Okay, so every type of plane should have a startEngine() method. If the class subclasses Airplane, I am guaranteed that all concrete subclasses of Airplane will have a startEngine() method. I can enforce this through the use of an abstract method, without having to inspect any code. If I create an abstract class, every class that subclasses my abstract class, like F15 or SopwithCamel, must implement all the abstract methods in my abstract base class, Airplane, or the subclass itself is considered an abstract class by the compiler. Note You will get compiler errors if you don't implement all these methods, but don't mark your class as abstract. The compiler will complain that you have an abstract class, but didn't label it as abstract. You must implement the methods in the abstract base class so that your subclass is a concrete class.
So, I create an F15 class and a Boeing747 class that both subclass Airplane, and I know that Airplane has an abstract startEngine() method, I can be confident that both subclasses have each implemented a startEngine() method with code that makes sense for the specific class. So I might have this:
class F15 extends Airplane // The concrete class inherits from the abstract class { public boolean startEngine() // Remove the abstract keyword in the subclass' // overridden version of the method. Everything // else in the method signature must match the // abstract version exactly { // Start the F15 jet engine and crank up the afterburner return true; } } // End F15 class definition class Boeing747 extends Airplane // Another concrete // class that inherits from an // abstract class { public boolean startEngine() { // Start the jet liner's multiple engines | } // End Boeing747 class definition
You want to have a specific startEngine() method for each kind of aircraft. There's a specific design reason for using an abstract base class. The main reason for using an abstract base class is that you have a generic data type, such as Airplane, or Account, or Person, or Product, that you don't logically want an instance of. Instead, you want to have lots of subclasses of the base class, and you want to make sure that every child class has an implementation of a specific method. That implementation must be logically unique to the child class. Therefore, you make the base class abstract and make the method that you want every subclass to implement abstract as well. Now the compiler will force every subclass to implement the method. In case you're thinking that plain inheritance of a method would be sufficient, it's not. If I declare a concrete method that every subclass inherits, the subclasses are not required to explicitly implement it. Therefore, subclasses are inheriting only a generic implementation. Jet liners, jet fighters, propeller-driven biplanes, and rocket planes (like the X-15) don't all start the same way. Each class, therefore, needs its own version of startEngine(), and an abstract method is exactly the tool to accomplish this. When you have a group of related classes, and you want to require them all to implement a given behavior, consider making them subclasses of an abstract base class that has an abstract version of that method. This design approach is to be contrasted with using an interface, which I'll discuss later. Also, let me note that abstract classes sometimes have a create sort of method and serve as object factories. That is, you don't want to make an instance of the abstract class, but you are using a concrete method in the abstract class to produce some other type of object. You'll need to consult a book on design patterns for further information.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008186051230254100087072
Final Classes
Making a data member or method final means that the data member or method cannot be overridden in a subclass or modified in any way at runtime. The same holds true for classes. A class that is declared as final cannot be subclassed or overridden. This again serves as a security feature in Java. No one can modify my CreditCard or Login class. The class String was made final because of several nefarious hacker tricks that could be used if Strings were not final. It is more common to make data members final than it is to make classes final, but you can find uses for this in class declarations.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 4. Data Types > Some Important Common Classes < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008186051230242105184022
You'll explore these classes in the following sections. String Just about the most common class used in Java programs, besides Object (which is always the base class of every other class, so it's used implicitly), is the String class. Unlike C, a String in Java is not merely a contiguous set of characters in memory, followed by a null terminator, \0. Instead, Java Strings are objects, with properties and behaviors. The JVM has a special place in which Strings commonly reside, called the String literal pool. The way to create a String that is placed in the String literal pool is like this:
String author = "Josephus"; // Note the use of double quotes. // This is how String // literals are delimited in Java This line of code, when it is executed, causes a String literal, "Josephus", to be placed in the String literal pool. The other way to create a String is to use an explicit constructor:
String author = new String("Josephus"); This statement places a String object in memory on the heap, rather than merely putting the String literal "Josephus" in the String literal pool. Although it does not make much difference which of these two approaches you take, remember which one you used. It affects how you compare two Strings to each other. Here are two ways to compare two Strings to each other:
public class Book { String author; String title; public Book(String anAuthor, String aTitle) {
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=39 (1 of 12) [6/2/2002 9:45:42 PM]
author = anAuthor; title = aTitle; | public compareBooks(String title1, String title2) { if(title1.equals(title2) { // Use the equals() method to compare these two Strings System.out.println("Book 1 equals() Book 2"); } if(title1 == title2) { // Use the == operator to compare these two Strings System.out.println("Book 1 == Book 2"); } } // End Book class definition As a rule, the method equals() compares the contents of two objects, whereas generally the == operator compares object references only. So, if you create two Button objects
Button b1= new Button("OK"); Button b2 = new Button("OK"); and then try to compare them with the == operator
if(b1 == b2) { System.out.println("You can't ever get here"); } the comparison will fail. You are comparing object references, not the contents of the objects. In the case of Strings, however, the rules are different. If you use the code in the Book class shown earlier, you are creating two Strings:
String author = "the value of anAuthor"; This creates a String in the String literal pool. If you create a second String with the same value, such as
String author1 = "Josephus"; String author2 = "Josephus"; Java will put the first String literal, "Josephus", in the String literal pool and puta reference to it in the author1 object reference variable. When you try to create author2, the system will check the String literal pool, determine that the String literal "Josephus" already exists, and simply return a reference to the existing String literal. At the end of these two statements, you have
q q q
One copy of the String literal "Josephus" in the String literal pool The Object reference in author1 pointing to this literal String in the pool The object reference in author2 pointing to the same physical String literal in the pool
The upshot of this is that, whether you compare the object references with the == operator, which compares object references, or with the equals() method, which compares the contents of each object, both tests will return true. The case is quite different if one or both of the Strings was created as a separate object, as in this example:
String author1 = "Josephus"; String author2 = new String("Josephus"); If you run this code, author2 does not have a literal value put in the String literal pool. Instead, the String "Josephus" is part of a separate String object. If you do it this way, or define both with the String constructor, signaled by the use of the word new, here's what happens when you compare them:
if(author1 == author2) { System.out.println("Won't happen. The object references will" + " never be equal"); } if(author1.equals(author2)) { System.out.println("This should return true, " + "since the contents of each" + " String are equal, even if the object " + " references are different.)" } So remember that to compare two Strings and be sure that you are always testing for equal content, use the equals() method. There is one other point about Strings that is worth mentioning. Strings are immutable, first-class objects. That means that when you create a String in memory, that String cannot be changed there. You can modify a String with the concatenation operator, +, like this:
String s = "Dead Sea"; s = s + "Scrolls"; This does not, however, change the original String. Instead, Java essentially acquires a brand new piece of memory that is big enough for the whole new String, copies the old one into this new one, and adds the new piece, "Scrolls", to it. The old version of the String is then available for garbage collection. If your code has to do a lot of String manipulation, you can potentially use up lots of memory. You might consider using StringBuffer instead of String. Although it's a little more work to use StringBuffer, the StringBuffer class does enable you to append characters to the original memory area, rather than copying the old version to a new, larger location. This can result in a significant performance gain in some text manipulation-intensive programs such as word processing. Date In JDK 1.0, a Date object had to serve two roles: a moment in time and a date such as a calendar date. In JDK 1.1, these roles were split into two groups. Now, in Java 2, the Date class, as in JDK 1.1, represents a moment in time. The former methods in the Date class, which treated it as a calendar-type date, have been deprecated. You are encouraged to use the Calendar class and its subclasses instead. Note Because you should be using Calendar and its subclasses instead of using Date, I won't show any examples of using Date on its own. It is used with the Calendar classes and classes in java.text. It is important, but it does not stand on its own.
In JDK 1.1, JavaSoft introduced the Calendar class, built by Taligent. The Calendar class and its subclasses represent calendar-type dates. You should use these if you want to deal with dates in your program. In JDK 1.1, there were some suboptimal features in the Calendar class hierarchy. Java 2 improves on these to make the Calendar classes more useful. There is, however, still a use for the Date class. I suggest using the Date class if you need to measure how long it takes for something to happen. You can create a Date object before performing some activity, do the work, and then create a second Date object. If you subtract the value of the first Date object in milliseconds from the value of the second Date object in milliseconds, you'll find out approximately how long it took to perform the work. Object Because the class Object is at the top of the class hierarchy for all classes, you should know what's in it. The Object class provides several methods that every other class inherits. Of special note is the clone method. If you can live with a shallow copy, you can use the clone method to actually copy an object of any kind. If not, you need to implement your own deep copy in your subclass. Object has the wait() and notify() methods that are used for thread synchronization. Because Object is the superclass for all other classes, you can use an instance of Object anywhere you want something else. That is, you can make generic methods, such as those used by the Vector class. Here's a sample method:
public String testObject(Object someObj) { String className = null; if (someObj instanceof Button) className = "Button"; if (someObj instanceof Window) className = "Window"; return className; } This method accepts an object of type Object. Using the Vector class, you can pass any reference type at all to this method. Internally, you can test the type of object or operate on it in other ways. This means that you only need one method, not a method for every single object type. Apart from this kind of code, it is unusual to use the Object class directly. It is much more common to use methods inherited from it, such as wait(). Class The class Class has several handy methods that you can use for getting information about an object and for loading a class. You can use
Class.forName(*class name"); to load a class, which is generally how you load a JDBC driver for database programming in Java. If you want to print the name of the class of an object, you can code
public boolean displayClassName(Object someObject) { System.out.println(someObject.getClass().getName()); } To get some basic information about a class, you can use methods in the Class class. To get more detailed information, you'll need to use reflection, covered in the Technical Reference section.
System The System class has several important and handy methods in it. You use System.in, System.out, and System.err to address standard input, standard output, and standard error, respectively. The System class has methods to set a SecurityManager object, which is more relevant to JDK 1.0 or 1.1 than Java 2, but is still available for backwards compatibility. The System class also has methods for getting one or more System properties, such as the root directory and user name. The System class also has a method for copying one array to another array, arraycopy(). The System class is one of the classes in the JDK that is a singleton. That is, you can't instantiate it yourself. There's no way to call a System constructor. It's taken care of for you and you get exactly one in your JVM. Here are some examples of methods you might call with an object of type System (of which there is never more than one):
public boolean snoopSystem() { Properties p = System.getProperties(); long time = System.currentTimeMillis(); System.setSecuritymanager(new CommerceSecurityManager()); // Install a new custom-made // SecurityManager class. return true; } Runtime Like the System class, the Runtime class only has one instance at one time. In an earlier JDK, it was possible to instantiate additional Runtime objects. These additional objects were of no help to you, however, because only the current Runtime object, created for you by the JVM, had links to the objects you might need. The Runtime class has a method for executing other programs, exec(), and methods to get the amount of free and total memory in the JVM. This class can do some significant tasks, so be sure you understand the methods and their implications before you use them. Here are a few things you can do with the Runtime class:
public boolean checkStuff() { long freeMem = System.getRuntime().freeMemoery(); // Get the amount of free memory // Note the use of the System // class to access the Runtime // object getRuntime() is also a // method of the class Runtime System.getRuntime().gc(); // Please take out // the memory garbage } Note If you want a Java program to run Acrobat or Notepad, use exec(). Be aware, however, that this usually makes your code platform-specific because you'll generally be executing a program with a platform-dependent path. For those on a Win32 system who want to execute an MS DOS program, don't try to code
Don't do it.
This is wrong because copy is not a program. It's part of the command shell. Instead, ask for Command.com to execute, passing it the copy command as a parameter to Command.com.
Vector Java has basically two kinds of objects for dealing with arrays of things: array and Vector. At least, this was true before the Java 2 Collection classes. What's important to note here is that arrays can contain only one kind of data type (or subtypes of that type), no matter how many elements there are, and arrays are fixed in size. Arrays can't grow. Vectors, on the other hand, can grow dynamically and can accept objects of varying types. Notice that I said that Vectors can accept varying kinds of objects. That means that a Vector cannot accept a primitive. All the methods for the Vector class accept parameters of type Object (and thus any subclass of Object, which is every other class). If you need to put primitives into a Vector, put them in a wrapper class like Boolean or Integer. Here's an example of using a Vector that would be suitable for e-commerce tier 1 to tier 2 communication:
private boolean sendParms() { Vector dataVector = new Vector(10,2); // For performance reasons, // always use the two-argument // constructor dataVector.addElement(lastNameString); dataVector.addElement(firstNameString); dataVBector.addElement(qyIntegerWrapper); dataVector.addElement(todayGregCalendar); tier1ObjOutStr.writeObject(dataVector); // Write all the data values // out as one big object // by using a Vector return true; } You might also use a Vector for getting a list of components from a Container or saving the values of a column from a JDBC ResultSet (covered in Chapter 17, "JDBC: Java Database Programming" ). Properties The Properties class is a subclass of HashTable. A Properties object is a combination of name and value pairs. There are System properties, like root directory and username, mentioned above, as well as properties such as Java vendor URL, Java version, and several other values. You can also create your own Properties objects. You should consider doing this to make configuration files, similar to those you might find in an .ini file or the Windows registry. Unlike the Win32 registry, however, a Java Properties object stored in a file is platform-independent. This makes it a much better choice for storing configuration information. Here's a simple example of getting a System property:
public String buildConfigFilePath() { String sep = System.getProperty(file.separator); String path = "." + setp + "GreatClasses" + sep + "SuperGUI.class"; // Dynamically create a file path // after getting the system// dependent file separator.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=39 (6 of 12) [6/2/2002 9:45:42 PM]
// This code is fully portable. return path; } Calendar I mentioned this class above. It is used for dealing with dates. The Calendar class itself is not what you want to instantiate. You want to use a subclass of Calendar, such as GregorianCalendar. If you need some other kind of calendar, such as a Jewish calendar, you should subclass Calendar to achieve this. The JDK only supplies the GregorianCalendar subclass. I mentioned above that there are some suboptimal features in the Calendar classes, and one of those protected methods in GregorianCalendar still exists. Hopefully you won't need them. The Calendar class will let you set a day, month, year, and time, including time zone, and adjust that time for daylight savings time. You can inquire of this Calendar object all these values plus the day of the week, week of the month, month of the year, and so forth. Here's a code snippet for making and using a GregorianCalendar:
public Calendar createDate() { GregorianCalendar today // // // // // Date now = new Date(); today.setTime(now); // System.out.println("The // return today; } Thread
= new GregorianCalendar(adjTimeZone); Make a calendar object, It is set for a specific time zone and optionally adjust for daylight savings time. Set the actual date month is: " + today.get(Calendar.MONTH)); Print the month
As mentioned earlier in the book, Java supports and uses multithreading. You can create your own Thread objects. Threads execute a run() method. Any logic you want them to perform goes in the run() method. There are, basically, two ways to do this. One is to create a Thread subclass that has its own run() method. The other is to create a class that has a Thread object as a data member and a run() method in the class for the Thread to run. In the latter case, your class needs to implement the Runnable interface. The Runnable interface has one method, run. So your class needs to implement the run method.
class ImageGetter implements Runnable { // This is not sufficient to actually meaningfully use this Thread Thread getterThread; MediaTracker mt; public boolean getImages() { getterThread = new Thread(this); // Create a Thread object and pass // in a Runnable object, in this case the // current ImageGetter object. getterThread.start(); // Tells the Thread to start running } public void run()
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=39 (7 of 12) [6/2/2002 9:45:42 PM]
Runnable r = new ImageGetter(); r.getImages(); You need a Runnable object for a Thread. I haven't shown you code here for making a Thread subclass. There's a reason for that. From a design point of view, I would argue that you almost never want to create Thread subclasses. Here's why: A Thread represents a unit of execution, or subprocess. The odds of your having a class that logically has an IsA relationship with an execution context is pretty slim. You should not, from a design point of view, define classes that extend other classes, but are not really specialized forms of that base class. You will almost never have a class in a design that is a specialized form of an execution context. Using the Runnable interface in a class instead enables you to have proper design in which you subclass a meaningful class and simply define your subclass to contain a Thread. Threads will be discussed in Chapter 11, "Multithreaded Programming in Java." Wrapper Classes for Primitives As mentioned above, you can't put a primitive into a Vector, and there might be other times that you want to pass a primitive as an object. You can do this in a number of ways, one of which is to put the primitive into a wrapper class instance. There is a wrapper class for each primitive (see Table 4.2).
Table 4.2. Mapping Between Primitives and Corresponding Wrapper Classes Primitive Wrapper Class Float Integer Boolean Short Byte Character Long Double
You can use one of these in an insertElement call for a Vector object. These classes can come in handy, but remember that a Boolean is a totally different thing from a boolean; the two terms are not interchangeable in your code. You can't use an Integer wrapper class object where you need an int, or vice-versa. Trying to do so can lead to compiler type-mismatch errors. Arrays of Objects Because arrays are not classes as such, but are objects for which there is not a specific API section, I'll treat them here. An array is a multi-element object that can hold 0 to n instances of an object or any number of a given primitive. Note the implication here. You can only put one kind of thing in a given array. Unlike C, Java arrays are not merely contiguous memory areas. Instead, an array is an object, and implicitly extends Object. Although it is possible to define a class for an array, each array has an associated class automatically defined by the JVM, which can be accessed by the getClass() method of the class Object. First, let's look at how to declare an array:
String stringArray[];
public ArrayMaker() { intArray = new int[6]; // Create an array that will accept six // int elements stringArray = { "Plato, Aristotle", "Tacitus", "Jerome", "Augustine"} ; // Explicitly initialize an array with // five String elements } public boolean initializeArrays() { intArray[0] = 5; intArray[1] = 3; intArray [5] = 7; System.out.println(stringArray[stringArray.length - 1]); } } This small class is meant to illustrate several things at once. First there are two basic ways to declare an array. To declare an array, you use one or more pairs of [] in the declaration. Java does this by beginning with the data type, followed by [], followed by the object reference to the array object. It is also possible to compromise and code it the C way, in which the [] follow the object reference name. I recommend the Java way because I think it's easier to read. You can read it straight across: Type is int; it's an array; its name is intArray. Using the second approach, you have to read it in this way: data type is String, the object reference is stringArray, and it is an array. Wait, I have to go back and see what kind of array it is. Oh, it's a String array. I think the Java way is more intuitive. It is a matter of style primarily, but most Java programmers declare arrays the Java way. I instantiate the arrays in the class's constructor. In the first case, I state that the array of type int has six elements, but I don't immediately give them values. After this statement is executed, there will be an array in memory for holding six ints. Because I didn't initialize these elements, Java will give them a default value. Because they are ints, the default value will be zero. It is also possible to instantiate and initialize an array at the same time, whether it contains primitives or objects, as I have done in the case of the stringArray object. You could even code
Panel[] panelArray = { new Panel(), new Panel(), new Panel()} ; That is, you can instantiate any object within an array definition. When you use this style for initializing an array, Java counts how many elements you put inside the curly braces. That is how long the array will be. Until you instantiate an array, it has no length. After you instantiate it, it has a fixed length. You cannot add elements. Next, you can see in the initialize() method that I assign values to some of the elements of the intArray object. Note that the first element I give a value to is element 0, [0]. Like C, arrays start counting elements at zero, so the range of the number of the elements of intArray is 0[nd]5, rather than 16 (WRONG). Because arrays are objects, they have properties. One of those properties is a length. There's no sizeof operator in Java, and, in the case of arrays, all you need to know is how many elements there are. You can get that by using
int x = someArray.length; Note that this is not a method call. It is a data member that you don't have to define. Because the value of length is always going to be one greater than the number of the highest element in the array, to examine the highest
element, you need to look at length - 1. In your case, there are six elements in the intArray, but the highest element is element 5, because the first element is intArray[0]. So if you subtract one from the length which is six, you get five. You can use this information to loop through an array in a for loop, for example. This is discussed further in Chapter 7, "Syntax." There's another feature of arrays in Java that you need to know. Java provides for arrays of arrays, or ragged arrays. C supports multidimensional arrays of contiguous memory. Java doesn't work like that. Let's say I have this declaration:
int[][] doubleIntArray = new int[6][5]; In C, there would be thirty contiguous elements together. In Java, however, the location in memory of the second dimension for any given element in the first dimension is unpredictable. That means the memory for doubleIntArray[0][1] might be nowhere near the location in memory of doubleIntArray[3][6]. Because Java works this way, you can do something like this:
public class ArrayMakerNd { int[][] intArray; public ArrayMakerNd() { intArray = new int[6][]; // Create an array that will accept six int arrays } public boolean initializeArrays() { intArray[0] = new int[10]; intArray[1] = new int[4]; intArray [5] = new int[97]; return true; } } So you can define different length second dimensions. It will help if you don't think of them as second dimensions, but as simply another array. Java may allocate memory for any of these arrays wherever it wishes. So, if you are wondering if Java can do three-dimensional arrays, the answer is that you can do any level of arrays of arrays you want, dynamically. You can have fifteen dimensions if you want. Notice, also, that it is illegal to do this:
//WRONG.
CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067008186051231024014031024
Chapter 5. Methods
Much that needs to be said about defining methods has appeared in earlier chapters, but there are other important features of method definition.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark 2002, O'Reilly & Associates, Inc. Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067009214093043026238011079
Method Signatures
As shown in Chapter 3, "Java Program Features," a method signature's syntax looks like this:
<access modifier> <other modifiers> return-type method-name(<parameters>) <throws exception-class> Again, the <> pairs mean this is optional. Unlike C, you do not need method prototypes. The compiler resolves method names which occur farther down in the class definition or in other classes without a problem, as long as the compiler can find the class and method. The same holds true for data members, which may be coded at the bottom of a class definition. The javac compiler doesn't care.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067009214093040106241105178
Parameter Names
Choose names carefully for clarity. Apart from names that don't communicate anything meaningful like doIt(), avoid the common technique of naming parameters the same as data members. Listing 5.1 shows poor parameter name choices.
Listing 5.1 Poor Parameter Name Choices
public class LibraryCard { String name; int value; public boolean verify(String name, int value) { name = "Ted"; // Don't do this // if you want to assign "Ted" to the // data member name. } } When Java looks at a variable name in a method, it has to decide in each case whether you are referring to a data member, local variable, or parameter. By default, Java first assumes you meant a parameter. In the preceding code, that is not what was intended, because the string "Ted" is assigned not to the data member name but to the parameter name supplied in the method call. To avoid this confusion, you need a way to distinguish between the data member and the parameter.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067009214093040105237101006
this
This ambiguity in the code, which might result in unexpected results, is avoided by using the this object reference. The object reference that points to the current object whose code you are executing at the moment is this. You don't declare this yourself. It's supplied by the Java runtime system. So if I'm in the verify method, and I want to ask the current object to do something, I could code
this.someMethod(); Except for special uses we will see later, it is generally redundant to code this. In the preceding example, however, we need to use this. Listing 5.2 shows the class with a modification that removes the ambiguity and behaves as desired.
Listing 5.2 Using the this Object Reference to Disambiguate a Variable Name
public class LibraryCard { String name; int value; public boolean verify(String name, int value) { this.name = "Ted"; // Using "this" means // "apply this to the data member of this // instance." this.name = name; // You have to use "this" to assign the // parameter's value to the // data member if they have the same name. } } Note Using the name of a data member for a parameter is confusing. Also, writing extra code with this prefixed to it is really suboptimal. I recommend that you avoid having to do this, or having to remember, by always using a different parameter name than any data member name.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067009214093041082040173010
Modifiers
There are a numberof modifiers you can use concurrently or by themselves in the definition of a method. These modifiers, such as final and static, provide various functions in terms of how they make your methods behave. Each has design and execution considerations and consequences, so they should all be used with care. For example, the final modifier makes your method non-overrideable. That's good for security, but it might not be desirable if you are shipping classes that you expect others to subclass, because they will not be able to change a final method's definition. Static methods are methods that can be used independently of an instance. They can be handy for running mathematical formulas, but in general they make your code less flexible. A class with nothing but static methods isn't going to be meaningful to create instances of. Do you really want a singleton? If not, avoid static methods. Each modifier is explained in the following sections. Caution In a Java 2 environment, especially with Hotspot, you should not use final or static to get the compiler to inline the method. You do not want to tie the hands of the compiler or runtime system (Hotspot) that way, because your effort to make your code inlined may in fact result in suboptimal runtime execution. You should leave the compiler and the runtime environment, especially Hotspot, free to inline or not, as seems best to the JVM.
Final A method that is marked final cannot be overridden. There are a couple of reasons to do this. One is for security purposes. If you want to be sure no one overrides your method and redirects a user ID and passwordor the credit card number or other sensitive informationto an inappropriate destination, you should code your method as final so that no one can inherit the name and change its behavior in a subclass. Another reason might simply be that you really don't want anyone to change how the method works in a subclass. For example, it implements very specialized business logic that is very sensitive, and you don't want that logic modified at all. If the method is final, all subclasses get this sensitive logic for free, and if it needs to be changed, it needs to be changed only once, even though you also get the benefits of inheritance. Static Marking a method as static indicates that the method can be used with or without an object instance, because it is a class method. You might use this because you want to be able to use a method even if there is no instance of the class. The most important example is main, but you can easily do this with any class to do any sort of processing. From a practical perspective, you can think of a static method or variable as existing in memory only once. In other words, if
five objects call a given static method, they are all calling the same copy. Static methods are associated with a class and are sometimes called class methods. You can invoke them without needing an object reference. Instead, you only need the name of the class and the name of the method. Here is a common example of this:
Properties p = System.getProperties(); The class System has several static methods, including one called getProperties(). To execute this method, it is not necessary (or, in this case, even possible) to obtain an object reference to the System class. Instead, you simply use the class name and append to it the name of the method. If you have a class that contains static methods, but you have one or more instances of it, you might use either the class name or the object reference name. For example, Listing 5.3 shows a class, Catalog, that has a static method and a public constructor.
Listing 5.3 A Class with a Static Method
public class Catalog { String catalogName; public Catalog(String name) // Constructor { catalogName = name; } public static String getCatalogName() // Accessor method { return catalogName; } } // End Catalog class definition Now, I can execute the getCatalogName static method two ways from another class:
public class Searcher { String name; // Catalog name public Searcher() { name = Catalog.getCatalogName() // Use the class name Catalog myCat = new Catalog ("Aers and Letters"); name = myCat.getCatalogName() // Create an instance and use the object reference name } } // End Searcher class definition Caution Beware of making all your methods static. This might sound like an easy way to code, but it means that you never need a constructor. That's right. Since there are no instance methods, if all your data is defined as static, you never need to instantiate objects of this class. In fact, you can't instantiate objects of a class that has only static variables and static methods. You can have only one of whatever class you are defining in memory at once.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=45 (2 of 5) [6/2/2002 9:47:47 PM]
Also, you create problems for yourself if you want to serialize the object for this class. In the case of a static object, the one "object" essentially equals the in-memory copy of the class definition. Java provides only default values for static fields of serialized objects. Serialization is discussed fully in Chapter 12, "I/O in Java." This is a significant deficiency and should be avoided unless there are compelling reasons not to. The use of singletons also makes such classes difficult to subclass.
Note An object that cannot be instantiated, for which there is always only one instance because all the data and methods are static, is called a singleton in design pattern parlance. Unless there is a clear rationale for having only one "instance," it is considered bad design to use only static methods and variables in an object.
The other main use of static methods is to simply execute a function, like computing a square root, in which you don't really need an object. Suppose you want to get a random number. You don't need any special objects. You only want a numeric value, so you use java.lang.Math's
public static double random() method. This returns a "random" double number between equal to or greater than 0.0 and less than 1.0. Because that's all you wanted, there's no point in creating a Math object just to get this number. Thus, this method is a static method. You might also use a static method when you need to run some code that does a task not specifically related to an object. An example is the
public static void gc() method. This method requests that garbage collection take place. It is a static method in the System class. The java.lang.System class itself is a singleton that cannot be instantiated by any user-defined object. I don't specifically need an object after this method call; I just need a way to make the gc() call. You have to have some class own the method, because it's not legal to have a method outside a class. Therefore, an appropriate class was chosen for this method.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
static 2nd 3rd 4th 5th 6th 7th objects serializing singletons security final methods serializing objects singletons static methods 2nd 3rd 4th 5th 6th 7th Catalog class 2nd class methods code running constructors functions executing objects serializing System class System class static methods
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067009214093046160106204060
Access Modifiers
The purpose of access modifiers is to find which objects have access to your object's methods (or data members). In general, you want to provide public interfaces to your private data, while making most of your methods that do real work, as well as your data, private. There are times when other options might be desirable. For example, you might allow the classes in your package to have access to your methods. In this case, you might give them package-level access. Or, you might want all the classes of your package, and any subclasses in any other packages, to have access to your methods, an essential ingredient of inheritance by those subclasses. In that case, you might make your classes protected. For well-encapsulated classes, which minimize maintenance, I recommend that you keep your methods and data as inaccessible as possible while still providing for reasonable access by other objects to get work done. As stated in an ear lier chapter, there are four access modifiers. Public Methods designated public can be called from any class in any package. Most of the methods in the JDK classes are public so you can call them from your own classes. Listing 5.4 shows the definition of public methods.
Listing 5.4 A Class with a Public Method
public class GUI extends Frame { public GUI() { setBackground(Color.blue); setSize(400,300); setVisible(true); } } // End of GUI class definition This class, GUI, extends Frame, and Frame inherits methods from Window, Container, and Component. Since those methods, like setVisible(), are public, I can call them from my GUI class. Protected If I used a methodthat was marked protected, I would only be able to call it, such as disableEvents(), from the java.awt package or from a subclass of Frame. I'd still be okay here because GUI subclasses and inherits from Frame. I changed my class definition so it does not subclass Frame, as shown in Listing 5.5.
Listing 5.5 A Class Illegally Using a Protected Method
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=46 (1 of 4) [6/2/2002 9:47:51 PM]
public class GUI { Frame aFrame; public GUI() { aFrame = new Frame(); aFrame.setBackground(Color.blue); aFrame.disableEvents(); // Error: Call to a protected // method from an ineligible // class aFrame.setSize(400,300); aFrame.setVisible(true); } } // End of GUI class definition Because disableEvents() was defined as protected rather than public, this class would not compile. This is because GUI would not be part of the same package, java.awt, and does not subclass Frame, so you would not have access to this method. It's okay to mark a method as protected. Just be sure you understand the implications of that access modifier. You restrict how your class can be usedat least, this method of your class. Private Private methods cannot be accessed by any object of another class, although they can be accessed from other methods defined by the same class. This is the appropriate access modifier for methods that do the real work in your class. They implement the actual logic that your object needs to carry out, such as validating data, and modifying private data members. In contrast to private methods, an object's public methods are for other objects to request that your object do something, and yet to not provide direct access to your object's data. A public method should then call a private method to get real work done. Listing 5.6 shows a class that declares both public and private methods accordingly.
Listing 5.6 A Class with Both Public and Private Methods
public class Book { private String borrower; public boolean checkOutBook(String borrowerID) { public boolean result; result = validateBorrower(borrowerID); result = checkOut(borrowerID); return result; } private boolean validateBorrower(String id) { result = false // Check database for ID // if(id - CURRENT) result = true;
return result; } private boolean checkOut(String ID) { borrower = id; } } In this code, checkOutBook() is marked public, so it can be called from anywhere. The method checkOut(), however, is marked private. This means that objects not of type Book cannot access this method. Tip Access level is an important design principle that will lower your program maintenance. If checkOut() were public, any object could call the method and set the borrower ID, even if the ID were totally bogus. Because the methods validateBorrower() and checkOut() are both private, the actual data member borrower is protected from any object that is not a Book, just putting whatever it wants to in this field when it publicly calls the checkOut() method. This means one kind of object cannot corrupt the data in an object of another class. Making the methods that do actual work private, and having them called from public methods, also means that you can change your implementation all day long in validateBorrower(), such as switching from a flat file to an object database management system (ODBMS). No other class is affected by this change because no other class participates in how objects of your class get work done. This helps maximize encapsulation and cohesion and minimizes coupling between classes.
Package-Level Default Package-level access limits access to methods of one class to only the same class or other classes in the same package. Package-level default access is more restrictive than protected but less restrictive than private access, in which objects can call the methods. There is no keyword for package-level access. This is the access level you get if you specify nothing. This permits classes in the same package to access methods of a class in the same package but doesn't allow subclasses outside of the packageor, indeed, any classes outside the package to use the method. You might find that for some classes that need to function solely inside a package, methods that you might otherwise mark as public should be package-level access, with the other methods of the class marked as private. This could be reasonable if you had, say, a class that did bank transaction logging inside the Banking package but there was no reason for any class to subclass this application-specific class outside the package. Or, you might have a Catalog class in the gradschool.library package that should be accessible from classes that find books or add books to a Library object's collection, but there might not be a good reason to allow any class outside the gradschool.library package to access any methods of the Catalog class, while giving full access to the getter/setter methods of the Catalog class in other classes in the same package. Remember, you don't need to allow others to subclass your classes.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067009214093046164216059218
Other Modifiers
A number of other modifiers are useful in defining methods. You can specify that a method should synchronize its resources, or that the method is for a native code implementation. These options are discussed next. Native Declaring a method as native means that you are giving the signature for a method that will be actually coded in another language, such as C. You can then call this code from a Java method. This feature is called the Java Native Interface, or JNI. This is a big topic that whole books cover, so this section will just point you to the basics. Caution This topic has a lot of potential "gotchas," so I recommend it only for experienced Java programmers.
Native methods are used when you really need to accomplish something that you can't accomplish in the JVM. This might be special number crunching that you think might be better done in C, or it might be using C, for example, to call a proprietary API that Java can't call, like IMS DL/I to hook up Java to a legacy application. Since other languages such as C vary from platform to platform, calling a native method is by definition non-portable code. So if you decide to not write 100% pure Java applications, be aware that they will need even more testing because of the need to validate the non-Java code on multiple platforms. Listing 5.7 shows a small sample class.
Listing 5.7 A Class That Declares a Native Method
public class IntenseMath { static { System.loadLibrary("intense"); } public native long doIntenseCalculation(long numb); } Notice that the doIntenseCalculation() method is marked as native but has no method body. A native method is implemented in another language, like C or C++. There are rules to observe about accessing Java data members, arrays, Strings and the like, and you'll need to check on them when you write JNI code.
After you've written the Java class(es) and the native methods in another language, you need to create a special JNI header, .h, file. You use the javah tool to do this. The header file must be named after the Java class that calls the native method. So in this example, the header file is called IntenseMath.h. The javah utility produces the header file for you, and you should not edit the file. You then need to use this header file in the native function. Once you've created the header file, and then created the native method, you need to compile them. As a last step, you need to create a shared library for the native code to reside in, which can then be loaded by Java when the static initializer method runs and loads the "intense" library. Because creating a shared library is a platform-specific activity, you need to check the specific syntax for your platform. That's the minimal basics of doing this in Java. You can also call C++ functions, but you have to adhere to C naming conventions. In addition, JNI can allow your Java code to be called from C as well as making calls to C. The keyword native signals to the compiler that the method implementation is done in another language elsewhere. Java uses native methods to call native peer code for AWT components all the time, so JNI, while not portable, is pretty common in the JDK, though Javasoft is working to reduce the amount of C code with each release. Finally, let me note that in Java 2, you can have control over the version of the shared library you load, and there is new support to avoid loading multiple copies of the same shared library or having namespace collisions. Using JNI is a very complicated topic and is not "Pure Java," so it will not be covered further in this book. Synchronized You might mark a method as synchronized. What this does is cause Java to acquire a lock on the object whose synchronized method was invoked. This prevents other threads from invoking this or another synchronized method on the same object simultaneously. The general purpose of marking a method as synchronized is to protect resources from being corrupted. To illustrate this, think about a semaphore objecta flag that shows whether a particular resource is in use. Listing 5.8 shows a class with such a flag, used to track appointments at the library's reference desk.
Listing 5.8 A Class That Illustrates a Resource That Can Be Changed Incorrectly by Multiple Threads
// A Vector for storing Appointment objects // A flag to show if the ApptBook object is // in use
ApptBook() { appts = new Vector(50,2); // Instantiate the appts Vector, being careful // to specify an initial number // of elements and a reasonable // amount by which to increase the capacity inUseFlag = false; } boolean addAnAppt(Appointment app) { // You'd have to define an Appointment // class for this to compile boolean result = false;
if(inUseFlag == false) // If the ApptBook object is not in use, // do this: { InUseFlag = true; // Set the flag to show this ApptBook // object is in use appts.addElement(app); result = false; } inUseFlag = false; // Show you are done with the ApptBook // object return result; } } // End class definition for ApptBook
There's a potential problem. If multiple threads call the addAnAppt method at the same time, you could get corrupted data. Here's how: 1. ThreadA calls addAnAppt and sees that the inUseFlag equals false but stops running before the inUseFlag is set to true. 2. ThreadB calls addAnAppt and sees that the inUseFlag equals false. 3. ThreadB adds an element. 4. ThreadA adds an element, now in the wrong order. Without making this more complicated, the simple issue is that, because you can't predict when a given thread will stop or start executing, especially when moving from platform to platform, you have no idea where in this code you might be when your thread stops or starts running again. This could lead to data inconsistency because one thread thinks that the inUseFlag is false when logically it should not be because the other thread has already tested it and should have exclusive control of the ApptBook object. We need a way to prevent ThreadB from entering the addAnAppt method at all until ThreadA has executed the entire method, no matter how long that takes. Listing 5.9 shows an improved version that does not have the potential for incorrect changes being made to the inUseFlag by multiple threads. The way to do this is with the synchronized keyword.
Listing 5.9 A Class Illustrating the Use of the synchronized Keyword
// A Vector for storing Appointment objects // A flag to show if the ApptBook object is // in use
ImprovedApptBook() { appts - new Vector(50,2); // Instantiate the appts Vector, being careful // to specify an initial number of elements and // a reasonable amount by which to increase the // capacity inUseFlag = false;
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=47 (3 of 5) [6/2/2002 9:47:56 PM]
} synchronized boolean addAnAppt(Appointment app) { // You'd have to define an Appointment // class for this to compile boolean result = false; if(inUseFlag == false) // If the ApptBook object is not in use, // do this: { InUseFlag = true; // Set the flag to show this ApptBook object is // in use appts.addElement(app); result = false; } inUseFlag = false; // Show you are done with the ApptBook object return result; } } // End class definition for ImprovedApptBook Note With the addition of the synchronized keyword, the inUseFlag is unnecessary. Since only one thread might call the method at a time, only one thread might act upon the object at runtime. I've left it in to illustrate that a synchronized method can be used to protect a resource.
I've added the keyword synchronized to the addAnAppt method. This simple change guarantees that only one caller can execute this code at one time. The way this works is that the objects in a synchronized method each have an object lock. That lock is acquired by the first caller of the method. You have to have the object lock to enter the synchronized method. In my office, there's a supply cabinet. To open the cabinet, you have to have the key. There is only one key. So if you get to the key when it's not in use, you can go open the supply cabinet. If the key is in use, you have to wait for whomever has the key to bring it back, and then it's your turn. When a synchronized method is exited, the object lock is released by the holder and can be acquired by another caller. If that caller has been waiting to enter the synchronized method, it now acquires the object lock and may enter the code. It's possible to have many callers waiting for the object lock at one time. See Chapter 11, "Multithreaded Programming in Java," for more information on synchronized methods.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 5. Methods > Overloading Methods < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067009214093047051118116206
Overloading Methods
The basic idea of overloading a method is that it is easier to call a method called println with varying data types, like this:
System.out.println(some String) System.out.println(some double) System.out.println(some boolean) than to have to call special methods for each of these (the following are fictitious methods that don't exist):
System.out.printlnString(some String) System.out.printlnDouble(some double) System.out.printlnBoolean(some boolean) In the first set of methods, I, as the programmer, don't have to remember or look up 5, 10, or 20 versions of println in order to call it for multiple data types. The Java designers thought the first form was easier. As a result, Java provides the ability for you to define multiple versions of the same method which must differ only in the parameter list. This makes calling the println method much easier. As long as there's a form of println defined that accepts the data type you supply, the compiler will select the correct version of the method and at runtime you will call that specific version of the method. You may change other features of the method signature as well, such as the return type, but the key thing that you need to change is the type and/or number of parameters. Another example would be a Painter class with a draw method. You could call the draw method, passing in one of a set of given shapes, such as
public boolean draw(Rectangle r) public boolean draw(Ellipse e) public boolean draw(Point p) All you really need to know is that to get a shape drawn, you need a Painter object instance and that the Painter object has a draw method. You call draw() and pass in a shape and the Painter takes care of the rest, because the compiler figures out which of the several draw() methods you need to have invoked. So if you define a class that needs to do a similar kind of thing to multiple kinds of primitives or objects, consider using overloaded methods.
Delivered for Maurice ling Swap Option Available: 7/15/2002 Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing
All Books
Java > PURE Java 2 > 5. Methods > Polymorphism: Overriding Methods < BACK
158122020158178182047242169203138024176246252243131023021092067009214093047052125187124
public class Book { public GregorianCalendar dueDate; public boolean checkOut() { dueDate = new GregorianCalendar(new SimpleTimeZone(-8 * 60 * 60 * 1000,
graphics/ccc.gif zoneID));
// This is insufficient code // for making a proper GregorianCalendar // object, but it will serve as an // illustration. You'll have to add more // to get a real GregorianCalendar object. dueDate.set((Calendar.DAY_OF_YEAR)+28); // Set the due date to be four weeks from now. return true; } } // End definition of class Book class ReservedBook extends Book { public boolean checkOut() { super.checkOut(); // First, optionally, call the parent's // version of the method. dueDate.set()Calendar.DAY_OF_YEAR)-27); // Reserved books may be checked // out for only one day, so reset // the due date by subtracting 27 days // from it. Still, we need a dueDate // data member, so call on the // superclass's method by the same name // to do its work, and then tweak that // result or do any extra work you might // need to do. return true; } } // End definition of class ReservedBook What I've done here is created a Book class and defined a checkOut() method in it. In that method, I create a GregorianCalendar object to represent the due date for when the book must be returned to the library. I'm assuming here a four-week period that a patron can have a book checked out from the library. This is the checkOut() method for a normal
Book. If a book is on reserve for some reason (such as when a professor wants it available readily to everyone in a class), the book can only be checked out for one day. At the same time, there's no need to write the code to create a GregorianCalendar object twice, once in Book and once in ReservedBook. Instead, my ReservedBook class needs to modify the logic for setting the due date just a bit. Therefore, I'll modify the checkedOut() method in the ReservedBook class by overriding it, but I can still call the checkedOut() method of the parent class, Book. It's not at all a requirement for the subclass to call the superclass's overridden method. It was useful here, but might not be in other cases. There are some rules about what you can do in an overridden method in the subclass:
q
You cannot override a superclass's static method as an instance method. This would not change just the logic carried out by the method, but also the nature of the method, making it an instance method instead of a class method. It is permissible for a subclass to override a static method in a superclass, which effectively hides the static method in the superclass. It is also illegal for a static method in a subclass to override an instance method in a superclass. It is legal for a static variable to override an instance variable. An overridden method cannot throw more Exception types than the super class's version. I'll cover exceptions in Chapter 10, "Exceptions." For right now, simply accept that in the definition of a method you can list exceptions that can be thrown by the method, as shown in Listing 5.11.
Listing 5.11 A Class with a Method Declaring That It Throws Exceptions
public class Networking { public boolean ping() throws IOException, MalformedURLException { // Ping the server } } // End of class definition Networking In this class, the ping() method is declared as throwing two Exception subtypes: IOException and MalformedURLException. Don't worry about what these are. You'll learn about them later. What's important to note here is that the method signature says that it is possible, in case of error, for this method to throw one of these two Exception types. Now, if I code a subclass, I can declare fewer Exception types, but not more. Listing 5.12 illustrates this.
Listing 5.12 A Subclass That Overrides a Method and Declares That It Throws Fewer Exceptions than the Superclass's Version
public class NetProtocolA extends Networking { public boolean ping() throws IOException { // Do a ping with this network protocol, // but since this protocol doesn't deal // with URLs, it makes no sense to // declare that your method can throw // a MalformedURLException. That will // never happen, so don't make other // developers write code to catch // this when it can't happen. } } // End class definition for NetProtocolA I've removed one of the Exception subtypes because this networking protocol doesn't deal with URLs. This is legal to do. On the other hand, the code in Listing 5.13 is definitely not legal.
Listing 5.13 A Subclass That Illegally Overrides a Method Based on the Declaration of Thrown Exceptions
public class NetProtocolB extends Networking { public boolean ping() throws IOException, MalformedURLException, RemoteException // DON'T DO THIS. IT'S ILLEGAL { // Do a ping with this network protocol. } } // End class definition for NetProtocolB Here I've overridden the ping() method and declared that it can potentially throw one of three Exception subtypes. This is not legal and won't compile.
q
A third requirement is that the overridden method must have the same return type. You cannot change return types or go from a void to a specific return type. So you can't write the code shown in Listing 5.14.
Listing 5.14 A Class That Illegally Overrides a Superclass's Method by Changing the Return Type
public class Book { public boolean checkOut() { // Check out a book } } class ReservedBook extends Book { public String checkOut() // ILLEGAL. Won't compile. { // Check out a reserved book } }
q
You are not permitted to make the overridden method more private in access than the superclass's method. For example, if the method in the superclass is public, you cannot make the overridden method version protected. You can, however, make your method more public. This way, if the parent's method is protected, you can override the method and make it public. Listing 5.15 shows an example.
Listing 5.15 An Example of a Subclass That Overrides a Parent's Method by Making the Access Level Broader from Where It Permits Access
public class Book { boolean checkOut() // The access level is package-level default { // Check out a book } } class ReservedBook extends Book { public String checkOut() // The overridden method's access level // is public, which means more objects // can call this method than the // parent's version. { // Check out a reserved book } } Also note that you cannot override a private method in a superclass. You can't really see this method anyway, so that shouldn't
be a problem. However, you might want to think about whether a superclass should have private methods.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021092067009215242171116056222156
Chapter 6. Interfaces
I said previously that everything in a Java program is done by objects interacting with other objects and that those objects must be defined by classes. Well, that's basically true, but there is one important exception. Java has a special entity called an interface. I don't mean the term in the generic sense that interface is often used in software parlance, as a way to access some code. This use of interface essentially refers a list of abstract methods that specifies the names of behaviors that classes can have. An interface, however, has no implementation of the methods in it. Abstract methods have no implementation, but must be implemented by a subclass of the abstract class that declares the abstract method(s). There is, however, another way to create abstract methods. They can be declared in an interface. The Java 2 SDK provides several excellent examples of what an interface can do. Tip An interface is a powerful mechanism for defining behavior among unrelated classes. I suggested earlier that an abstract class is a good tool for enforcing common behavior among related subclasses. An interface is an effective way to enforce behavior among potentially unrelated classes.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 6. Interfaces > An Example from GUI Event-Handling Interfaces < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191094047142062225159030
public abstract interface ActionListener extends EventListener Because this is an interface, it does not define a class. In fact, an interface can only contain public, static, final data members and abstract methods. Note Notice that the interface signature, which is similar to a class signature, contains the word abstract. Because an interface is abstract by definition, including the word abstract is not necessary, but there's no harm in doing so. You might keep the abstract keyword in your own interface definitions to remind people that they must implement the methods in it if they use the interface.
It is possible for one interface to inherit from, or extend, another interface. This is fairly common in the Java 2 SDK. This interface has only one method defined in it:
public void actionPerformed(ActionEvent event); Briefly, here's how GUI event handling works: 1. You create a GUI object. 2. You create an object that implements the ActionListener interface. That means the object implements all the methods in the ActionListener interface. There's only one
method in this interface, so a class that implements it only has to implement that one method. For example,
class OKButtonHandler implements ActionListener { public void actionPerformed(ActionEvent event) { // Handle a click of the OK button in the window } } 3. Hook up this class to the OK button with code like this:
okButton.addActionListener(new OKButtonHandler()); Now, here's how the use of the interface benefits a developer. The addActionListener method registers an instance of the OKButtonHandler class with the Java runtime. That registration tells the JVM that, when an ActionEvent is created for a click on the okButton GUI component, the object that ought to handle this ActionEvent is the instance of the OKButtonHandler class. There's a special mechanism to tell Java which method to call in the OKButtonHandler class. Java doesn't really know anything much about the OKButtonHandler class. However, Java does know that this object is an ActionListener object. That means Java knows that you have promised that this object implements the ActionListener interface. Because this object implements this interface, you have set up a contract with the Java runtime that your object has an actionPerformed() method. Therefore, the object that keeps track of registered event-handler objects, AwtEventMulticaster, knows that it can simply call the appropriate method for any object that implements the ActionListener interface: actionPerformed. So, without knowing anything about your listener object and without having a clue about what your OK button needs to do in your application or Applet, the Java runtime can get a mouse click on the OK button, generate an ActionEvent instance, and call the actionPerformed() method of the OKButtonHandler object. By saying your class implements an interface, you are making a contract with the system that you are going to implement all the methods of the interface your class is declared to implement. So, for example, if you have
public class CatalogSearcher implements Runnable you've just made a promise to the system that your CatalogSearcher class implements all the methods in the Runnable interface. There happens to be only one such method:
public void run() You use the Runnable interface for working with threads. So when you create and start a thread, the JVM knows that it can call the run() method of the CatalogSearcher class. In this way, a Java interface enables you to declare to the Java runtime that you are going to require your class to implement methods of an interface. You can use this to enforce common behavior, with different implementations, across numerous, disparate, unrelated classes.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=52 (2 of 4) [6/2/2002 9:58:09 PM]
You do not have to deal with events to use interfaces. You might have an interface that declares the methods to be used by the data model in a model-view-controller architecture. The controller component does not need to know anything about the data model. It only needs to know about the methods listed in the interface. The controller can then simply call those methods on any object that implements the interface. I strongly recommend this as a design approach. It helps separate the view from the data model. Other examples in the Java 2 SDK where an interface is used include Observer-Observable, the Runnable interface for multithreading, and the interfaces in java.sql which form the basis of how JDBC lets you communicate with databases in a database-neutral way. JDBC provides an excellent example of something else that interfaces are good for. Let's say that you want to create an object, and it's not so important to you from which class the object is instantiated. Rather, what's really important is that the object implements a given interface. Everything you've studied for creating an object returns an object that is of a specific class typefor example,
Window w = new Window(); There is, however, a way to define a method that can return multiple kinds of objects. You can code the following:
Connection conn = DriverManager.getConnection(dataaseURL); Without explaining at this moment what a database URL is or what a database connection is, I want you to notice that Connection is not a class name. It is the name of an interface found in the java.sql package. What this line of code says is, "Return to me any object whatsoever that implements the Connection interface." As you'll see later, that might mean a database connection to an Oracle database, a Sybase database, or a database connection to an InstantDB database. You don't know, and it does not matter as far as you're concerned. You just need an object that implements all the methods in the Connection interface. So, let me emphasize again: an interface is essentially a contract between the implementing class and other classes that all the methods in the interface are available in the class that implements the interface. In the same way that a class is defined, an interface is defined in a .java file in this way:
public interface Logging { public boolean writeLog(byte[] logrec); // Write a log record with an arbitrary byte array } This interface should be defined in a .java file, generally without any other classes or other interfaces. You put this in a file named Logging.java. So far, I've talked mainly about methods. It is possible to define variables in an interface. You might want all classes that implement the interface to have these variables, defined as static final constants, available to them. So, you might decide that the file name you want an object to use when writing a log file is Logging.txt. You could provide for this by making that name a constant in the Logging interface:
public interface Logging { public static final String Logging_File_Name = "Logging.txt"; public boolean writeLog(byte[] logrec); // Write a log record with an arbitrary byte array } All the variables in an interface need to be public, final, and static. That's reasonable because you are trying to assure that all classes using the interface have this same value. If you want a common variable, it is implemented by a base class.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
158122020158178182047242169203138024176246252243131023021084157229191094047142058100235163
Interface Contents
Here again are the elements that can go into an interface:
q
public, static, final data members. This is about the closest you get in Java to a global constant. By default, even if you don't declare them as such, all the data members of an interface are public, static, and final. So you could code
public static final int MAIN_BRANCH_NUMBER = 01; The Java Language Specification says that it is strongly discouraged to redundantly specify these keywords. I happen to disagree and feel strongly that, for the sake of those who don't know the rules, you should specify the keywords. Doing so is useful for documentation purposes and certainly doesn't hurt anything. What's more, the keywords show that the developer understands what he or she is doing. You can, however, omit them without the compiler giving you any grief.
q
abstract methods By definition, all the methods in an interface are abstract because they have no implementation. So it's legal to code
public interface Logging { public abstract boolean writeLog(byte[] logRec); } You are not allowed to put any code in the writeLog() method inside the interface definition. Like an abstract method inherited by a concrete subclass, a class that is declared as implementing an interface must implement every single method in the interface. So, in a class that implements the Logging interface, you must implement the writeLog() method, or the class definition will not compile. Here is a legal version of a class that implements logging:
public CardCatalog implements Logging { public boolean writeLog(byte{ } logrec) { System.out.println(logrec); // Write the log record to the
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=53 (1 of 3) [6/2/2002 9:58:12 PM]
// // // // // }
command line. You probably wouldn't do this in real life but it illustrates a way to implement the interface's writeLog method.
There might be times when an interface contains one or more methods that you have no interest in implementing. The requirement to implement all the methods can be met in a fairly minimal way. The following illustrates this idea.
public CardCatalog implements Logging { public boolean writeLog(byte[] logrec) { // A pair of curly braces and nothing more constitutes //"implementing" a method } } // End class definition for CardCatalog If you do not want to have the method do anything, you can simply code a pair of curly braces and leave it at that. This makes the compiler happy, and you don't have to write any code. In general, outside of GUI event handling, you probably won't do this. If, however, you don't want to implement a method, the preceding code shows you how to avoid it. Be aware, of course, that if you do this for certain methods and interfaces, you defeat the point of the interface. For example, you never want to do this:
public class CatalogSearcher implements Runnable { public void run() [ // the run() method has no code in it } } The preceding code ensures that, if someone tries to create a Thread and use your run() method, nothing happens. The Thread becomes dead essentially immediately. (Other users will know your class has the run() method because it implements the Runnable interface.) There's no point to making a class Runnable if you don't implement the run() method with actual code. Finally, you have to make an interface available to classes just as though it were a class. You must compile the interface. If you used the Logging interface above, and put it in a file called Logging.java, you would compile it like this:
javac Logging.java Compiling an interface creates a .class file, even if the contents do not represent a class as such.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229191094047142054172008002
Chapter 7. Syntax
Now that you've got the object-oriented foundation down, you can look at the basic syntax elements. Here Java will look very much like C, and that's by design. One of Java's design goals, as you may remember from Chapter 1, "What Is Java?," is to be easy for C programmers to learn. Therefore, a lot of the basic syntax is the same.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229191094047143151151065105
Punctuation
You've already seen the basics of Java punctuation. Statements are ended with a semicolon (;). You can also use the comma to separate identifiers, like this:
The other main punctuation you regularly use are curly braces . You use curly braces, followed by no punctuation, to delimit class definitions and method definitions, like this:
class Book { method checkOut() { } // End definition of method checkOut() } // End of class definition for Book
Class definitions are not followed by a semicolon as a C struct or C++ class definition is. Also, as noted earlier, all the methods of a given class must be defined inside the curly braces that delimit the class definition. Parentheses are used to enclose the parameters for a method call and are included in the method signature, even if there are no parameters. Finally, like C, extra white space is ignored by the Java compiler. So, whether you write elegant, highly readable code, like
public class Book { public String author = "none"; public String title = "none"; public String isbn; public String callnumber; public boolelan checkOut() { int checkOutPeriod = 28; // Do checkOut logic here }
public class Book { public String author = "none"; public String title = "none"; public String isbn, callnumber; public boolean checkOut() { int checkOutPeriod = 28; // Do checkOut logic here } }
I think the former is much more readable and easier to maintain, so I recommend its use. In particular, aligning curly braces vertically rather than using K & R-style, which buries curly braces in code, will make your code much more readable and will help resolve unmatched curly brace errors.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
punctuation syntax punctuation 2nd class definitions identifiers method definitions parameters statements white space white space syntax punctuation
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229191094047143147071196164
Keywords
Java has several keywords. You should avoid giving classes, methods, or variables the same names as these keywords. You've already seen several of them, but for completeness, here are several of the common keywords:
abstract native volatile public private protected synchronized instanceof final static class interface for default void return package import break if else do while switch case continue
Note Java has the keyword goto , but it is merely reserved. It is not implemented, so you cannot use goto yourself for anything, nor can you, thankfully, use goto in your code.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229191094047140098048214046
Operators
There are several kinds of operators in Java, including relational, bitwise, and arithmetic. Logical Operators The two logical operators in Java are AND: && OR: || These are primarily used in if and while tests. For example, you might have
// if both conditions are true, // do the following System.out.println("x qualifies"); else // They are not both true if(a ==3 || b < 7) // if one or both of these // conditions is true do the // following code System.out.println("a might qualify");
The AND condition says that both Boolean tests must resolve to true for the overall test to be true. Remember that an if test must resolve to a Boolean condition of true or false. The OR test says that either the first or subsequent Boolean tests must resolve to true for the overall statement to resolve to true. Note Java uses "short-circuit" processing of && and ||. That is, if the system can tell that the first condition makes the statement true or false and doesn't need to check the second condition, it omits the second test. For example, if I code
if(a == 1 && b == 2)
and a does not equal 1, the system won't bother performing the test of b because the outcome of that test won't affect whether the whole statement evaluates to true. The test of a == 1 already resolved to false, making the whole expression false.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=58 (1 of 15) [6/2/2002 9:58:35 PM]
Arithmetic Operators First there are the basic operators for doing arithmetic: + Adds two numbers together. - Subtracts one number from another. * Multiplies two numbers together. / Divides one number by another. % Modulo division of one number by another. This returns only the remainder from a division. There are also postfix operators that provide shorthand notation for operations on two operands. So, instead of coding
int x = 7; x = x +5;
x+= 5;
There is likewise a complementary -= operator that behaves similarly, except that it performs subtraction. There are also similar operators for most operations, such as &= which means op1 = op1 & op2 |= which means op 1= op1 | op2 <<= which means op1 = op1 << op2 >>= which means op1 = op1 >> op2 >>>= which means op1 = op1 >>> op2 ^= which means op1 = op1 % op2 Note These operators can be used within the scope of other expressions. So you might code
Increment/Decrement Operators: ++ and -Another useful pair of arithmetic operators are the increment and decrement operators. These two operators add or subtract one from the value of a variablefor example,
int x x++; x It is x = x
= 6; // After this statement, x equals 7 // After this statement, x equals 6 legal, of course, to code -1;
and
x = x + 1;
The increment and decrement operators are merely shorthand ways of doing this, as are the prefix and postfix operators. Like the former, because you can use the increment and decrement operators in compound statements, you want to be careful when you decide whether to code
x++
or
++x
The former expression evaluates to the value of x and then adds one to it. The latter, ++x, adds one to x first and then evaluates x to determine the value of the expression. Java always evaluates expressions from left to right. Operators of equal precedence are also evaluated from left to right. Therefore, ++x is evaluated first, and then x is evaluated in a compound expression. So,
int y = 0; y = (x++) - c;
In the first version, c is subtracted from x, the result is put in y, and then x is incremented by one. In the second, x is incremented by one, c is subtracted from it, and then the result is put into y. So the order of operation is important. Relational Operators These operators are used to compare two operands. They include
== != < > <= >= ! Is equal Is not equal Is less than Is greater than Is less than or equal to Is greater than or equal to Not
These should be fairly self-explanatory. They are used in if and while tests, like this:
One of the really big improvements in basic syntax in Java or C relates to the == operator. In C, it is possible to code:
The = assignment operator merely assigns one operator to another. In this case, x would be given the value in y. The == equality relational operator, on the other hand, just tests for equality. In the first case, the C compiler lets you assign y to x, and then tests to see if x is non-zero. If so, the statement is true. As noted previously, Java requires tests to resolve to true Boolean statements that equal true or false. Therefore, (x = y) is not that kind of statement because an assignment never resolves in Java to true or false; therefore, you must use the == operator to compare two values for equality. In this way, Java eliminates a common and hard-to-find bug that has caused unexpected results in more than one C program.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=58 (4 of 15) [6/2/2002 9:58:35 PM]
if(aBooelan = aBoolean)
because this is one exception to what is required to make the statement resolve to a Boolean. Because one does not usually compare Boolean variables, this error is unlikely but possible.
The ?: Operator Because the ?: operator also relates to testing for equality, it belongs with these relational operators. This operator is shorthand for an if statement . Here is its syntax:
<test condition> ? <return value for true> : <return value for false>
Here's an example:
int w = a < b ? 5 : 4;
This statement says, if a is less than b, set w to 5. Otherwise, set w to 4. Although this notation exists, it is not used very often, and it is recommended that you use more common if-else statements for readability, but it is certainly syntactically legal. This statement can be used for assignment, as in the preceding statement, but can also be used in other contexts in which you need to evaluate a value. Bitwise Operators Java provides several operators that let you work on data at the bit level. You might, for example, want to save space in memory by putting several flags into one byte, representing each by one bit. Applied to a character in a word processing program, the flags might mean highlighted or normal, fixed width font or proportional font, italic or normal, and so on, with each bit representing two possible states, based on the fact that a bit can only be zero (off) or one (on). Or, you might use a set of bits to show which rows in a page of a relational database table are locked, provided your RDBMS supports row-level locking. There are numerous applications, and bits are regularly used this way to describe the pixels on a computer screen. The operators used for modifying bits in Java are identical to C, except for the >>> operator described following. Here's a brief overview of how the bitwise operators work. The scope of this book does not allow a tutorial on binary arithmetic, so if you are not familiar with this, you might wish to consult a book that discusses hexadecimal numbers and hex arithmetic in detail. For some applications, the bitwise operators are just the right tool for the job. It's possible, however, to go through your whole programming career and never use them, so if this is very foreign, don't stress over it. Here are the
bitwise operators. Note Java likes to work at the int level, not at the byte level. That means that, for the bit shift operators in particular, even if what you try to use is a byte, Java will first promote it to an int before evaluating the expression. This can lead to unexpected results, such as compiler problems for mismatched types, if you try to work with values of different sizes or of too small a size. Always be sure to provide for getting an int result, or be sure to cast the result to something more appropriate for your application.
AND: &
The & operator is used to AND two sets of bits together. Here's how it works. Java compares the bits in the same bit position in each of the two operands. So, for example, bit 6 in operand1 is compared to bit 6 in operand2. You should count bits from the left to the right for this discussion, and the first position is bit 0.
When Java does an AND operation, it looks at the corresponding bits in each operand and either changes the value in the result, or uses the current value from operand1. Here are the rules for what is done:
q q q
If the bits in both operands equal 1, the bit in the result is set to 1. If the bit in one operand is 1 and the bit in the other operand is 0, the bit in the result is set to 0. If the bits in both operands are 0, the bit in the result is set to 0.
Now back to the code snippet. In binary, these are the representations of b1 and b2, and the results when you AND them together:
You can use the & operator to keep a bit on if it's on in another operand; otherwise, turn it off.
OR: |
The | operator is used to OR two sets of bits together. Here's how it works. Java compares the bits in the same bit position in each of the two operands. So, for example, bit 6 in operand1 is compared to bit 6 in operand2. You should count bits from the left to the right for this discussion, and the first position is bit 0.
When Java does an OR operation, it looks at the corresponding bits in each operand and either changes the value for the result or leaves it as it is in operand1. Here are the rules for what is done:
q q q
If the bits in both operands equal 1, the bit in the result is set to 1. If the bit in one operand is 1 and the bit in the other operand is 0, the bit in the result is set to 1. If the bits in both operands are 0, the bit in the result is set to 0.
Now back to the code snippet. Here, in binary, are the representations of b1 and b2, and what you get when you OR them together:
You can use the | operator to keep a bit on if it's on in either operand.
XOR: ^
The ^ operator is used to XOR, or exclusively OR, two sets of bits together. Here's how it works. Java compares the bits in the same bit position in each of the two operands. So, for example, bit 6 in operand1 is compared to bit 6 in operand2. You should count bits from the left to the right for this discussion, and the first position is bit 0.
When Java does an XOR operation, it looks at the corresponding bits in each operand and either changes the value for the result or leaves it as it is in operand1. Here are the rules for what is done:
If the bits in both operands equal 1, the bit in the result is set to 0. If the bit in one operand is 1 and the bit in the other operand is 0, the bit in the result is set to 1. If the bits in both operands are 0, the bit in the result is set to 0.
Now back to the code snippet. Here, in binary, are the representations of b1 and b2 and what you get when you XOR them together:
So you can use the ^ operator to essentially flip bits on and off. Because the XOR can flip bits like this, it is often used in graphics operations. You can modify the screen and then do an XOR to restore the original state of pixels.
Left Shift: <<
The left and right shift operators essentially do binary arithmetic on the operand. They multiply (left shift) or divide (right shift) the operand by a power of 2. You should think of each bit position in a byte as a power of two. In a byte, the right-most bit is the bit position for 20. The left-most bit in a byte is the bit position for 27. It's important to remember that when doing this sort of operation, as in other mathematical operations, Java promotes bytes to ints. It is best to define operands that you want to do bit shifts on as ints or longs to start. Ints are better because longs require emulation, which can cause a performance problem. Here's an example of how the << operator works.
The result of this operation is that b now equals 00001100. That is, all the bits were shifted left two positions. In the statement
b = b << 2;
the 2 indicates how many places to shift the bits to the left, or what power of two by which to multiply the byte b. Also, like all mathematical operations in Java, bit shift operations are signed. So numbers are kept positive or negative as a rule, except when you use the >>> operator. Note Due to hardware issues, Java will not perform a shift of 32. If you specify 32 for the number of bits to shift left or right, Java will do nothing. If you specify a number higher than 32, Java will shift it by the modulo of the number you specify and 32 (number of places to shift%32).
You can use the << operator to multiply, or shift, bits to the left, with zeros filling in the low-order bits. This does not apply to the sign bit, the left-most bit position. If the high-order bit was 0 to start, at the end of the shift, it is still 0. If the high-order bit is 1, which means the number is negative, the high-order bit is still one after the shift operation (except, possibly, in the case of an overflow). One of the consequences of this is that if you do a left or right shift operation on a byte, rather than an int, when Java promotes the byte to an int, you might get this:
byte b = -1;
// Binary form:
11111111;
If you do
b <<= 2;
the result is
11111111111111111111111111111100
That is, Java did the shift, but it turned the byte into an int, so Java propagates the sign bit over and over to get up to 32 bits.
The right shift operator works in the opposite direction from the left shift operator. All the elements of the left shift operator, keeping the numeric sign bit the same, propagating the sign bit when you promote from a byte to an int, and so on, is the same. The only difference is that the bits are shifted to the right. This amounts to division by a power of 2. So, if you have
this says to shift the bits in b three positions to the right, or divide it by 23. The result hence would be 00000001, or in decimal, 1. The high-order bits are replaced by 0, and the three right-most bits simply fall off.
Unsigned Right Shift: >>>
Those familiar with C will have recognized all the operators until now. The unsigned right shift operator, however, is unique to Java. This operator instructs the system to perform a right shift, like the right shift operator previously, but not to maintain the numeric sign. If you do an unsigned right shift of a negative number, the high-order bits are replaced by zeros, and a negative number becomes a positive number, because the high-order bit is no longer 1 but 0. You might want to think of this as a "right shift and absolute value" operation. Here's an example:
byte b = -5; // Binary form: 11111011 int I = b >>> 1; // Put the result in an int because of promotion
This tells the system to shift all the bits right one position, or divide by 21. The result is
01111111111111111111111111111101
Notice that the number is no longer negative (the high-order bit is not 1 anymore but 0). This doesn't mean that this number no longer has a sign. It simply means that it is now positive because an unsigned right shift will always set the high-order bit to 0, which equates to "positive."
Complement: ~
The complement operator is the last of the bitwise operators. It is the easiest to understand. It simply flips bits to the opposite value. If the bit is 1, it is turned to 0. If it is 0, it is turned to 1, like this:
byte b = 5; b = ~b;
// Binary form:
00000101
The result is 11111010. There are not any special rules here. It simply reverses the bits. Let me make one final note about bit manipulations. There is a class in java.util called BitSet. A BitSet is a vector of bits. Its methods include and, or, and xor, to perform operations similar to what I've been describing. It also has get and set methods so you can specifically modify a single bit, as well as other useful methods. So if you will be doing a lot of work with bits, and being able to deal with specific bits easily is important, you might want to use a BitSet rather than simply doing these operations on an int. If you don't have experience with hex arithmetic, a BitSet might be easier for you to use.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
^ [EDIT:this symbol should be a caret (Shift+6)] 2nd | [EDIT:s/b pipe symbol] 2nd ~[EDIT:should be a tilde] 2nd AND 2nd 3rd 4th complement 2nd OR 2nd XOR 2nd byte level classes BitSet complement bitwise operator 2nd decrement operators 2nd flags If statement ?: operator increment operators 2nd int level left shift [EDIT:2 left angle brackets] bitwise operator 2nd left shift [EDIT:place 2 left angle brackets] bitwise operator logical operators 2nd minus sign (+) arithmetic operator operators ! [EDIT:s/b exclamation point] != [EDIT:s/b exclamation point and equals sign, no space between] & &[EDIT:s/b double ampersands] 2nd =[EDIT:s/b ampersand and equals sign, no space between] [EDIT:s/b ampersand] 2nd <<=[EDIT:s/b 2 left angle brackets and equals sign, no space between] + (minus sign) ==[EDIT:s/b double equals signs, no space between] ?: [EDIT:s/b question mark and colon, no space between] 2nd [EDIT:1 right angle bracket] [EDIT:2 left angle brackets] [EDIT:place 2 left angle brackets] left shift 2nd 3rd [EDIT:place 2 right angle brackets] right shift 2nd [EDIT:place 3 right angle brackets] unsigned right shift 2nd [EDIT:s/b 1 left angle bracket and equals sign, no space between] [EDIT:s/b 1 right angle bracket and equals sign, no space between] [EDIT:s/b 2 right angle brackets and equals sign, no space between] 2nd [EDIT:s/b 3 right angle brackets and equals sign, no space between] ^ [EDIT:s/b a caret (Shift+6)] 2nd ^=[EDIT:s/b caret (Shift+6) and equals sign, no space between] | [EDIT:s/b pipe symbol] 2nd |=[EDIT:s/b pipe symbol and equals sign, no space between] ~[EDIT:s/b a tilde] 2nd AND 2nd 3rd 4th arithmetic 2nd bitwise 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th & [EDIT:s/b ampersand] 2nd && [EDIT:s/b double ampersands] 2nd [EDIT:2 left angle brackets] left shift [EDIT:place 2 left angle brackets] left shift 2nd [EDIT:place 2 right angle brackets] right shift 2nd [EDIT:place 3 right angle brackets] unsigned right shift 2nd ^ [EDIT:this symbol should be a caret (Shift+6)] 2nd
| [EDIT:s/b pipe symbol] 2nd ~[EDIT:should be a tilde] 2nd AND 2nd 3rd 4th complement 2nd OR 2nd 3rd 4th complement 2nd decrement 2nd increment 2nd logical 2nd OR 2nd relational 2nd syntax XOR 2nd OR bitwise operator 2nd relational operators 2nd right shift unsigned [EDIT:place 3 right angle brackets] bitwise operator 2nd right shift [EDIT:place 2 right angle brackets] bitwise operator 2nd statements If ?: operator syntax ?: [EDIT:s/b question mark and colon, no space between] operators 2nd arithmetic operators 2nd bitwise operators 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th & [EDIT:s/b ampersand] 2nd && [EDIT:s/b double ampersands] 2nd [EDIT:2 left angle brackets] left shift 2nd [EDIT:place 2 left angle brackets] left shift [EDIT:place 2 right angle brackets] right shift 2nd [EDIT:place 3 right angle brackets] unsigned right shift 2nd ^ [EDIT:this symbol should be a caret (Shift+6)] 2nd | | [EDIT:s/b pipe symbol] ~[EDIT:should be a tilde] 2nd AND 2nd 3rd 4th complement 2nd OR 2nd XOR 2nd decrement operators 2nd increment operators 2nd logical operators 2nd operators & [EDIT:s/b ampersand] 2nd && [EDIT:s/b double ampersands] 2nd ?: [EDIT:s/b question mark and colon, no space between] 2nd [EDIT:2 left angle brackets] left shift 2nd [EDIT:place 2 left angle brackets] left shift [EDIT:place 2 right angle brackets] right shift 2nd [EDIT:place 3 unsigned right angle brackets] unsigned right shift 2nd ^[EDIT:this symbol should be a caret (Shift+6)] 2nd | [EDIT:s/b pipe symbol] 2nd ~[EDIT:should be a tilde] 2nd AND 2nd 3rd 4th arithmetic 2nd bitwise 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th
complement 2nd decrement 2nd increment 2nd logical 2nd OR 2nd relational 2nd XOR 2nd relational operators 2nd unsigned right shift [EDIT:place 3 right angle brackets] bitwise operator 2nd XOR bitwise operator 2nd
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229191094047141034114189075
Control Flow
There are five basic statements or constructs that modify the control flow of a program: if-else do-while while for switch if-else The if statement, with an optional else, is a simple construct for conditionally executing a block of code. Here is the basic format:
Java evaluates the Boolean statement in the if clause. If the clause evaluates to true, the code following the if statement is done. If it is false, the code immediately after the if clause is skipped. You might also optionally supply an else clause. In this case, if the if clause evaluates to true, the code between if and else is performed. If the If clause resolves to false, the code immediately following the else keyword is executed. Both the if clause and the else clause can govern one or multiple statements. Here is an example.
Again, I don't have to code an else clause, but frequently you might want to do certain code if a condition is true, and other code if it is false. As noted earlier, it is a requirement that an if clause provide a test that resolves to a Boolean, using a relational operator (unless the operand is itself a Boolean). Therefore, you cannot code something like this:
if(doSomething())
The compiler might not know ahead of time whether the statement will evaluate to true or false, but it has to be able to determine that it will. A method call, unless the method returns a Boolean, does not satisfy that requirement. You can, however, code
To have multiple statements executed for the if or else clause, you need to put the statements inside a pair of curly braces. In fact, for readability, and to avoid accidentally introducing bugs, you should consider always putting your code for an if-else statement inside curly braces, even if there is only one line of code to be executed. do-while The do-while and while loops are similar because in both constructs, a Boolean condition in the while clause is tested to see if the loop should be executed. Here's the syntax for a do-while loop:
Just as in C, this is a way to execute a block of code multiple times based on a condition in the while loop. Unlike
C, however, the while condition must resolve to a Boolean, just like a Java if clause. As long as the condition is true, the do loop will be done. When the while condition becomes false, the loop is no longer executed and you go to the next line of code after the while statement. This means that the do-while loop is always executed at least once because the loop is executed and then the while condition is tested. Here's an example:
do { assignBookDeweyDecimalNumber(); // give the book a number like // "BS 2595.3 BA 1990" addBookToCatalog(); } while(MORE_NEW_BOOKS == true);
Here, while there are more new books to add to the library's collection, this code will assign a call number to the book and add it to the library's electronic catalog database. while A while loop is similar in function to a do-while, with one important difference. A do-while loop is always executed at least once. A while loop can be executed zero or more times. Here's the syntax:
Because the while test is done at the start of the loop, if it is false the first time, the loop will not be executed at all. The code will immediately skip to the first statement after the while loop. If the while condition resolves to true, the loop will be executed until the while condition is false. Here's an example:
while(MORE_NEW_BOOKS == true) { assignBookDeweyDecimalNumber(); // give the book a number like // "BS 2595.3 BA 1990" addBookToCatalog(); }
Here, I've rewritten the do-while loop into a while loop. This way, if there are no new books to add to the catalog at this time, the code won't be done at all. while and do-while loops work the same in Java as they do in C. for The for loop works the same way in Java as in C. Here's the basic syntax:
for(statement (usually initial value)>; <boolean test>; <statement expression>) // Some code
As for an If statement, I recommend that for readability, you put the code for the For loop inside curly braces. Here's an example:
It creates and initializes an int to the value of 0. Just as a reminder, the first element in an array is element 0. Each time through the loop, the value of i is compared to the length of the array. The length, as noted in an earlier chapter, is the number of elements in the array. This is easily-maintained code. You can change the length of the array elsewhere, and this loop will still step through the whole array. The test is to compare the value of i to the length of the array. Because the first array element is element 0, you need to stop before the actual number that "length" is equal to, because otherwise, you'll get an ArrayIndexOutOfBoundsException. The int i is incremented by 1, using the increment operator, ++. Inside the for loop is code that is performed the number of times the for statement is executed. Note that like C++, but unlike C, you can define the variable used for controlling the loop inside of it (int i = 0). You don't have to do this. You might define the variable that controls the loop outside of it, like this:
q q
int i = 5; for(;i < arrayLength; i++) // Keep initial semicolon! { // Do some code }
For a single statement to be done multiple times, curly braces are optional. To execute multiple statements, you must use curly braces. Unlike While and do-while loops, a For loop is generally used for a set number of times. switch The switch statement provides a mechanism to perform code conditionally based on whether a specific condition is true. It is like a set of if statements together. Here's the syntax:
switch (operand) { case case1: <code;> <break> case case2: <code;> <break>; <default:>
<code;> }
You provide an operand and a number of case statements that provide alternative values for this operand. When a case statement is encountered that matches the actual value of the operand, the code following that case statement is executed until you either a) encounter a break statement, or b) you come to the end of the switch statement. That means that if you do code for a specific case, but do not follow that code with a break statement, you will fall through and do the code for all the succeeding case statements until the next break statement or the end of the switch statement. Using a break statement is very important unless you really want the code for all the succeeding cases executed. The switch statement can be quite useful, but it is limited in the data types it can test. The switch statement can test integral types and chars. So I can code a switch statement that tests a byte, short, or int, but not a long, float, double, or any kind of object. You can also switch on a char. Listing 7.1 contains an example.
Listing 7.1 An Example of a Switch Statement
int request = getRequest(); switch (request) // Call a method based on the value of request { case 1: // Same as "if(request == 1) { addBookToCatalog(); break; } case 2: { putBookOnHold(); break; } case 3: { putBookOnReserve(); break; } default: { checkOutBook(); break; } } In this switch statement, I state that I want to test the value of the request variable and perform logic based on its value. Then I provide three case statements. If the value of request is 1, the code for the case "1" is executed. The call to addBookToCatalog() is followed by a break statement. The break statement causes the program to exit the switch code block at that point and continue at the line following the switch block. If the value of request does not equal 1, it goes to the next case statement. It can be coded in any order, but putting the most common choices first is best for performance. If the value of request is 2, the code for the case 2 case is executed. The method putBookOnHold() would be executed if request equals 2, and then there is a break statement. Again, if I didn't have the break statement, I would call the putBookOnHold() method, and then call the next method, putBookOnReserve(). That's not usually what you want, so be sure to use the break statement for each case code block. The final case is the "default" casewhat you should use if request is not equal to 1,2, or 3. What you do here is of course up to you. You might choose to do "error" code in the default case because the operand you are doing the switch on should have had a value
Safari | PURE Java 2 -> Modifying Control Flow: break and continue
All Books
Search
Java > PURE Java 2 > 7. Syntax > Modifying Control Flow: break and continue < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191094047138019126243081
BigLoop: {
String author = rs.getSring("Author"); if(author != "Tacitus) { continue BigLoop; // Do the while statement again } for(i=0, i < totalBookDetails.length; i++) // for loop inside // a while loop { if(publicationDate > 1950) // Test the publication date { break; // Leave the for loop but go ahead // and do the while loop again. } System.out.println(totalBookDetails[i]); } // End for loop } // End while loop Using these loop control statements, you can break out of an inner loop, do another iteration of an inner loop, break out of an outer loop, or do another iteration of an outer loop, either with or without a label, depending upon what and where you put break or continue. The break statement serves the only really valid purpose of a goto, which is to get out of a loop. It does not violate the coding concept of having a single entry point and a single exit point from a method, a very important rule to follow.
Safari | PURE Java 2 -> Modifying Control Flow: break and continue
Note The break and continue statements can cause exceptions and therefore the execution of a final block.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229191093030156009013193116
All Books
Search
Java > PURE Java 2 > 8. GUI Programming I: Applications < BACK Make Note | Read Notes | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191093030156005006235064
Basic GUI coding in applications using the AWT Swing/JFC The Delegation event-handling model
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Read Notes | Bookmark 2002, O'Reilly & Associates, Inc. CONTINUE >
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
All Books
Search
Java > PURE Java 2 > 8. GUI Programming I: Applications > Basic GUI Application Programming < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191093030157211115221115
In a Java GUI, the main component, which all other components depend on, is a Frame. A Frame is the outermost "window" of your application. If you are using a word processing software package is written in Java, the outside border of the program, the rectangle that contains everything, is a Frame. Frame is a specific class in the java.awt package. All other GUI componentsmenus, scrollbars, buttons, and so forthgo inside of a Frame. Even other windows, such as dialog boxes, are dependent on the Frame in your application. Figure 8.1 shows the class hierarchy for components.
Figure 8.1 The AWT Component class hierarchy.
In this UML class diagram, you can see that Component subclasses Object. All Java GUI components (except for menu-related components) are a subclass of Component. Descending from Component are Button and Container. Many GUI components inherit directly from Component. Button was chosen as an example. In addition to providing many methods and data members that all other components inherit, Component provides the basic capability to display a component on the screen. Java components such as Frame and Dialog are also subclasses of Component through Container. Because you place components into a Container, and because all Container classes subclass Component, you can put a Component into a Container, and that Container can be put into a Container just as though it were a Component. This is important for positioning components on the screen, our next topic. This inheritance hierarchy allows us to embed components in containers in components in containers in components!
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 8. GUI Programming I: Applications > Layout Managers < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191091023055040132176106
Layout Managers
There are two ways to position a component inside a container. You can either specify the exact location, width, and height of the component in pixels. This is called absolute positioning. Alternatively, you can rely on a layout manager to determine the size and location of a component on the screen (although you have some control, which varies depending on the layout manager you choose).
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 8. GUI Programming I: Applications > Absolute Positioning < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191091023055047155003247
Absolute Positioning
Although it is not a recommended technique, because many IDE tools use absolute positioning, this will be covered first. Absolute positioning is a means of specifying the exact size and location of every GUI component by pixels. You do this when you tell Java not to do anything about the sizing or positioning of components. You take on that responsibility completely. Using Absolute Positioning (No LayoutManager): Code Example You need to do two things to use absolute positioning. First, remove any LayoutManager implementation object from the GUI by setting its object reference to null. Second, define the position, width, and height in pixels of every component in the GUI (except for a MenuBar, which has a predefined location that cannot be changed without subclassing MenuBar). Listing 8.1 shows how to place a Button in a Frame using absolute positioning.
Listing 8.1 Absolute Positioning Using No LayoutManager Implementation (GUIDriver1.java)
import java.awt.*; class GUI1 extends Frame { // Define a GUI class that subclasses Frame. // This means that everything a Frame is, // a GUI object is, and more. Button okButton; // Declare components outside of any method // so they will be visible to all methods. public GUI1() // Constructor for class GUI. // Do all the setup for the // screen in the constructor // that is the best place. { setLayout(null); // Set the value of the Layout Manager object // reference to null, which means you are doing the // layout yourself. // By default, the LayoutManager object reference // for a Frame object points to an instance of // the BorderLayout class. // This tells Java you the developer // are 100% responsible // for the size and location of // each and every Component in the Frame. okButton = new Button("OK"); // Instantiate the Button object, and give it the // label of "OK". okButton.setBounds(new Rectangle(100,100,80,30));
// Define a rectangular // area for the Component. All AWT components // are rectangular. // The "100,100" gives the x,y coordinates // for the component, relative to the top left // corner of the Frame, which starts at 0,0. // The width is 80 pixels. // The height is 30 pixels. add(okButton); // Add the Button to the Frame. setBackGround(Color.blue); // Set the color of the Frame's background. setSize(400,300); // Set the size of the Frame. setVisible(true); // Make the GUI visible. } } // End class definition for GUI class.
public class GUIDriver1 { public static void main(String[] args) { GUI1 gui = new GUI1(); // Create the gui and let the main thread exit. } } // End class definition GUIDriver1. Figure 8.2 shows a user interface created using absolute positioning rather than a LayoutManager implementation class.
Figure 8.2 An example of absolute positioning using setBounds().
Caution There are two mistakes that you as a novice Java programmer might make when using Frames. First, you might
forget to call either setSize() or pack(). If you don't tell the system the dimensions of the Frame, it gets the default size of 00 pixels, no matter how many components you have placed in the Frame. Second, you might forget to call setVisible(true) . By default, Frames, and, hence, all their contents, are invisible.
I've defined two classes: GUI1 and GUIDriver1. Many (if not most) books on Java combine the contents of these two classes into one. I think this is a design problem because an object should be about one thing. So, a GUI object should logically be about GUIs (the user interface), not flow of control. The main method has nothing to do with the user interface, except for creating it. Therefore, it's best to think of main (and the class it is in) as a CEO or a general. It doesn't do anything itself. It orders other objects to do work. So I always separate main from any classes that actually do work or implement presentation or business logic. Logically, main doesn't belong in these classes. In a well-designed class, you should always be able to ask, "What is this object about?" If you put main in a GUI class, you can't give a single answer to that question because it depends on what method you are in. Tip All the code in this part of the book has a driver class. This approach has benefits for program maintenance. If the classes in an application are connected through a driver class, and main is in the driver class, you can more easily reuse the classes in other applications. You can, however, have a main method in every .java file as long as you don't call it, a technique used to provide for unit testing.
Next, notice that the class GUI1 extends Frame. Note While avoiding the debate about designing with inheritance versus containment, I think it's much easier to inherit from Frame than to instantiate a Frame object separately. If Javasoft ever changes the definition of Frame significantly, no approach to class design will avoid difficulties.
Because GUI1 extends Frame, all the methods and data members that a Frame has, a GUI1 object also has, along with all the data members and methods that a Window, a Container, and a Component have. These include a layout manager; a background color; size; a title bar with Minimize, Maximize, and Close buttons; and a visibility attribute. In Listing 8.1, I set the object reference to the layout manager (which is otherwise set for you by the Container object you are using to a real object) to null. This tells the JVM that the GUI1 class is now totally responsible for specifying the x and y coordinates and the width and height of every single component that is added to the Frame. Caution Absolute positioning of components allows you to exercise total control over their size and location. This approach is easy and works well as long as you don't run your code on more than one platform. Although a Button that is 8020 pixels looks good in Win32, it is unacceptable to the default font in Motif. This means you might be displaying totally illegible text on buttons and the like when your program runs on UNIX. So what might seem like an attractive solution is really an option that is not recommended. Layout managers never have these problems.
The next two lines create a Button object, called okButton, and specify its position in the Frame, as well as its width and height. The constructor specifies that the letters OK will appear on the Button when it is drawn on
the screen. Buttons, like Label and Checkbox components in Java, allow you to specify text and justification for the text (left, right, or center), as well as a font. Unlike a Label, by default you can interact with a Button. It has two visual modes, Pressed and Unpressed. Also, as with a Label or Checkbox, you cannot put an icon on a Button. Only text is allowed. Because all AWT components are rectangular in shape, I have to specify a rectangular area to contain the Button component. The first two numbers are the x and y coordinates, measured from the top-left corner of the Frame (or other container to which you are adding components), which is 0,0. You can use this approach for Panel and Dialog as well. The second pair of numbers specify width and height. Incidentally, Java won't tell you if there's a problem with two components overlapping, or if the MenuBar in the Frame overlaps and covers your components. Tip If you use absolute positioning instead of a layout manager, your GUI will appear better on every platform if you explicitly specify the font size and type to be used by the GUI componentsfor example: setFont("Serif", FONT.PLAIN, 14).
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
code, running on platforms No LayoutManager, code 2nd 3rd setBounds() method absolute positioning of components absolute positioning of components, code 2nd 3rd 4th 5th 6th 7th application programming absolute positioning of components absolute positioning of components, code 2nd 3rd 4th 5th 6th 7th Frame Frames novice mistakes main method driver class methods main 2nd layout managers No LayoutManager absolute positioning, code 2nd 3rd main method driver class methods main driver class pack() setBounds() absolute positioning setSize() setVisible (true)() No LayoutManager absolute positioning code 2nd 3rd pack() method platforms absolute positioning running code positioning components absolute code 2nd 3rd 4th 5th 6th 7th setBounds() method absolute positioning setSize() method setVisible (true)() method
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 8. GUI Programming I: Applications > Leaving Positioning to Layout Managers < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191091023052221149056093
The following sections show examples of how to use each layout manager. FlowLayout Code Example The strategy used by the FlowLayout layout manager is to place components from left to right, top to bottom, as they will fit across the target container. The size of each component is based on the smallest size possible. That is, a Button with the text OK takes up less space than a Button with the text Cancel. Listing 8.2 shows an example of using FlowLayout to arrange and size components.
Listing 8.2 Using FlowLayout to Size and Position Components (GUIDriver2.java)
import java.awt.*; // Needed to find java.awt classes class GUI2 extends Frame { Button shortButton; Button mediumButton; Button veryLongButton; TextField nameTF; // A TextField is a single-line // text input field. TextField passwordTF; public GUI2() // Construct a GUI2 object. { setLayout(new FlowLayout( FlowLayout.LEFT)); // Create a new layout manager object // and replace the current layout // manager object with the new one. // Again, the JVM instantiated a layout // manager object for you of type // BorderLayout. // FlowLayout allows for alignment // requests of LEFT, CENTER and RIGHT.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=68 (1 of 17) [6/2/2002 10:01:41 PM]
// The default is FlowLayout.CENTER. setBackground(Color.blue); // There are sixteen basic Java colors. // Use the default blue color for a // background. setSize(150,200); // Size the outer container. shortButton = new Button("Short"); mediumButton = new Button("mid-Sized"); // Add three buttons with different labels. veryLongButton = new Button("Really BIG Button"); nameTF = new TextField(); // This will be a short TextField // because the FlowLayout layout // only makes the component as big as // it thinks it needs and nothing // here gives it reason to make this // TextField big. passwordTF = new TextField("*********"); // Give this TextField some length by // giving it an initial value. // This is essential for FlowLayout. add(shortButton); // Add components to the GUI. add(mediumButton); add(veryLongButton); add(nameTF); add(passwordTF); setVisible(true); } // End class definition of GUI2.
public class GUIDriver2 { public static void main(String[] args) { GUI2 gui = new GUI2(); // Create the gui and let the main thread exit. } } // End class definition GUIDriver2. Figure 8.3 shows an example of a Container laid out using FlowLayout.
Figure 8.3 An example of Using FlowLayout to position components top to bottom, left to right as space allows.
In this program, I display a simple GUI with three Java Buttons and two TextFields. What's most important to notice is that the three buttons differ in size considerably, and that they are arranged to fit the available space, left to right, top to bottom. Each is big enough to fit the text label defined in its constructor, but nothing more. This is followed by two TextFields. One is very shorttoo short for most data entry items, except perhaps single-letter values like Y/N or M/F. The FlowLayout object didn't see a need for a field that was any larger. The second TextField is larger because it is initialized with a value. Although it was not done here, it is also possible to specify a width for a TextField in terms of columns, even though no value is specified:
Depending on the LayoutManager class, such a request for a specific width might not be honored. The GridLayout, for example, disregards such requests.
Because of this behavior of the FlowLayout class, this layout manager is not suitable for most GUI tasks. If you use FlowLayout, you might get a screen that looks the way you wantuntil the user attempts to resize the Frame. At that point, the positioning of components might change radically. To prevent this, you must make your container unresizable by coding this:
setResizable(false); // Prevent the user from resizing the Frame. Also, FlowLayout is practical only for very simple GUIs. Therefore, although the FlowLayout layout manager is simple to use, it generally leads to suboptimal GUIs. FlowLayout is the default layout manager for a Panel. Table 8.1 lists the default layout managers for the major Container classes you will generally use.
Table 8.1. The Default Layout Managers for Major Containers Default Layout Manager BorderLayout FlowLayout BorderLayout
BorderLayout Code Example A second layout manager included in Java is BorderLayout. This layout manager works by dividing its container into five regions: North, South, West, East, and Center. You can place components in one or more of these five
You do not need to put a component in every region. Any regions without components in them, except for Center, will be taken over by the area in the Center, even if there is no component in the Center region. Unlike FlowLayout, when a container with a BorderLayout is resized, the components in each region continue to have the same relative size and the same positions in the container. The West and East regions might get taller, but not wider. The North and South regions might get wider, but not taller. The Center region will expand to take unused space. Tip Because containers are components, you can place containers, such as a panel, into any of these five regions and place components into the panel. This is actually true for the other layout managers and is recommended because it provides both flexibility and the capability to create complex layouts. You must use a panel, for example, in order to place more than one component in a region of a BorderLayout-managed Frame. Nesting of containers within containers is a good technique for building complex layouts, without having to resort to GridBagLayout.
Specify the location using the pre-defined public static final strings in the BorderLayout class, such as BorderLayout.SOUTH. Specify the location with a string, such as "East". These strings must be spelled exactly like this: "North", "South", "East", "West", and "Center". If they are not spelled as shown here, the code will compile, and it throws an exception at runtime. Use the public static final values predefined in BorderLayout, such as AFTER_LAST_LINE (South) AFTER_LINE_ENDS (East) BEFORE_FIRST_LINE (North) BEFORE_LINE_BEGINS (West) These four values should not be combined with other specifiers for the constraint.
import java.awt.*; class GUI3 extends Frame { Panel p1; Panel p2; Button okButton; Button cancelButton; Label northLabel; // Labels for the Frame. Label centerLabel; Label westLabel; Label eastLabel; Label panelNorthLabel; // Labels for the Panel in the center of the Frame. Label panelSouthLabel; Label panelEastLabel; Label panelWestLabel; Label panelCenterLabel;
public GUI3() // constructor { setBackground(Color.lightGray); // Use normal application color. // Use the default layout for a // Frame, which is BorderLayout. p1 = new Panel(new BorderLayout()); // Override the layout for this Panel. // A Panel is a borderless Container. // Panel and set it to BorderLayout. p1.setBackground(Color.yellow); // Make Panel's edges readily visible. p2 = new Panel(); // Use default layout: FlowLayout. p2.setBackground(Color.cyan); // Make this Panel's edges readily visible. okButton = new Button("OK"); cancelButton = new Button("Cancel"); northLabel = new Label("North"); westLabel = new Label("West"); eastLabel = new Label("East"); panelNorthLabel = new Label("p1 North"); panelSouthLabel = new Label("p1 South"); panelEastLabel = new Label("p1 East"); panelWestLabel = new Label("p1 West"); panelCenterLabel = new Label ("p1 Center"); // Add Components to the Panels. p1.add(panelNorthLabel, BorderLayout.NORTH); // Use constants for regions. p1.add(panelSouthLabel, BorderLayout.SOUTH); p1.add(panelEastLabel, BorderLayout.EAST); p1.add(panelWestLabel, BorderLayout.WEST); p1.add(panelCenterLabel, BorderLayout.CENTER); // Then, add the Panel to the Center
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=68 (5 of 17) [6/2/2002 10:01:41 PM]
// region of the Frame. add(p1,"Center"); // Use Strings for regions. p2.add(okButton); // A form of add for other layouts // since p2 is using FlowLayout. p2.add(cancelButton); add(p2, "South"); // Put this Panel in the South region // of the Frame. // Add Components to the Frame. add(northLabel, "North"); add(eastLabel, "East"); add(westLabel, "West"); setTitle("BorderLayout Example"); // Set the Frame's title. // This method is unrelated to the // LayoutManager used. setSize(300,300); // Size the Frame. setVisible(true); // Make the Frame Visible } } // End of GUIDriver3 class definition.
public class GUIDriver3 { public static void main(String[] args) { GUI3 gui = new GUI3(); } } Figure 8.5 shows an example of a container laid out using BorderLayout.
Figure 8.5 An example of using BorderLayout in all five regions.
CardLayout Code Example The CardLayout layout manager provides a Rolodex metaphor. That is, you create one container, such as a panel, and then create several "cards" to put into the first panel. No matter how many cards you create (usually this is done with panels that contain components), only one of these cards can be visible at one time, just as the cards in a Rolodex or a card catalog at the library are. The CardLayout does not control the size or position of any components. It controls the display of cards. So, for every card, you can choose a different layoutone card with FlowLayout, another with BorderLayout, and so forth. Listing 8.4 shows an example of how to use CardLayout. Tip You can use CardLayout to create the visual effect of a tabbed dialog, not available in the AWT. To do this, you put a CardLayout-based container on the GUI. Above it, use a row of buttons or labels. When you click on a button or label, you switch to the appropriate "card." Of course, the Swing JTabbedPane is easier to use and looks better, but using CardLayout and buttons is about the only way to do this using only AWT components.
import java.awt.*;
class GUI4 extends Frame { Panel card1; Panel card2; Panel card3; Panel card4; Label cardLabel1; Label cardLabel2; Label cardLabel3; Label cardLabel4; CardLayout cl; public GUI4() // Construct a GUI4 object. { cl = new CardLayout(); setLayout(cl); // Create a new layout manager object // and replace the current layout // manager object with the new one. // Again the JVM instantiated a layout // manager object for you of type // BorderLayout. // You have to create and add the cards yourself. setBackground(Color.lightGray); setSize(200,200); // Size the outer container. card1 = new Panel(); card2 = new Panel(); card3 = new Panel(); card4 = new Panel(); cardLabel1 = new Label("This is card one"); cardLabel2 = new Label("This is card two");; cardLabel3 = new Label("This is card three");; cardLabel4 = new Label("This is card four"); card1.add(cardLabel1);
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=68 (7 of 17) [6/2/2002 10:01:41 PM]
card2.add(cardLabel2); card3.add(cardLabel3); card4.add(cardLabel4); // Create "card" Panels and add Labels to them. add(card1, "one"); // The add method for a Container that uses CardLayout // takes a "card" name and an optional String name // for the card. // The optional name is a logical name, not a Component // name or object reference. add(card2, "two"); add(card3, "three"); add(card4, "four"); // Now there are four cards in the "deck," but only // one card can be shown at once. cl.show(this, "one"); // Show the first card. // This show() method is specifically // for CardLayout, not the JDK 1.0 // method to display a GUI. setVisible(true); // Make the GUI visible. } public boolean displayCards() { // Cycle through the cards using Threads. // This technique as it is shown here should NOT be used // normally in a GUI because it causes the GUI to hang. try { Thread.currentThread().sleep(5000); cl.next(this); // Show the next card. Thread.currentThread().sleep(5000); cl.next(this); Thread.currentThread().sleep(5000); cl.last(this); // Show the last card. Thread.currentThread().sleep(5000); } catch(InterruptedException ie) {} return true; } } // End class definition of GUI4.
public class GUIDriver4 { public static void main(String[] args) { GUI4 gui = new GUI4(); boolean result = gui.displayCards(); }
} // End class definition GUIDriver4. Figure 8.6 shows an example of a container laid out using CardLayout.
Figure 8.6 An example of Using CardLayout.
This program uses a few basic methods of the CardLayout class. You can display a specific card using the show() method. You can show the previous(), next(), last(), or first() cards with method calls as well. Note Do not confuse this show() method of the CardLayout class with the deprecated show() method that you can use to display a Frame object.
You can put anything you want to on a given card. A card can be any AWT component you want to use, generally a Container subclass. Again, using a CardLayout layout manager does not affect how components are organized on a given card. It only controls their display. As this code shows, in fact, you ask the CardLayout object to show the card, not the container it governs. GridLayout Code Example The GridLayout layout manager is easy to use. Unlike the FlowLayout class, GridLayout does not vary the size of components. That is, all components are the same size in a grid. The GridLayout layout manager divides the container it governs into a specified number of rows and columns as dictated by the constructor. You add components to the container, and they are placed in the grid left to right and top to bottom, one cell at a time. Each cell, as specified by the number of rows and columns, is equal in size, and every component is given the same size. This makes a GridLayout good for things like calculators, phone pads, and calendars, but bad for most data entry screens. You could also use it for thumbnail images. As just stated, components are placed in cells in the order they are added to the container, and there is no way to specify explicitly that a given component should go in a given cell. You need to plan your grid according to what you are trying to accomplish. Note The "grid" created by the GridLayout layout manager only applies to layout. It is not a spreadsheet, nor does it provide any extra features beyond sizing and positioning components.
Listing 8.5 Using GridLayout to Lay Out a Set of Cells for a Phone Dialer (GUIDriver5.java)
import java.awt.*; class GUI5 { Panel Label Label Label Label Label Label Label Label Label Label Label Label extends Frame gridPanel; label1; label2; label3; label4; label5; label6; label7; label8; label9; label0; labelPound; labelStar;
public GUI5() { setLayout(new GridLayout(4,3)); // Create a grid with four rows and three columns. setBackground(Color.lightGray); setSize(200,300); // Size the outer container. gridPanel = new Panel(); // Create the Labels for the phone pad. label1 = new Label("1"); label2 = new Label("2"); label3 = new Label("3"); label4 = new Label("4"); label5 = new Label("5"); label6 = new Label("6"); label7 = new Label("7"); label8 = new Label("8"); label9 = new Label("9"); label0 = new Label("0"); labelPound = new Label("#"); labelStar= new Label("*"); // Add the Labels to the cells. add(label1); add(label2); add(label3); add(label4); add(label5); add(label6); add(label7); add(label8); add(label9); add(labelStar); add(label0); add(labelPound); setVisible(true);
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=68 (10 of 17) [6/2/2002 10:01:41 PM]
public class GUIDriver5 { public static void main(String[] args) { GUI5 gui = new GUI5(); } } // End class definition GUIDriver5. Figure 8.7 shows an example of a container laid out using GridLayout.
Figure 8.7 An example of Using GridLayout to arrange components.
It is also possible to specify a space between (but not around) cells by using the hgap and vgap values in the constructor to specify the horizontal and vertical gap between cells. Caution If you specify too few or too many cells compared to the number of components you add to the container, the GridLayout layout manager decides for you what the number of rows and columns ought to be. In the latter case, even an empty cell has the same size as the others.
GridBagLayout Code Example The fifth AWT LayoutManager class is the GridBagLayout layout manager. This is by far the most flexible and the most complex layout manager. Like the GridLayout class, GridBagLayout organizes components by cells. Unlike GridLayout, GridBagLayout allows components to span cells, allows you to place a component at a specific location, allows you to decide how much of a cell a component ought to take, and performs other tasks. These settings are done on a component-by-component basis, using a GridBagConstraints object. Because the focus here is on techniques, you will need to consult the API specification for details on what each parameter in the GridBagConstraints constructor means. Some of the more important parameters, however, are defined as follows:
gridXThe cell column that the left edge of the component should start in. gridY The row that the top of the component should start in. gridWidth The number of cells wide that the component should be. gridHeight The number of cells high that the component should be. GridBagConstraints.REMAINDER Size the component so that it spans the number of cells remaining to the right or bottom of the container, depending on whether you used this for the gridWidth or gridHeight parameter. GridBagConstraints.RELATIVE Place the component next in the row or column. This is an alternative to specifying a gridX or gridY.
Note This class was designed by someone outside of Javasoft, and the documentation has never been optimal. Thus, you will need to experiment to see what various settings do.
GridBagLayout requires a fair amount of getting used to before you can predict just what a GUI using it will look like. You might need to tweak several parameters, one at a time, to get the desired effect. Listing 8.6 shows a basic example of using GridBagLayout to lay out a simple data entry screen. Tip You must have an idea of how your screen will look before you start specifying constraints, so it's best to draw it on paper first (preferably on graph paper) so that you can see the proportions of one component relative to another.
Listing 8.6 Using GridBagLayout for a Simple Data Entry Screen (GUIDriver6.java)
import java.awt.*; class GUI6 extends Frame { Panel centerPanel; Label nameLabel; Label emailLabel; TextField nameTF; TextField emailTF; GridBagConstraints c; Button okButton; Button cancelButton; public GUI6() { centerPanel = new Panel(); GridBagLayout gridbag = new GridBagLayout(); c = new GridBagConstraints(); // Create a GridBagLayout layout manager, // then create a GridBagConstraints object to // use to size and position components. centerPanel.setLayout(gridbag); // Set the layout to GridBagLayout. c.fill = GridBagConstraints.BOTH;
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=68 (12 of 17) [6/2/2002 10:01:41 PM]
// Fill the cell with the component. c.weightx = 20; c.weighty = 100; // Weight cannot exceed 100 because it is a percentage. setBackground(Color.lightGray); setSize(300,140); // Size the outer container. Toolkit tk = Toolkit.getDefaultToolkit(); Dimension d = tk.getScreenSize(); setLocation((d.width-300)/2, (d.height-175)/2); // Get the default Toolkit. The // Toolkit is the AWT's "Swiss Army Knife." // It helps provide lots of useful system // resources and information. // Here, it is used to get the platform's screen // dimensions and set the size // of the Frame dynamically. nameLabel = new Label("Name"); nameTF = new TextField(); c.fill = GridBagConstraints.NONE; add(nameLabel,c,0,0,1,1); c.fill = GridBagConstraints.HORIZONTAL; add(nameTF,c,1,GridBagConstraints.RELATIVE,3,1); emailLabel = new Label("Email address"); c.fill = GridBagConstraints.NONE; c.weightx = 70; add(emailLabel, c,0,1,1,1); emailTF = new TextField(); c.fill = GridBagConstraints.HORIZONTAL; add(emailTF,c,1,GridBagConstraints.RELATIVE,3,1); add(centerPanel, "Center"); c.anchor = GridBagConstraints.SOUTH; c.weighty = 0; c.fill = GridBagConstraints.NONE; // Only fill the space in the cell needed for a Button. // Otherwise, you get enormous buttons. okButton = new Button("OK"); add(okButton,c,0,2,1,1); cancelButton = new Button("Cancel"); add(cancelButton,c,1,2,1,1); setVisible(true); } public void add(Component c, GridBagConstraints gbc, int x, int y, int w, int h) { gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = w; gbc.gridheight = h; centerPanel.add(c, gbc); } } // End class definition of GUI6. public class GUIDriver6
{ public static void main(String[] args) { GUI6 gui = new GUI6(); } } // End class definition GUIDriver6. Figure 8.8 shows an example of a container laid out using GridBagLayout.
Figure 8.8 An example of using GridBagLayout.
This code does not show how to use each parameter in the GridBagConstraints object. Instead, it shows that you need to change the value of a given field several times to get the desired effect. As you can see from Figure 8.8, the GridBagLayout layout manager has its own interesting features, and you might want to compensate for them to get the desired effect. Generally, you will have to experiment with the values of several fields to get the exact appearance you want. The payoff is a very flexible way to size and position components that works across all platforms without your having to do any platform-specific work. Note Many Java developers consider GridBagLayout too complicated to be worth the trouble. Instead, they opt for combining several panels, each with a specific layout manager implementation, to achieve the desired effect.
So far, the examples have used GUI components to illustrate how to use a layout manager to size and position components. There are other GUI components that have not been used yet. They will be shown later when event handling is discussed.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
components code to display 2nd 3rd 4th CardLayout class show() method cells layingout code 2nd classes CardLayout show() method Container default layout managers layout manager inheritance code BorderLayout 2nd 3rd CardLayout components, displaying 2nd 3rd 4th cells layingout 2nd FlowLayout components, sizing and positioning 2nd 3rd GridBagLayout data entry screen 2nd 3rd GUI positioning components with BorderLayout 2nd 3rd 4th 5th positioning components with CardLayout 2nd 3rd 4th 5th positioning components with FlowLayout 2nd 3rd 4th positioning components with GridBagLayout 2nd 3rd 4th 5th 6th positioning components with GridLayout 2nd 3rd 4th components arranging CardLayout code to display 2nd 3rd 4th containers unresizable FlowLayout code, sizing and positioning 2nd 3rd positioning positioning with BorderLayout, code 2nd 3rd 4th 5th with CardLayout, code 2nd 3rd 4th 5th with FlowLayout, code 2nd 3rd 4th with GridBagLayout, code 2nd 3rd 4th 5th 6th with GridLayout, code 2nd 3rd 4th with layout managers 2nd Container classes default layout managers containers BorderLayout default layout managers locations 2nd unresizable data entry screen code 2nd 3rd default layout managers containers for Container classes FlowLayout components code, sizing and positioning 2nd 3rd positioning containers
unresizable Frame object show() method GridBagConstraints parameters 2nd GridBagConstraints.RELATIVE parameter GridBagConstraints.REMAINDER parameter GridBagLayout data entry screen code 2nd 3rd gridHeight parameter GridLayout cells laying out, code 2nd components arranging gridWidth parameter gridY parameter GUI application programming positioning components with BorderLayout, code 2nd 3rd 4th 5th positioning components with CardLayout, code 2nd 3rd 4th 5th positioning components with FlowLayout, code 2nd 3rd 4th positioning components with GridBagLayout, code 2nd 3rd 4th 5th 6th positioning components with GridLayout, code 2nd 3rd 4th positioning components with layout managers 2nd BorderLayout code 2nd 3rd CardLayout components, code to display 2nd 3rd 4th containers BorderLayout locations 2nd FlowLayout components, positioning GridBagConstraints parameters 2nd GridBagLayout data entry screen, code 2nd 3rd GridLayout cells, code to layout 2nd components, arranging layout manager classes inheritance positioning components BorderLayout, code 2nd 3rd 4th 5th CardLayout, code 2nd 3rd 4th 5th FlowLayout, code 2nd 3rd 4th GridBagLayout, code 2nd 3rd 4th 5th 6th GridLayout, code 2nd 3rd 4th layout managers 2nd inheritancelayout manager classes layout manager classes inheritance layout managers default containers for Container classes locations containers 2nd methods show() CardLayout class
Frame object objects Frame show() method parameters GridBagConstraints 2nd GridBagConstraints.RELATIVE GridBagConstraints.REMAINDER gridHeight gridWidth gridY positioning components with BorderLayout code 2nd 3rd 4th 5th with CardLayout code 2nd 3rd 4th 5th with FlowLayout code 2nd 3rd 4th with GridBagLayout code 2nd 3rd 4th 5th 6th with GridLayout code 2nd 3rd 4th with layout managers 2nd screens data entry code 2nd 3rd show() method CardLayout class Frame object
2002, O'Reilly & Associates, Inc.
All Books
Java > PURE Java 2 > 8. GUI Programming I: Applications > Java Foundation Classes < BACK
158122020158178182047242169203138024176246252243131023021084157229191091023053084240043219
The main difference between the AWT and Swing is that all Swing classes are lightweight components, which means they can be drawn any way a developer wants, except for the heavyweight containers JFrame, JApplet, JWindow, and JDialog. That is, they do not require native peers. Because no C code on the native platform has to be called to make a platform-dependent component peer, Swing components may look and act however you want them to look and act. Note Even when using the AWT, you can code your own lightweight component by directly subclassing java.awt.Component, but that is beyond the scope of this book.
The practical result is that Swing provides several components not available through the AWT. For example, you no longer need to use a CardLayout and Buttons to simulate a tabbed dialog. Instead, you can use the JTabbedPane component, which functions like a tabbed dialog and is very easy to use. Swing is JavaSoft's direction for GUI development. There are a few important things to keep in mind in moving from AWT to Swing:
q
Except for Canvas and Choice, Swing provides an equivalent to all the AWT GUI components. Generally you can find the Swing component's name by adding a J to the front of the AWT component. For example, Button becomes JButton, and Panel becomes JPanel. Although Swing still uses the same event handling model as the AWT, which is covered later in this chapter, its classes reside in the package javax.swing and in its subpackages. Unlike other javax package names, this package is a core package. You should avoid mixing Swing and AWT components. If you must do this, see Amy Fowler's article at the Swing Connection (http://java.sun.com/products/jfc/tsc/) on mixing heavyweight and lightweight components. Unlike the java.awt.Frame class, javax.swing.JFrame is built with multiple panes. There is a glass pane, root pane, drag-and-drop pane, and so forth (see the Swing Connection article on the various panes for more information). You do not add components directly to a JFrame; instead, you add the components to the JFrame's ContentPane. You can access this by doing either a setContentPane() or a getContentPane(). A default ContentPane (of type.java.awt.Container) is available, or you can replace the default with your own. You are allowed to add components to other panes, but you would do this only under special circumstances. You are not allowed to add components to the JFrame itself. In general, this is the only pane you need to be concerned with, although several more panes are present in a JFrame.
Although Swing is often used interchangeably with JFC, technically Swing is only one component of the JFC. Accessibility
and other features are really separate (but require Swing). Basic Swing Code Example Writing a GUI usingSwing is similar to writing a GUI using the AWT, except that the component names (and sometimes behavior) are changed, and you need to deal with the ContentPane. Listing 8.7 shows an example of a Swing GUI that illustrates some features of Swingbut definitely not all.
Listing 8.7 Basic Swing Coding Example Using JFrame, JList, and Other Swing Components (GUIDriver7.java)
import javax.swing.*; import java.awt.*; class GUI7 extends JFrame { // Extend JFrame, just like Frame. JButton okButton; JButton cancelButton; JList subjectList; JScrollPane scrollPane; JCheckBox englishChkbox; JCheckBox frenchChkbox; JCheckBox germanChkbox; JRadioButton allLibRB; JRadioButton thisLibRB; ButtonGroup libSelectBG; JMenuBar menubar; // Most Swing components have the same JMenu fileMenu; // name as their AWT counterpart with a "J" prefix. JMenu helpMenu; JMenuItem newSearchMI; JMenuItem printResultsMI; JMenuItem exitMI; JMenuItem aboutAppMI; JMenuItem helpIndexMI; Container theContentPane; // Add components to this pane. String[] catalogSubjects = {"Archaeology", "Biblical Studies", "Dead Sea Scrolls", "Graeco-Roman History", "Linguistics"} ; public GUI7() { // JFrame uses a BorderLayout, just like a Frame object. // No need to change that. setSize(600,440); setBackground(Color.blue); // Set the size and background of the JFrame. theContentPane = getContentPane(); // Get a reference to the existing ContentPane // so components may be added to it. okButton = new JButton("OK");; cancelButton= new JButton("Cancel");; JPanel buttonPanel = new JPanel(new GridLayout(7,1)); // Create a JPanel and add the JButtons plus // empty JPanels in order to get the grid cells // and the JButtons to be an acceptable size. buttonPanel.add(new JPanel()); buttonPanel.add(okButton); buttonPanel.add(new JPanel()); buttonPanel.add(cancelButton); buttonPanel.add(new JPanel()); buttonPanel.add(new JPanel());
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=69 (2 of 11) [6/2/2002 10:01:53 PM]
theContentPane.add(buttonPanel, "East"); // Unlike java.awt.List, a JList cannot scroll on its own. // So create a JList and a JScrollPane for it. subjectList = new JList(catalogSubjects);; scrollPane = new JScrollPane(); scrollPane.getViewport().setView(subjectList); // A JScrollPane provides for scrolling without any messy // code for scroll bars. You put a Component, even an image, // into a JScrollPane, and it lets the user scroll // with no programmatic intervention. JPanel listPanel = new JPanel(new BorderLayout()); JLabel listLabel = new JLabel("Catalog Subject List"); // Create a JList and a JLabel for it. listPanel.add(listLabel, "North"); listPanel.add(scrollPane, "Center"); theContentPane.add(listPanel, "West"); // Create a JPanel to hold the JList and a descriptive // JLabel for it. JPanel choicePanel = new JPanel(new GridLayout(3,2)); JPanel containingPanel = new JPanel(new BorderLayout()); JLabel choiceLabel = new JLabel("Limit Results", SwingConstants.CENTER); // Create a JLabel and align the text to the center of // the JLabel's area. containingPanel.add(choiceLabel, "North"); libSelectBG = new ButtonGroup(); // Like a java.awt.CheckboxGroup, this is a non-visual // component that provides for mutually exclusive // JRadioButton selection. allLibRB = new JRadioButton("Graduate Library", true); // Provide two JRadioButtons. Put them in a ButtonGroup // Use the constructor that takes a boolean to specify that // this JRadioButton should be selected // by default when the GUI appears. thisLibRB = new JRadioButton("All campus Libraries"); libSelectBG.add(allLibRB); libSelectBG.add(thisLibRB); // Create JRadioButtons and add them to a ButtonGroup to // get radio button, i.e., exclusive selection, behavior. englishChkbox = new JCheckBox("Works in English", true); frenchChkbox = new JCheckBox("Works in French"); germanChkbox = new JCheckBox("Works in German"); // Note that, unlike the java.awt.Checkbox class, the B in // JCheckBox is capitalized, as it should be. choicePanel.add(englishChkbox); choicePanel.add(thisLibRB); choicePanel.add(frenchChkbox); choicePanel.add(allLibRB); choicePanel.add(germanChkbox); // Remember that Components are added to grid cells // left to right, top to bottom. containingPanel.add(choicePanel, "Center"); theContentPane.add(containingPanel, "Center"); // The basic JMenuBar and JMenu methods are the same as for // the AWT counterparts, so this shows the // basics of an AWT menu. menubar= new JMenuBar();; fileMenu= new JMenu("File"); helpMenu = new JMenu("Help"); newSearchMI = new JMenuItem("New Search");
printResultsMI = new JMenuItem("Print Results"); exitMI= new JMenuItem("Exit"); fileMenu.add(newSearchMI); // Create components for a program menu and then // build the menus. fileMenu.add(printResultsMI); fileMenu.add(exitMI); aboutAppMI = new JMenuItem("About Library application"); helpIndexMI = new JMenuItem("Help Index"); helpMenu.add(aboutAppMI); helpMenu. add(helpIndexMI); menubar.add(fileMenu); // menubar.setHelpMenu(helpMenu); // You can't make the above call as of Sept. 1999. // The setHelpMenu method puts the "Help" menu // on the far right. // However, while this works for a Menu object, // the setHelpMenu()method is not yet // implemented for JMenuBar, so use menubar.add(helpMenu); // of the JMenuBar class. setJMenuBar(menubar); setVisible(true); } } // End of class definition for GUI7.
public class GUIDriver7 { public static void main(String[] args) { GUI7 gui = new GUI7(); } } Figure 8.9 shows an example of a Basic Swing GUI.
Figure 8.9 The basic Swing GUI with JButtons, JLabels, JList, JRadioButtons, and JScrollpane.
Much of how this GUI works, using Swing, should be familiar to you already. It uses the familiar layout managers: BorderLayout, GridLayout, and FlowLayout. It adds components to containers. The big difference in this regard is that the constructor for GUI7 gets the ContentPane of the JFrame. This program also shows how to use menus. The methods used are almost identical to those for AWT menu creation. The two notable exceptions are that you use a setJMenuBar() in Swing rather than java.awt.Frame's setMenuBar(). Also, java.awt.MenuBar supports a setHelpMenu() method that places the menu you designate on the far right of all the Menu objects on the MenuBar. The JMenuBar version of setHelpMenu(), as of July 1999, has not been implemented yet. The code compiles basically, but the program won't run if you try to use setHelpMenu(). Hopefully, JavaSoft will fix this soon. In the AWT, the Checkbox class must do double-duty, functioning as a check box when not in a CheckboxGroup and functioning as a radio button when created with a CheckboxGroup. In Swing, JRadioButton is a separate class from JCheckBox. Caution Remember to put JRadioButtons in a ButtonGroup. If you do not do this, JRadioButtons function identically to JCheckBox components. That is, the choice of a specific JRadioButton will exclude other JRadioButtons from being selected.
In most other respects, this application is pretty similar to a basic AWT GUI. Note Swing is intended as a complete replacement for the AWT. Therefore, most of the other JFC features, such as Accessibility, depend on Swing. In addition, the Delegation event handling model, discussed later in this chapter, applies equally to Swing. Because Swing is what Javasoft wants developers to use, further examples will use Swing rather than the AWT counterparts. Where there are similar components, it should be easy to figure out how to code the AWT version.
Using Accessibility in a Java GUI Application As mentioned in Chapter 2, "New Features in Java 2," the Accessibility feature of the JFC is intended to make Swing-based applications easier to use for the physically challenged. There are many things that you can do with the Accessibility APIs. This section shows a simple example of how to do some of them. Because almost all the work to make an application accessible has been done by the Swing team, there is little reason not to add this feature to your application. The Accessibility APIs allow Java programs to use assistive technologies, such as Braille terminals. It is not necessary, however, for the program to run on a machine that features assistive technology. Rather, Accessibility APIs can work on any Java-enabled system, with or without assistive technologies. There are two basic things that you can easily do with the Accessibility APIs. First, you can add AccesssibleText to a component. This is text that pops up like a ToolTip when the mouse pointer hovers over it. This feature lets you tell a user what the component is used forfor example, "Click this button to submit your order." The second accessible feature you can add to a component is a mnemonic for a component or a keyboard accelerator for a menu. In fact, you need to add a keyboard accelerator to at least one JMenu component in a menu bar in order to make all the menus and menu items accessible components. Note In September 1999, Sun released a beta version of a bridge from Java's Accessibility APIs and assistive devices running under Microsoft Windows.
Tip If you have an ImageIcon in a Component, you should do a setDescription() call for the ImageIcon to help blind users.
Listing 8.8 shows the preceding program with the addition of basic Accessibility features.
Listing 8.8 Accessibility Example Using JButtons, JMenus, and JMenuItems (GUIDriver8.java)
class GUI8 extends JFrame { // Extend JFrame, just like Frame. JButton okButton; JButton cancelButton; JList subjectList; JScrollPane scrollPane; JCheckBox englishChkbox; JCheckBox frenchChkbox; JCheckBox germanChkbox; JRadioButton allLibRB; JRadioButton thisLibRB; ButtonGroup libSelectBG; JMenuBar menubar; JMenu fileMenu; JMenu helpMenu; JMenuItem newSearchMI; JMenuItem printResultsMI; JMenuItem exitMI; JMenuItem aboutAppMI; JMenuItem helpIndexMI; Container theContentPane; String[] catalogSubjects = { "Archaeology", "Biblical Studies", "Dead Sea Scrolls", "Graeco-Roman History", "Linguistics"} ; public GUI8() { // JFrame uses a BorderLayout, just like a Frame object // No need to change that. setSize(600,440); setBackground(Color.blue); theContentPane = getContentPane(); // Set the JFrame's size and background color. // Then get the ContentPane reference from the JFrame. okButton = new JButton("OK");; cancelButton= new JButton("Cancel"); // First, add mnemonics to the JButtons. okButton.setMnemonic('o'); cancelButton.setMnemonic('c'); // Second, add ToolTip text for accessibility. okButton.setToolTipText("Press OK to search for books"); cancelButton.setToolTipText ("Press Cancel to cancel the subject search"); // Third, set AccessibleName for the JButtons. okButton.getAccessibleContext().setAccessibleName("okButton"); cancelButton.getAccessibleContext(). setAccessibleName("cancelButton");
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=69 (6 of 11) [6/2/2002 10:01:54 PM]
// The AccessibleName is part of the AccessibleContext // of a Component, which is the minimal amount of
graphics/ccc.gif
// information // an Accessible Component returns. // This is done using the getAccessibilityContext() // method. JPanel buttonPanel = new JPanel(new GridLayout(9,1)); // Create a JPanel and add the JButtons plus // empty JPanels in order to get the grid cells // and the JButtons to be an acceptable size. buttonPanel.add(new JPanel()); buttonPanel.add(okButton); buttonPanel.add(new JPanel()); buttonPanel.add(cancelButton); buttonPanel.add(new JPanel()); buttonPanel.add(new JPanel()); buttonPanel.add(new JPanel()); buttonPanel.add(new JPanel()); // Create JPanels to put into the JFrame. theContentPane.add(buttonPanel, "East"); // Unlike java.awt.List, a JList cannot scroll on its own, // so create a JList and a JScrollPane for it. subjectList = new JList(catalogSubjects);; scrollPane = new JScrollPane(); scrollPane.getViewport().setView(subjectList); // A JScrollPane provides for scrolling without // any messy code for scrollbars. // You put a Component, even an image, into a JScrollPane // and it lets the user scroll // with no programmatic intervention. JPanel listPanel = new JPanel(new BorderLayout()); JLabel listLabel = new JLabel("Catalog Subject List"); listPanel.add(listLabel, "North"); listPanel.add(scrollPane, "Center"); // Create a JList and JScrollPane for it // so it can be scrolled. theContentPane.add(listPanel, "West"); // Create a JPanel to hold the JList and a descriptive // JLabel for it. JPanel choicePanel = new JPanel(new GridLayout(3,2)); JPanel containingPanel = new JPanel(new BorderLayout()); JLabel choiceLabel = new JLabel("Limit Results", SwingConstants.CENTER); // Create a JLabel and align the text to the center of // the JLabel's area. containingPanel.add(choiceLabel, "North"); libSelectBG = new ButtonGroup(); // Like a java.awt.CheckboxGroup, this is a non-visual // component that provides for mutually exclusive // JRadioButton selection. allLibRB = new JRadioButton("Graduate Library", true); // Provide two JRadioButtons. Put them in a ButtonGroup. // Use the constructor that takes a boolean // to specify that this JRadioButton should // be selected by default when the GUI appears. allLibRB.setMnemonic('a'); thisLibRB = new JRadioButton("All campus Libraries"); thisLibRB.setMnemonic('t');
graphics/ccc.gif
libSelectBG.add(allLibRB); libSelectBG.add(thisLibRB); englishChkbox = new JCheckBox("Works in English", true); frenchChkbox = new JCheckBox("Works in French"); germanChkbox = new JCheckBox("Works in German"); englishChkbox.setMnemonic('e'); frenchChkbox.setMnemonic('r'); germanChkbox.setMnemonic('g'); // Note that, unlike the java.awt.Checkbox class, the B in // JCheckBox is capitalized, as it should be. choicePanel.add(englishChkbox); choicePanel.add(thisLibRB); // Add the JRadioButtons to their JPanel. choicePanel.add(frenchChkbox); choicePanel.add(allLibRB); choicePanel.add(germanChkbox); // Remember that Components are added to grid cells // Left to Right, Top to Bottom. containingPanel.add(choicePanel, "Center"); theContentPane.add(containingPanel, "Center"); // The basic JMenuBar and JMenu methods are the same as for // the AWT counterparts, so this shows the // basics of an AWT menu. menubar= new JMenuBar();; fileMenu= new JMenu("File"); helpMenu = new JMenu("Help"); fileMenu.setMnemonic('f'); helpMenu.setMnemonic('h'); newSearchMI = new JMenuItem("New Search"); printResultsMI = new JMenuItem("Print Results"); exitMI= new JMenuItem("Exit"); newSearchMI.setMnemonic('n'); printResultsMI.setMnemonic('p'); exitMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.SHIFT_MASK)); // Set keyboard accelerator to make all the menu objects // accessible. fileMenu.add(newSearchMI); // Create components for a program menu and then // build the menus. fileMenu.add(printResultsMI); fileMenu.add(exitMI); aboutAppMI = new JMenuItem("About Library application"); helpIndexMI = new JMenuItem("Help Index"); helpMenu.add(aboutAppMI); helpMenu.add(helpIndexMI); menubar.add(fileMenu); // Put JMenuItems in JMenus and JMenu objects in the
graphics/ccc.gif // JMenuBar.
// menubar.setHelpMenu(helpMenu); // You cannot use this method at this time. // The setHelpMenu method puts the "Help" menu on the far // right. However, while this works for a Menu object the // setHelpMenu() is not yet implemented for JMenuBar, so use menubar.add(helpMenu); // of the JMenuBar class. setJMenuBar(menubar); setVisible(true); }
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=69 (8 of 11) [6/2/2002 10:01:54 PM]
public class GUIDriver8 { public static void main(String[] args) { GUI8 gui = new GUI8(); } } Figure 8.10 shows an example of a GUI with Accessibility, although you will have to execute the program to really see the accessible features properly. You will probably need to look at the actual screen, as opposed to the figure, to see the keyboard accelerator indicators.
Figure 8.10 An example of using Accessibility through AccessibleText in a GUI.
This application adds to the previous one (GUIDriver8.java) calls to the setMnemonics method (GUIDriver8.java). This provides a shortcut key to interact with a GUI component, including JMenu and JMenuItem components. In addition, the code calls setToolTipText() to set up the text that is displayed when the mouse pointer hovers over a component. When you set ToolTip text on a component, set a mnemonic, and then put the mouse pointer over the associated component, you get ToolTip-style text that contains both the ToolTip text and the mnemonic. As this code shows, adding Accessibility to code is quite simple.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
comparing components code 2nd 3rd 4th GUI JFC (Java Foundation classes) 2nd 3rd code 2nd 3rd 4th 5th technologiesassistive
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 8. GUI Programming I: Applications > Using Delegation Event Handling < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191091023051104084014182
Event sourcesComponents such as Button, JList, and JMenuItem, as well as Containers. EventsDefined by classes in java.awt.event. The class names have the form xxxEvent, where xxx represents an event type, such as Action or Item. The main event types you will normally deal with are ActionEvents, ItemEvents, FocusEvents, MouseEvents, and WindowEvents. Other useful event classes include TextEvent and MouseMotionEvent. Event ListenersDefined in java.awt.event. Class names have the form xxxListener, where xxx is an event type, such as ActionListener, TextListener, and WindowListener. The event listeners are all Java interfaces. This means that to use them, you need to implement the interface in a user-defined event listener class.
An application defines a GUI. Then it instantiates event-listener objects. Next, the program registers a listener for a given GUI component. When the user interacts with that component (for example, by clicking a JButton), the action causes an ActionEvent to be generated. Note All GUI Components generate events. Some components generate multiple events of different types. Each component, however, has a main or preferred type of event that you should listen for, rather than listening for every event type that it produces. Although you might be able to detect a MouseEvent for a JButton, you should listen only for ActionEvents.
To register a listener on a component, you call an addxxxListener() method, where xxx represents an event type, such as addActionListener() or addWindowListener(). Every such addxxxListener() method takes as a parameter a reference to an object that implements the related interface. So addActionListener() takes as a required parameter an instance of a class that implements the ActionListener interface. There are four ways to implement the Listener class:
q
An inner class that implements the event-listener interface An anonymous inner class that implements the event-listener interface The GUI class that contains the source component, which implements the interface itself
I recommend that, based on good object-oriented design principles, you use only the first option, a separate class that implements the event-listener interface. If you use any of the other three approaches, you end up creating a GUI class that is actually two classes in one: a user interface class and an event-handling class. This is a design problem. Also, you reduce any potential reuse of your GUI code. Any other approach also makes your GUI code more complicated, and in the case of anonymous inner classes, the code might be highly unreadable. It should be noted that this may require creating multiple small event-handling classes that, unless packaged in a jar file, may take longer to download one by one.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Java > PURE Java 2 > 8. GUI Programming I: Applications > Adapter Classes < BACK
158122020158178182047242169203138024176246252243131023021084157229191091023051111139254091
Adapter Classes
As explained in Chapter 6, "Interfaces," a class that is declared as implementing an interface must implement all the methods declared by that interface. For an ActionListener, this is pretty simple, because the ActionListener interface defines only one method:
public void actionPerformed(ActionEvent e) For other interfaces, however, that's not the case. For example, every GUI application should have a WindowListener. The WindowListener interface has several methods declared in it, most of which are not necessary for most programs. In order to implement this interface, you might have to create an event-listener class that has several empty methods, like the one shown here:
public void windowClosed(WindowEvente) { } In order to save developers from having to write empty methods for conditions that programs do not need to deal with, Java provides Adapter classes. These are convenience classes that implement the various methods with empty curly braces, { }. By subclassing an Adapter class, you can override just the method(s) of interest to you, inheriting the empty methods from the Adapter class:
class WindowHandler extends WindowAdapter { // Only implement the WindowListener method you want to use. public void windowClosing(WindowEvent e) { System.exit(0); } } The use of both approaches to event handling (creating a separate event-listening class and using an Adapter class) is illustrated in Listing 8.9. The code is arranged by class in logical order, so the program flow should be easy to follow in spite of the program's length.
Listing 8.9 Delegation Event Handling with Separate Event Listener Classes and Adapter Classes (GUIDriver9.java)
import javax.swing.*; import java.awt.*; import java.awt.event.*; // This import is needed for Event objects and Event listeners. class GUI9 extends JFrame { // Extend JFrame, just like Frame. JButton okButton; JButton cancelButton; JList subjectList; JScrollPane scrollPane;
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=71 (1 of 10) [6/2/2002 10:02:08 PM]
public JCheckBox englishChkbox; public JCheckBox frenchChkbox; public JCheckBox germanChkbox; // The three JCheckBox objects are declared as having // public access so that they can be seen from another class. JRadioButton allLibRB; JRadioButton thisLibRB; ButtonGroup libSelectBG; JMenuBar menubar; JMenu fileMenu; JMenu helpMenu; public JMenuItem titleSearchMI; public JMenuItem printResultsMI; public JMenuItem exitMI; JMenuItem aboutAppMI; JMenuItem helpIndexMI; JTextField titleTF; Container theContentPane; String[] catalogSubjects = { "Archaeology", "Biblical Studies", "Dead Sea Scrolls", "Graeco-Roman History", "Linguistics"} ; public GUI9() { // JFrame uses a BorderLayout, just like a Frame object. // No need to change that. setSize(600,440); setBackground(Color.blue); theContentPane = getContentPane(); // Set the size and background color and get the // ContentPane reference. titleTF = new JTextField(); JLabel titlelabel = new JLabel("Title Searched for: "); JPanel titlePanel = new JPanel(new GridLayout(2,1)); titlePanel.add(titlelabel); titlePanel.add(titleTF); theContentPane.add(titlePanel, "South"); // This JTextField will be updated by a method called from a // JDialog to illustrate how to get input from a Dialog/JDialog // to another object easily, short of making remote method
graphics/ccc.gif
// calls // with RMI or CORBA. okButton = new JButton("OK"); cancelButton= new JButton("Cancel"); JPanel buttonPanel = new JPanel(new GridLayout(9,1)); // Create a JPanel and add the JButtons plus // empty JPanels in order to get the grid cells // and the JButtons to be an acceptable size. buttonPanel.add(new JPanel()); buttonPanel.add(okButton); buttonPanel.add(new JPanel()); buttonPanel.add(cancelButton); buttonPanel.add(new JPanel()); buttonPanel.add(new JPanel()); buttonPanel.add(new JPanel()); buttonPanel.add(new JPanel()); // In the above lines, I'm using a trick to get the JButtons // sized and positioned the way I want them. I do this by // creating a JPanel with a GridLayout arrangement. // Then I add empty JPanels and put them in the cells. // This creates more cells and thus makes each cell
// shorter top to bottom. This technique also guarantees // that each JButton is aligned vertically and that all are // the same width, regardless of the text label for each. // Now, set up to listen for button clicks. ButtonListener buttonListener = new ButtonListener(this); // Create an ActionListener object to listen for button
graphics/ccc.gif
// clicks. // In order for the buttonListener object to read/write // a Component on the screen, the listener object has // to have a reference to the GUI object. Do this by // passing "this" (object reference of current GUI9 // object) to the ButtonListener constructor. okButton.addActionListener(buttonListener); cancelButton.addActionListener(buttonListener); // JButtons, Buttons and JMenuItems generate // ActionEvent objects. This code "delegates" // responsibility for handling these ActionEvents from // the two JButtons to the buttonListener object. theContentPane.add(buttonPanel, "East"); // Unlike java.awt.List, a JList cannot scroll on its own. // So create a JList and a JScrollPane for it. subjectList = new JList(catalogSubjects);; scrollPane = new JScrollPane(); scrollPane.getViewport().setView(subjectList); // A JScrollPane provides for scrolling without any messy // code for scroll bars. You put a Component, even an image, // into a JScrollPane, and it lets the user scroll // with no programmatic intervention. JPanel listPanel = new JPanel(new BorderLayout()); JLabel listLabel = new JLabel("Catalog Subject List"); listPanel.add(listLabel, "North"); listPanel.add(scrollPane, "Center"); theContentPane.add(listPanel, "West"); // Create a JPanel to hold the JList and // a descriptive JLabel for the JList. JPanel choicePanel = new JPanel(new GridLayout(3,2)); JPanel containingPanel = new JPanel(new BorderLayout()); JLabel choiceLabel = new JLabel ("Limit Results", SwingConstants.CENTER); // Create a JLabel and align the text // to the center of the JLabel's area. containingPanel.add(choiceLabel, "North"); libSelectBG = new ButtonGroup(); // Like a java.awt.CheckboxGroup, this is a non-visual // component that provides for mutually exclusive // JRadioButton selection. allLibRB = new JRadioButton("Graduate Library", true); // Provide two JRadioButtons. Put them in a ButtonGroup // Use the constructor that takes a boolean // to specify that this JRadioButton // should be selected by default when the GUI appears. thisLibRB = new JRadioButton("All campus Libraries"); libSelectBG.add(allLibRB); libSelectBG.add(thisLibRB); // Create JRadioButtons and add them to a ButtonGroup // so they behave like radio buttons. englishChkbox = new JCheckBox("Works in English", true); frenchChkbox = new JCheckBox("Works in French"); germanChkbox = new JCheckBox("Works in German");
// Note that, unlike the java.awt.Checkbox class, the B in // JCheckBox is capitalized, as it should be choicePanel.add(englishChkbox); choicePanel.add(thisLibRB); choicePanel.add(frenchChkbox); choicePanel.add(allLibRB); choicePanel.add(germanChkbox); // Remember that Components are added to grid cells // Left to Right, Top to Bottom. ChkBoxListener cbListener = new ChkBoxListener(this); // Create a Listener object for ItemEvent notification. // Pass it a reference to the current GUI object. englishChkbox.addItemListener(cbListener); frenchChkbox.addItemListener(cbListener); germanChkbox.addItemListener(cbListener); // JCheckBoxes produce both Item and ActionEvent objects // when they are selected or deselected. It is not // necessary to listen for any event unless you wish to // respond immediately to the JCheckBox changing state. // Otherwise, you could simply wait for something like the // OK button being clicked and then interrogate the state // of the JCheckBox (or java.awt.Checkbox). // If you do listen for events on the JCheckBox // you should listen for an ItemEvent. containingPanel.add(choicePanel, "Center"); theContentPane.add(containingPanel, "Center"); // The basic JMenuBar and JMenu methods are the same as for // the AWT counterparts, so this shows the basics of // an AWT menu. menubar= new JMenuBar();; fileMenu= new JMenu("File"); helpMenu = new JMenu("Help"); titleSearchMI = new JMenuItem("Search by title"); printResultsMI = new JMenuItem("Print Results"); exitMI= new JMenuItem("Exit"); // Add ActionListeners to some JMenuItems // This allows you to detect the fact that a JMenuItem // was selected. MenuProcessor menuProcessor = new MenuProcessor(this); titleSearchMI.addActionListener(menuProcessor); printResultsMI.addActionListener(menuProcessor); exitMI.addActionListener(menuProcessor); // MenuItems and JMenuItems produce ActionEvents // when they are clicked. fileMenu.add(titleSearchMI); // Create components for a program menu and then // build the menus. fileMenu.add(printResultsMI); fileMenu.add(exitMI); aboutAppMI = new JMenuItem("About Library application"); helpIndexMI = new JMenuItem("Help Index"); helpMenu.add(aboutAppMI); helpMenu.add(helpIndexMI); menubar.add(fileMenu); // menubar.setHelpMenu(helpMenu);\ // Do not use this method as of Sept. 1999. // The setHelpMenu method puts the "Help" menu on the // far right. However, while this works for a Menu object
// the setHelpMenu() is not yet implemented for JMenuBar, // so use menubar.add(helpMenu); // of the JMenuBar class. setJMenuBar(menubar); // Enable the Close button on the title bar of the JFrame // so that it does something useful. The default behavior // is probably not what you want. addWindowListener(new Terminator()); setVisible(true); } // The following method exists so that another object, // here a JDIalog, can update the field by calling // this method. public boolean setTitleText(String title) { titleTF.setText(title); titleTF.revalidate(); return true; } } // End of class definition for GUI9.
public class GUIDriver9 { public static void main(String[] args) { GUI9 gui = new GUI9(); } }
class ButtonListener implements ActionListener // You must implement the Event listener interface // for the Event type you are interested in processing. { GUI9 source; public ButtonListener(GUI9 g) { source = g; // Save object reference to GUI9 object. } public void actionPerformed(ActionEvent e) // This is the only method in the ActionListener interface. { String command = e.getActionCommand(); // the getActionCommand method is a special method usable // only with ActionEvents (so it is not a generic // event handling method) that provides the "label" // on the button. If you are using a JButton // which only has an ImageIcon and no text, you will need // a more generic approach like e.getSource(). if(command.equalsIgnoreCase("OK") ) JOptionPane.showMessageDialog (null, "OK Button Clicked", "Subject Search", JOptionPane.INFORMATION_MESSAGE); // This creates a JOptionPane, a pop-up message box // that is informational. You could also create such a
// pop-up with options like yes/no, OK/Cancel and the like. if(command.equalsIgnoreCase("Cancel") ) JOptionPane.showMessageDialog (null, "Cancel Button Clicked", "Subject Search", JOptionPane.INFORMATION_MESSAGE); } // End class definition for ButtonListener.
class ChkBoxListener implements ItemListener { GUI9 delegator; public ChkBoxListener(GUI9 g) { delegator = g; } public void itemStateChanged(ItemEvent e) { Object source = e.getItemSelectable(); // Returns a reference to an object of type Object // that is an object reference for the Component // object the user selected or deselected. if(source == delegator.englishChkbox) JOptionPane.showMessageDialog( null, "englishChkbox selected/deselected", "Subject Search", JOptionPane.INFORMATION_MESSAGE); if(source == delegator.frenchChkbox) JOptionPane.showMessageDialog (null, "frenchChkbox selected/deselected", "Subject Search", JOptionPane.INFORMATION_MESSAGE); if(source == delegator.germanChkbox) JOptionPane.showMessageDialog (null, "germanChkbox selected/deselected", "Subject Search", JOptionPane.INFORMATION_MESSAGE); } } // End class definition for ChkboxListener. class MenuProcessor implements ActionListener { GUI9 delegator; public MenuProcessor(GUI9 g) { delegator = g; } public void actionPerformed (ActionEvent e) { Object source = e.getSource(); if(source == delegator.titleSearchMI) { // If the user clicked the "Title Search" // menu choice, TitleSearchDialog tsDialog = new TitleSearchDialog(delegator, "Title Search Dialog", true); tsDialog.setSize(400,200);
tsDialog.setVisible(true); } if(delegator.printResultsMI == source) { JOptionPane.showMessageDialog (null, "Printing Results", "Print Status", JOptionPane.INFORMATION_MESSAGE); } if(source == delegator.exitMI) System.exit(0); } } // End class definition for MenuProcessor. class TitleSearchDialog extends JDialog { public GUI9 creator; public JTextField titleTextField; public JButton okButton; public JButton cancelButton; public TitleSearchDialog(GUI9 g, String title, boolean modal) { super(g,title, modal); // Call the parent constructor for JDialog (or Dialog). There // is no default constructor, so you need to explicitly // call the JDialog constructor with parameters. // The three parameters are: // A reference to a JFrame object (Frame object for a Dialog). // A title String that appears on the title bar of the dialog. // A boolean flag that states whether the dialog should be // modal (true) or modeless (false). creator = g; // Save GUI9 object reference. JPanel titlePanel = new JPanel(); JPanel buttonPanel = new JPanel(); titleTextField = new JTextField("", 50); JLabel titleLabel = new JLabel("Enter a title to search for"); titlePanel.add(titleTextField); getContentPane().add(titleLabel, "North"); // JDialogs, like JFrames use BorderLayout. okButton = new JButton(" OK "); // Pad the text with blanks to make the JButton wider. cancelButton = new JButton("Cancel"); TitleDialogButtonHandler tdbHandler = new TitleDialogButtonHandler(this); // Create a listener object with a reference to the // current TitleSearchDialog object. okButton.addActionListener(tdbHandler); cancelButton.addActionListener(tdbHandler); buttonPanel.add(okButton); buttonPanel.add(cancelButton); getContentPane().add(titlePanel, "Center"); getContentPane().add(buttonPanel, "South"); } } class TitleDialogButtonHandler implements ActionListener {
TitleSearchDialog delegator; public TitleDialogButtonHandler(TitleSearchDialog tsd) { delegator = tsd; // Save a reference to the dialog object // so the event handler can query the dialog's components. } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); // The getSource() method is a generic way to find out the // Component that is the source of the event, and always // works, whereas getActionCommand will not work if // a JButton has only an ImageIcon and setActionCommand() // was not executed for the JButton. if(source == delegator.okButton) { boolean result = delegator. creator.setTitleText (delegator.titleTextField.getText()); // Get the text from the titleTextField, and then // use that String value to call the setTitleText() method // in the GUI9 class. This is an easy way to communicate // back to the GUI object no matter how // far it is removed from this event handler // because the reference kept getting passed on. delegator.dispose(); // Shut down the dialog. // Do NOT just call setVisible(false) because this dialog is // modal and your code will still be blocked // until the dialog is dismissed. } if(source == delegator.cancelButton) delegator.dispose(); } } class Terminator extends WindowAdapter { // Inherit all the empty WindowListener methods from // the WindowAdapter convenience class, then override // the method of interest to you. you need to be concerned with // the WindowListener methods, especially windowClosing() // in a GUI application (but NOT an Applet): // windowClosing(). The method windowClosed() is // not what you want for the Close // button on the JFrame's or (Frame's) title bar. public void windowClosing(WindowEvent e) { // You won't be back. System.exit(0); } } // End class definition of Terminator. Figure 8.11 shows an example of using Delegation event handling. Just as with Accessibility, you will have to execute the program to see it in action.
Figure 8.11 An example of using Delegation event handling (GUIDriver9.java).
This program illustrates several important points. First, it uses separate classes to handle events. This maximizes the potential for reusability and creates ease of maintenance while supporting good object-oriented design philosophy. The two most common kinds of events you will "listen" for in most business-oriented application GUIs are ActionEvents and WindowEvents. The program also listens for ItemEvents, just to show you how to do it. Tip As noted in the program, you only need to listen for ItemEvents on Lists, JLists, JComboBoxes, JCheckBoxes, and so forth, if you intend to do some operation at the moment someone selects or deselects one of these. If all you want to do is check the status of the affected component when the user clicks the OK button or the like, you can ignore ItemEvents and simply query the state of the component. This simplifies your code.
You should listen for the main kind of event produced by the component. In the case of JMenuItems and JButtons (such as Button and MenuItem), this is an ActionEvent. To deal with button clicks or menu selections in this program, it creates a component such as a JButton and instantiates an instance of a class that implements the ActionListener interface. Then the GUI9 object registers the fact that it is delegating the responsibility for handling ActionEvents generated by the JButton to the ButtonHandler instance. The ButtonHandler class shows an important technique for being able to track back to the delegating GUI. When the ButtonHandler object is instantiated, a reference to the GUI class is passed to the ButtonHandler object and saved. That reference can then be used later to interrogate or update components in the GUI or to execute methods of the GUI class. It is also valuable because if the listener object needs to display a dialog in response to a user request, an instance of Frame or JFrame is needed for the dialog's constructor, and the best way to do that is to provide the GUI's object reference. Also important for an application (as opposed to an Applet) is using a WindowListener. There are several WindowListener methods, but the only one that you usually need to be concerned about is windowClosing(WindowEvent e). This is the event created when a user clicks the Close button on the title bar of the Frame or JFrame window. All the new classes in this example are simply event listeners, except for a JDialog subclass that illustrates two things. It shows how to handle events in another window from the main GUI (the same way as in the main GUI) and how to call back to methods in the main GUI class using an object reference from it. This program shows the general approach to event handling in a Java program. Except for the WindowListener, all the event listeners can be used equally in an Applet. Note Although listening for events using an observer-observable combination can be done anywhere, the event listeners and events
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=71 (9 of 10) [6/2/2002 10:02:08 PM]
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 8. GUI Programming I: Applications > Using Drag and Drop in Java < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191090086103247069052240
The DragSourceThis is a component. Although the example here uses Swing components, you can use any subclass of java.awt.Component. DropTargetThis can be either a Java or non-Java target. For simplicity, the example here uses a Java DropTarget. DataFlavorThe DataFlavor is the type of thing being dragged and dropped. Currently there is only explicit Java 2 support for the text DataFlavor, although you can write code to drag and drop your own DataFlavor. For a drag-and-drop operation within the same JVM, the component being dragged is a Transferable. When the drag and drop is between two JVMs or between Java and a native program, the DataFlavor is usually provided with a java.io.InputStream.
Caution The java.awt.dnd package contains lots of classes with lots of methods with very similar names. When you select a class or method name to use, be careful to get the right one and spell it correctly.
Note To provide a DragSource object for your application, you can either obtain one DragSource per JVM through DragSource.getDefaultDragSource or implement your own DragSource object to provide for every instance of Component objects that you want to drag and drop.
The DragGestureRecognizer shields you from platform dependencies caused by differences in mouse button usage for drag and drop. The actions related to dragging and dropping are called DragGestures, which are processed by a DragGestureRecognizer. There are several types of drag actions:
ACTION_NONENo action is taken. ACTION_COPYThe DragSource copies only the DragSource. ACTION_MOVEThe DragSource deletes the data when the drop operation completes successfully. ACTION_COPY or ACTION_MOVEThe DragSource performs either a Copy or Move, depending on the action requested by the DropTarget. ACTION_LINK or ACTION_REFERENCEA data change to either the DragSource or DropTarget causes the same change to the other side of the drag-and-drop operation.
Listing 8.10 shows an example of a drag-and-drop program. It processes drag and drop by moving the contents of the drag area to the drop area. To implement this, the program does the following:
q q q q
Subclasses JTextArea to make a DragSource Subclasses JTextArea to make a DropTarget Creates a DragGestureRecognizer and a DragSourceListener Creates a DropTargetListener
To use the sample program, you type into the drag area on the top half of the screen. Then place your mouse pointer on the text you typed. Drag the mouse into the drop area on the bottom half of the screen. When you do the drag, the DragGestureRecognizer examines your action, such as ACTION_COPY, and determines if it is a valid action. If so, you can proceed with the drag. The DragSourceListener observes the drag to see if it is valid. When you get to the DropTarget area, the DropTargetListener implementation tracks the drop, including highlighting the drop area for you (when you do a valid drag and drop). The text in the drag source will disappear and reappear in the drop target area. This is shown in Listing 8.10.
Listing 8.10 Using Drag and Drop with Text (GUIDriver10.java)
class GUI10 extends JFrame { DragTextArea dragArea; DropTextArea dropArea; JLabel dragLabel; JLabel dropLabel; JPanel dragPanel; JPanel dropPanel; Container theContentPane; public GUI10() { setSize(600,440); setBackground(Color.blue); theContentPane = getContentPane(); theContentPane.setLayout(new GridLayout(2,1)); // Set the size, background, and layout of the GUI. Also get // a reference to the ContentPane.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=72 (2 of 14) [6/2/2002 10:02:19 PM]
dragLabel = new JLabel("Drag Source", SwingConstants.CENTER); dropLabel = new JLabel("Drop Target", SwingConstants.CENTER); dragArea = new DragTextArea(10,60); dropArea = new DropTextArea(10,60); // Currently, drag and drop works best with text. So, create two // JTextAreas, one for the source of the drag, and another for // the target of the drop operation. dragArea.setLineWrap(true); dropArea.setLineWrap(true); dragPanel = new JPanel(new BorderLayout()); dropPanel = new JPanel(new BorderLayout()); dragPanel.add(dragLabel, "North"); dragPanel.add(dragArea, "Center"); dropPanel.add(dropLabel, "North"); dropPanel.add(dropArea, "Center"); // Create two JTextArea components to drag and drop between. theContentPane.add(dragPanel); theContentPane.add(dropPanel); // Finish setting up the GUI and make it visible. setVisible(true); } } // End of class definition for GUI10.
public class GUIDriver10 { public static void main(String[] args) { GUI10 gui = new GUI10(); } } // Special JTexArea subclasses are defined here to // user for drag source and drop target text areas. class DragTextArea extends JTextArea { // This class provides a DragSource. You can type text into this // JTextArea and then drag the text to a DropTarget. DragSource dragSource; DragGestListener dragGestListener; DragSrcListener dragSrcListener; // You need a listener: one for the drag gesture with the mouse // and the second for drag actions from a specific source. public int dragAction = DnDConstants.ACTION_COPY_OR_MOVE; public DragTextArea(int r, int h) { super(r,h); // Call the JTextArea constructor, passing rows and columns. setOpaque(true); dragSource = DragSource.getDefaultDragSource(); // Get a drag source object reference. dragGestListener = new DragGestListener(this); // Instantiate DragGestureListener. dragSrcListener = new DragSrcListener(this); // Listen for dragging actions. dragSource.createDefaultDragGestureRecognizer( this, this.dragAction, this.dragGestListener);
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=72 (3 of 14) [6/2/2002 10:02:19 PM]
// Create a DragGestureRecognizer. This object determines // that the mouse is in fact doing a drag, not some other // action. } } // End class definition for DragTextArea.
class DragGestListener implements DragGestureListener { // This class starts the drag operation if the action is //
graphics/ccc.gif available.
// It uses java.awt.datatransfer.StringSelection to transfer // the data from one JTextArea to another. DragTextArea textarea; public DragGestListener(DragTextArea dta) { textarea = dta; // Create a drag gesture listener for the drag // source text area. } public void dragGestureRecognized(DragGestureEvent e) { // This is similar to an actionPerformed method for // an ActionEvent. if((e.getDragAction() & textarea.dragAction) == 0) { System.out.println("Invalid drag action" + textarea.dragAction); return; } // Get the DragTextArea's text and put it inside // a Transferable object. Transferable transferable = new TransferableString (textarea.getText() ); // You need an object that implements Transferable // to drag it. try { e.startDrag(DragSource.DefaultCopyNoDrop, transferable, textarea.dragSrcListener); // Begin the drag, specifying the initial cursor, // the Transferable object, and // the drag source listener. } catch( InvalidDnDOperationException idoe ) { // If the drag failed, show the reason. System.err.println( idoe.getMessage() ); } } } class DragSrcListener implements DragSourceListener
{ // A listener to track the DND operation. DragTextArea textarea; public DragSrcListener(DragTextArea dta) { textarea = dta; // Save a reference to the drag source // text area. } public void dragDropEnd(DragSourceDropEvent e) { // This is called when the drag gesture is completed. if( e.getDropSuccess() == false ) { // If the drag and drop failed, print an error message // to the console. System.out.println( "Drop failed"); return; } // Check the action selected by the drop target. if(e.getDropAction() == DnDConstants.ACTION_MOVE) // As of July 1999, Move, but not Copy, seemed to be the only // workable choice. The Drag and Drop APIs have a ways to go // to work well and be complete. textarea.setText(""); // Clear the text area. } public void dragEnter(DragSourceDragEvent e) { // This is called when the drag starts. The class and // DragSourceEvent object are somewhat analogous to classes // that implement MouseListener and MouseMotionListener. DragSourceContext context = e.getDragSourceContext(); int dropAction = e.getDropAction(); if( (dropAction & textarea.dragAction) != 0) { context.setCursor (DragSource.DefaultCopyDrop); // When a drag starts, change the pointer shape. } else { context.setCursor(DragSource.DefaultCopyNoDrop); } } public void dragOver(DragSourceDragEvent e) { // This is called when you drag a Transferable over // something else . DragSourceContext context = e.getDragSourceContext(); int userAction = e.getUserAction(); System.out.println("dragOver user actions " + userAction); }
public void dropActionChanged (DragSourceDragEvent e) { DragSourceContext context = e.getDragSourceContext(); context.setCursor(DragSource.DefaultCopyNoDrop); } } class DropTextArea extends JTextArea { // This class subclasses JTextArea and is the target // for the drop operation. DropTgtListener dropTgtListener; int acceptableActions; public Color borderColor; DropTarget dropTarget; public DropTextArea(int r, int c) { super(r,c); borderColor=Color.green; // After calling the JTextArea's constructor, set the border // color of an object being dropped on this drop target for // a visual cue for dropping. dropTgtListener = new DropTgtListener(this); // Create a DropTargetListener. acceptableActions = DnDConstants.ACTION_COPY_OR_MOVE; dropTarget = new DropTarget(this, acceptableActions, dropTgtListener, true); // Specify the component that is the DropTarget, // the acceptableActions, the DropTargetListener instance // and a boolean value to show if the DropTarget is // accepting drops. } private void showBorder(boolean borderFlag) { if(borderFlag) { setBorder( BorderFactory.createLineBorder (this.borderColor, 10) ); // Display the border on the Transferable // being dropped. } else { setBorder( BorderFactory.createEmptyBorder() ); } getParent().validate(); repaint(); } } // End of class definition for DropTextArea.
class DropTgtListener implements DropTargetListener { private Color borderColor; DropTextArea textArea; public DropTgtListener(DropTextArea dta) { textArea = dta; // Save a reference to the drop target text area. } private boolean isDragFlavorSupported(DropTargetDragEvent e) { // Determine if drag DataFlavor is acceptable. // The flavor refers to the type of data. // For example, a text flavor is supported while // a CORBA object or StarOffice spreadsheet might not be. // You can create your own flavor and write code to support it. boolean ok=false; if (e.isDataFlavorSupported(TransferableString.plainTextFlavor)) { ok=true; } else if (e.isDataFlavorSupported( TransferableString.localStringFlavor)) { ok=true; } else if (e.isDataFlavorSupported(DataFlavor.stringFlavor)) { ok=true; } else if (e.isDataFlavorSupported (DataFlavor.plainTextFlavor)) { ok=true; } return ok; } private DataFlavor chooseDropFlavor(DropTargetDropEvent e) { // This method sets the flavor. if (e.isLocalTransfer() == true && e.isDataFlavorSupported(TransferableString.localStringFlavor)) { return TransferableString.localStringFlavor; } DataFlavor theFlavor = null; if (e.isDataFlavorSupported(TransferableString.plainTextFlavor)) { theFlavor = TransferableString.plainTextFlavor; } else if (e.isDataFlavorSupported( TransferableString.localStringFlavor))
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=72 (7 of 14) [6/2/2002 10:02:19 PM]
{ theFlavor = TransferableString.localStringFlavor; // Local String flavor is supported. } else if (e.isDataFlavorSupported(DataFlavor.stringFlavor)) { theFlavor = DataFlavor.stringFlavor; } else if (e.isDataFlavorSupported (DataFlavor.plainTextFlavor)) // If plain text flavor is valid. { theFlavor = DataFlavor.plainTextFlavor; } return theFlavor; }
private boolean isDragOk(DropTargetDragEvent e) { // This method is used by dragEnter() and dragOver(). if(isDragFlavorSupported(e) == false) { // If the flavor is invalid, the drop fails. System.out.println( " Problem in isDragOk: no flavors chosen" ); return false; } // The API doc on DropTargetDragEvent rejectDrag says that // the dropAction should be examined. int dropAction = e.getDropAction(); System.out.print("dt drop action " + dropAction); boolean result = true; if ((dropAction & textArea.acceptableActions) == 0) result = false; // Test to see if the Drop action is acceptable. // Not all components can accept a drop. return result; } public void dragEnter(DropTargetDragEvent e) { // Feedback from acceptDrop or rejectDrop. if(isDragOk(e) == false) { System.out.println( "No entering to drop allowed"); textArea.borderColor=Color.red; showBorder(true); e.rejectDrag(); return; } textArea.borderColor=Color.green; showBorder(true); e.acceptDrag(e.getDropAction()); } private void showBorder(boolean borderFlag)
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=72 (8 of 14) [6/2/2002 10:02:19 PM]
{ // This method shows a border on a Transferable // inside the drop target. if(borderFlag) { textArea.setBorder( BorderFactory.createLineBorder (this.borderColor, 10) ); } else { textArea.setBorder( BorderFactory.createEmptyBorder() ); } textArea.getParent().validate(); textArea.repaint(); } public void dragOver(DropTargetDragEvent e) { // This method is invoked when you drag over the drop target. // Provide feedback from dragOK() for dragOver(). if(isDragOk(e) == false) { System.out.println( "DropTarget dragOver is not ok" ); textArea.borderColor=Color.red; // Provide a visual cue that the drop is not acceptable. showBorder(true); e.rejectDrag(); return; } e.acceptDrag(e.getDropAction()); } public void dropActionChanged(DropTargetDragEvent e) { if(isDragOk(e) == false) { System.out.println ( "DropTgtListener dropActionChanged is not allowed" ); e.rejectDrag(); } else e.acceptDrag(e.getDropAction()); } public void dragExit(DropTargetEvent e) { textArea.borderColor=Color.green; showBorder(false); } public void drop(DropTargetDropEvent e) { // This method handles the drop action. Object content =null; // Perform the action from the getSourceAction() method on // the Transferable. // Call acceptDrop or rejectDrop // For the same JVM as the DragSource, // use TransferableString.localStringFlavor.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=72 (9 of 14) [6/2/2002 10:02:19 PM]
// // // // //
When you find a match for the flavor, validate the operation on the DropTarget. Get the Transferable object's data according to the chosen flavor. Transfer the data.
DataFlavor theFlavor = chooseDropFlavor(e); // Validate the data flavor. if (theFlavor == null) { System.err.println( "No valid DataFlavor found" ); e.rejectDrop(); showBorder(false); return; } try { // Validate the drop action. e.acceptDrop(textArea.acceptableActions); // Accept drop Action. COPY_OR_MOVE results in MOVE. content = e.getTransferable().getTransferData(theFlavor); if (content == null) throw new NullPointerException(); // Throw an exception if the Transferable is empty. } catch (Exception ex ) { System.err.println( "No transfer data obtained:" + ex.getMessage()); ex.printStackTrace(); e.dropComplete(false); showBorder(false); return; } if (content instanceof String ) { // If the Transferable is a String, use it to update the // drop target text area. As this indicates, much about // drag and drop has to be done by the developer. String s = (String) content; textArea.setText(s); } else // If not a local String, use an InputStream to transfer data. if (content instanceof InputStream) { InputStream is = (InputStream)content; InputStreamReader isr = null; try { isr=new InputStreamReader(is,"Unicode"); } catch(UnsupportedEncodingException uee) { isr=new InputStreamReader(is); }
StringBuffer str = new StringBuffer(); int i = -1; try { while((i = isr.read()) >= 0 ) { if (i != 0) str.append((char)i); } textArea.setText(str.toString()); } catch(IOException ioe) { // As of July 1999, there is a bug when attempting to // drag to or from MS Word 97 so this won't work for that. System.err.println( "cannot read" + ioe); e.dropComplete(false); showBorder(false); JOptionPane.showMessageDialog(textArea, ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); return; } // End of catch block. } // End of if block. else { e.dropComplete(false); showBorder(false); return; } // End of else block. e.dropComplete(true); showBorder(false); // There was a problem trying to transfer the data. } // End of drop() method. } // End of class definition for DropTgtListener. The TransferableString class used in this example is shown in Listing 8.11.
Listing 8.11 The TransferableString Class Used for a Transferable in Drag and Drop from Listing 8.10 (TransferableString.java)
public class TransferableString implements Transferable, ClipboardOwner { // Implement the Transferable interface // to move dragged data. public static final DataFlavor plainTextFlavor = DataFlavor.plainTextFlavor; public static final DataFlavor localStringFlavor = DataFlavor.stringFlavor;
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=72 (11 of 14) [6/2/2002 10:02:20 PM]
public static final DataFlavor[] flavorArray = { TransferableString.plainTextFlavor, TransferableString.localStringFlavor} ; private String content; private static final List flavorList = Arrays.asList( flavorArray ); public TransferableString(String s) { content = s; } public synchronized DataFlavor[] getTransferDataFlavors() { return flavorArray; } public boolean isDataFlavorSupported( DataFlavor flavor ) { return ( flavorList.contains( flavor ) ); } public synchronized Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (flavor.equals(TransferableString.plainTextFlavor)) { return new ByteArrayInputStream(content.getBytes ("Unicode")); } else if (TransferableString.localStringFlavor.equals(flavor)) { return content; } else { throw new UnsupportedFlavorException (flavor); } } public void lostOwnership(Clipboard clipboard, Transferable contents) { System.out.println ("TransferableString lost ownership of " + clipboard.getName()); System.out.println ("Text: " + contents); } } // End class definition for TransferableString As you can see, simply copying a String from a JTextArea to another JTextArea is not a trivial task. There is code in this example for reading the dragged data through an InputStream. This is used in the case of a drag from a non-Java source. This functionality still had some bugs as of July 1999. When everything is working correctly, you will be able to drag and drop to and from Java and non-Java applications. Figure 8.12 shows the screen before a drag-and-drop operation, and Figure 8.13 shows the screen after a drag-and-drop operation.
Figure 8.12 A screen before drag and drop.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 9. GUI Programming II: Applets > An Applet Overview < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191090086102204105048074
An Applet Overview
An applet is a special type of Java GUI program that is intended to be run inside a Web browser. Unlike the programs shown in Chapter 8, "GUI Programming I: Applications," an applet cannot run by itself but must be hosted by another program. Note You can code an applet class that can run in a standalone fashion as an application and as an applet hosted by a browser. This is unusual but can easily be accomplished. To do this, you code all the applet methods that are appropriate. Then you code a main method that one way or another instantiates a Frame, instantiates the applet subclass, adds the applet object to the Frame, and then calls the applet methods as appropriate. Because the applet is running as a standalone application, it must have a WindowListener object that deals with the windowClosing() method and also has a call to System.exit().
The essential reason for this is that an applet is a subclass of the Panel class. As explained in the last chapter, a standalone Java GUI application must use a Frame as the outermost window. Because you put a Panel in a Frame, and normally not vice versa, an applet does not have a Frame. Note Technically speaking, an applet does have a Frame . When the Web browser detects an applet tag, it starts a Java program in the browser that instantiates a Frame. Then the applet class, after being downloaded, is instantiated and added to the Frame just as any AWT component would be. It is this Java program, which the browser runs, that has the main method and the Frame. Although it is possible to get to the Frame object through successive calls to getParent(), in general this approach of using the invisible Frame is a hack and is not recommended. The Frame object created by the browser is essentially invisible.
Because applets do not have their own Frame, there are some limitations to what an applet can do. First, it is generally difficult to add menus to applets. In an application, you can add menus to a Menubar, which is added to a Frame. Menubars cannot be added to Panels, so they cannot be added to applets easily. Caution It is possible to put a menu in an applet . Although is difficult to do, it is not nearly as difficult as returning user interaction with the menu to the applet. In general, you should avoid using a menu and find some other method for getting user choices.
Also, because of the lack of an immediate Frame object, applets do not normally have Dialogs . Dialogs, as shown in the preceding chapter, require an instance of a Frame for a constructor parameter. There are, again, ways to work around this, but none of them are very elegant. You can, for example, do this:
This statement creates a new Frame object on-the-fly to be used as the basis of the Dialog. Again, getting event notification back to the applet is a bit tricky, so this is usually avoided.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 9. GUI Programming II: Applets > Applet Security Considerations < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191090086101100143057089
Read from or write to the client machine's file system. Modify thread groups. Connect to any host besides the host they were downloaded from. Access any system properties related to the local machine's file system.
Because of these restrictions, an applet cannot execute code installed on a local machine, and therefore, an applet cannot print from the machine it is running on. Tip In Java 2, this particular problem (and, indeed, any given set of applet restrictions) can be overcome through the use of a policy file . In order to allow an applet to print, the client machine needs a policy file which specifies that the applet has the RuntimePermission for PrintJob.
Prior to Java 2, you could get around these limitations of the sandbox by signing your applet with a digital signature. In JDK 1.1, if a browser such as Netscape accepted signed applets, these signed applets could do whatever they wanted if the user let them execute. A signed applet in a JDK 1.1compatible browser has carte blanche to do anything an application program can do. All a signature did was tell the user the probable source of the applet. The user had to decide whether to trust the applet source not do anything malicious. In Java 2, this has all changed. Now, signing an applet is not sufficient. It is now also necessary to have a policy file to specifically grant signed code permission for the activities you want an applet to perform. Note that you can grant all applets certain permissions with a policy file even if they are not signed, but you would probably not want to do this.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 9. GUI Programming II: Applets > Main Applet Methods < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191090086101096122077021
The init() method is the place to put code that sets up the applet screen, such as adding Buttons or text fields or creating Image objects within limits. It is also the place to start the downloading of images through a separate thread using the MediaTracker class discussed later. Think of init() as analogous to a constructor. Applets do not have explicit constructors. The start() method is where you initiate what must happen when a user goes to your applet's Web page. Here is a sample scenario. Five Web pages are up, one of which has your applet on it. At first, the user is looking at another Web page. When he moves to your applet's Web page, the JVM calls the start() method of your applet. Now you begin animation or start playing soundany task that should happen only while your applet is being looked at by the user. The paint() method is used to paint the applet on the screen. Just as all Component subclasses have a paint() method, an applet has a paint() method. This method draws text, shapes, or images on the applet's surface. In fact, if your applet includes an image, the paint() method is called by the JVM whenever there are more pixels to add to the image as it is being downloaded. These cause the image to go from nonexistent to a clear picture. Your applet must override and implement at least one of these three methods: init(), start(), or paint(). It must have at least one of these, but it can have all three. If it does not, you will not be able to put anything on the applet's screen or have it perform any work. The stop() method is called by the JVM when the user switches from the Web page with the applet to another Web page. It is important to use the stop() method to halt activity that should not go on while the applet is not visible. This includes animation or the playing of sound filesanything that uses the CPU or that would interfere with the user. The destroy() method is called when the applet's Web page is closed. It is used to free resources held by the applet. Because applets usually do not hold large resources or the kind
of resources that must be closed (such as a file), there's generally no need to code this method.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 9. GUI Programming II: Applets > Running Applets < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191090086101108252118057
Running Applets
Applets run in Web browsers and are loaded through HTML applet tags. The most important tags are covered here. Listing 9.1 shows sample HTML for running an applet.
Listing 9.1 Sample HTML to Run an Applet (MyApplet.html)
<HTML> <Title> This is a sample Applet HTML file </Title> <BODY> <APPLET code=MyApplet archive=Myjar.jar width=400 height=300 codebase=http://www.gradschoollibrary.edu/Applets alternatetxt="Get a Java-enabled browser" vgap =5 hgap=5 <param name=imagefile > value=http://www.gradschoollibrary/Applets/images/libraryentrace.gif> </APPLET> </BODY> </HTML> This book does not cover HTML, so the non-applet tags will not be discussed. In the order in which they appear, here is what each tag does. The <APPLET> tag tells the browser that what follows is an applet that the browser must get from some disk, either locally or, more probably, remotely from a Web server. The code keyword specifies the name of the class to load first for the applet. This is generally the name of the applet class you want to use, but it can be any class, as long as that class causes the applet subclass to be loaded. The archive keyword tells the browser which (if any) .jar file(s) to download to find the classes and other resources needed by the applet. Tip Although using a .jar file is not necessary, it is highly recommended. A .jar file, by default, uses compression. It reduces the size of the downloaded code transmitted over the network, making downloading faster. In addition, .jar files reduce the overhead needed to make network connections for every individual file. If an applet has 10 classes, and there is no .jar file for the applet, the browser must make 10 separate connections (including a handshake) with the Web server's system. It must then request the download and then disconnect. There are multiple network trips required to do
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=77 (1 of 4) [6/2/2002 10:02:37 PM]
just the handshake. By using a .jar file, these trips are cut down to only one connect-download-disconnect cycle.
Note Generally speaking, a browser must be at least JDK 1.1compatible in order to use .jar files.
It is also possible to list multiple comma-delimited .jar files. The height and width specify the size in pixels of the applet. Unlike a GUI application, an applet generally cannot specify its size internally through a setSize() call. Instead, this must be set in the HTML tags. You can specify either a precise pixel size or a percentage of the screen real estate. The Applet class does have a resize() method, but it is not supported on most browsers. Caution Be very careful when sizing an applet. Because it cannot dynamically be resized, as an application can, you have to live with the size you choose. Do not assume that all users have the high screen resolutions that you might have on a 21-inch monitor for a Sparc system. It is more likely that end users on the Internet have 15-inch monitors with maybe 800600 resolution. If you specify a width of 900, you might end up with an OK button off the right side of the screen that a user has no way to reach.
The codebase refers to the directory (usually on a Web server) where the applet code resides. It is generally in a directory or a subdirectory of the directory that contains the source of the Web page, which is called the Documentbase. The alternatetext keyword is used to specify text that will appear on the screen if the applet can't run for any reason. Failure might occur because the browser doesn't support Java or for some other reason, excluding an exception. You might put a message here such as "If you had a Java-enabled browser, you'd see a great applet here." Or, you might provide a link to a Web site where you can download a Java-enabled browser. It's up to you what to say with this tag. Remember that it shows up only if your applet cannot run because the browser won't support it. You can specify how many pixels should separate the applet from what is around it by using the hgap and vgap options. The hgap value specifies horizontal separation, whereas vgap specifies the vertical gap between other Web page content and the applet. The final important option is to specify a parameter. The applet uses the getParameter() method to obtain the value specified in the parameter name. In the sample HTML, the applet uses a getParameter("imagefile") in order to get the name of the image file needed to display an image. Although the parameter name, imagefile, is not case-sensitive in your Java code, the value is case-sensitive. So you can code
getParameter("iMaGeFile");
and it would work. On the other hand, correctly specifying a case-sensitive value is critical for a filename. Systems like UNIX, which are case-sensitive, will not find the URL otherwise.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=77 (2 of 4) [6/2/2002 10:02:37 PM]
Using the AppletViewer Java 2 provides a simple tool for testing your Applet, called AppletViewer. The AppletViewer tool can be quite handy for testing, especially because it has menu options to call, on request, each main applet method, such as init, start, or stop. At the same time, remember that the AppletViewer is not a browser. It cannot be used for viewing Web pages. It only executes the applet on a Web page and does not reflect browser-specific implementation choices that might affect your code. It is just a quick unit-testing tool that is no substitute for doing full testing. To invoke the AppletViewer, you need an HTML file that includes necessary applet tags. You call it in this way:
The Java Plug-In It's no secret that there are many incompatibilities among browsers, and different versions of the same browsers, such as Netscape, support different versions of Java. Other browsers, such as Internet Explorer, don't fully support Sun's definition of Java, omitting important core packages, such as java.rmi. To address these potential incompatibilities and to respond to the apparent slowness of vendors in making their browsers JVMcompliant with the latest Java version, Sun has introduced the Java plug-in. In essence, this plug-in replaces the JVM of the browser. This allows you to code a Java 2based applet and run it on any client machine that has the Java plug-in for Java 2 installed, regardless of the browser version on the client machine. As of this writing (July 1999), it appears that Netscape will never support Java 2 in its browser's JVM, and Internet Explorer 5.0 supports only a partial version of JDK 1.1. Therefore, the only way to use Java 2specific features in an applet is to use the Java plug-in for Java 2. To use the Java plug-in, you need to change your applet tags. These tags tell the browser whether to use its own JVM or the plug-in's JVM. You are not required to use the plug-in at all times. You use it only when you go to a Web page that has the proper tags. The Java plug-in has a utility included with it that can change your applet tags appropriately. This is a simple way to get Java 2 features in your applet right now, instead of waiting and hoping that browser vendors, at some time in the future, will make their browsers Java 2compliant. Note One of the best things about using the Java plug-in is that it provides Swing in the Java 2 version. It is not practical to use Swing in an applet, unless it is already resident on the client machine. Swing classes are more than 1MB in size, which would be a prohibitively large applet download.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 9. GUI Programming II: Applets > Applet Programming < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229191090086100144071173199
Applet Programming
Applets, as Panels, conform to what was discussed about GUI programming in Chapter 8. This means you can add components to an applet, set the layout manager, add event listeners, and process user interaction. Yet, there are some things that applets are particularly good at, such as displaying images. The remainder of the chapter is devoted to showing how to code applets. A Simple Applet: Code Example Listing 9.2 illustrates code to show a minimal applet with some GUI components on it. Figure 9.1 shows the applet running in AppletViewer.
Figure 9.1 A simple applet running in the AppletViewer .
<HTML> <TITLE> Basic Applet Example for Pure Java </TITLE> <BODY> <Applet code=BookSearchApplet.class height=100 width=200> </Applet> </BODY> </HTML> Listing 9.3 shows the code for this basic applet.
Listing 9.3 A Simple Applet Using the Applet Class (BookSearchApplet.java)
import java.Applet.*; // You need to import the Applet package for an Applet. import java.awt.*; // Use the AWT component classes.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=78 (1 of 20) [6/2/2002 10:02:53 PM]
public class BookSearchApplet extends Applet // All Applets must subclass the // java.Applet.Applet class. { // Declare instance variables. Button findButton; Button clearButton; Label authorLabel; TextField authorNameTF; public void init() { // Override the init method to instantiate the GUI elements. // init() is the first method you can code for an applet that is // called by the browser. findButton = new Button("Find");; clearButton = new Button("Clear"); authorLabel = new Label("Author Name"); authorNameTF = new TextField("", 30); // Instantiate GUI components, including a Button, Label, // and TextField. Applets use the same GUI components // you would put into any Java GUI. setLayout(new BorderLayout()); // Override the default layout manager of the // Applet, which is FlowLayout, // just like a Panel. Panel tempP1 = new Panel(); // An applet is a Container and can hold // other Containers, as well as components. Panel tempP2 = new Panel (new GridLayout(2,1)); // Create two Panels that will be used just for // layout and do not need to be referenced // elsewhere. tempP1.add(authorLabel); tempP1.add(authorNameTF); tempP2.add(findButton); tempP2.add(clearButton); add(tempP1, "Center"); add(tempP2, "East"); // Add components to the Panels. } } // End class definition BookSearchApplet.
This applet can be executed with either a browser or the AppletViewer. Because it uses no Java 2specific features, it can run in most browser versions, such as the Icesoft browser, or Hotjava. This simple applet has only an init() method because there's nothing for a paint or start method to do, nor anything to stop, nor any resources to release in the destroy() method. Again, the init() method is used, in this case, like a constructor. Parameters in an Applet: Code Example Listing 9.4 shows some sample HTML for an applet that uses parameters. The applet in Listing 9.5 illustrates how to read an applet parameter from the applet tag. Caution Applet parameters are always read as Java Strings. Therefore, to give a number to an applet , you need to convert it
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=78 (2 of 20) [6/2/2002 10:02:54 PM]
You can put anything you want into a parameter. The main advantage of using a parameter through the PARAM option is to specify information to the applet. This eliminates the need to recompile the applet and create a new .jar file for a small change, such as a new image of the week. It's much easier to modify the HTML source than to modify, recompile, and put the new applet version in a new .jar file. Figure 9.2 shows an applet that uses parameters running in the AppletViewer.
Figure 9.2 An applet that uses parameters to set values .
Listing 9.4 Sample HTML for an Applet That Uses Parameters (newBook.htm)
<HTML> <TITLE> Basic Applet with Parameters Example for Pure Java </TITLE> <BODY> <Applet code=NewBookApplet.class height=200 width=350> <PARAM NAME=Title Value="Dead Sea Scrolls in Recent Research"> </Applet> </BODY> </HTML>
Listing 9.5 An Applet That Uses Parameters (NewBookApplet.java)
import java.Applet.*; // You need to import the Applet package for an Applet. import java.awt.*; public class NewBookApplet extends Applet // All Applets must subclass the // java.Applet.Applet class. { Button nextButton; Label newTitleLabel; String newTitleString;
public void init() // init() is the first user-defined method called in the applet. { // Override the init method to instantiate the GUI elements. nextButton = new Button("Next");; newTitleLabel = new Label("The most recent book added to the " + "Library collection is"); // Instantiate GUI components setLayout(new BorderLayout()); // Override the default layout manager of the Applet // which is FlowLayout, just like a Panel. Panel tempP1 = new Panel(new GridLayout(1,5)); // Here's a trick. To make the nextButton appear // smaller than a whole region on the BorderLayout, // surround it with empty Panels. This will make // the GridLayout create multiple, equal-sized // cells. // Add as many as needed to get the desired effect. tempP1.add(new Panel()); tempP1.add(new Panel()); tempP1.add(nextButton); tempP1.add(new Panel()); tempP1.add(new
Panel()); // The above code sets up a set of Buttons sized and spaced // based on adding empty Panel objects to the same container. add(tempP1, "South"); // Put the nextButton in the bottom of the Applet. add(newTitleLabel, "North"); newTitleString = getParameter("Title"); // Get the Parameter String and store it. // All applet parameters are Strings. repaint(); // Make sure the Applet is repainted // before it is displayed. // Always call repaint(), never paint(). } public void paint(Graphics g) { // All drawing is done in the paint() // method of a Component. g.drawString(newTitleString, 50,100); // The drawString method of the java.awt.Graphics // class draws the named String at the given // x and y coordinates every time the paint() // method is called. } // End class definition NewBookApplet
This applet reads a parameter, saves its value, and then displays the parameter's value by painting it on the screen using the drawString() method. The java.awt.Graphics class has several methods for drawing Strings and shapes, such as drawRect(). It also has methods for changing the color for drawing. Every time the applet is repainted, the String is drawn on its surface again. To paint on a specific component within the applet requires overriding paint specifically for that component. Tip
It is very important to call repaint() and not paint(). There are two reasons. The first is that you want the JVM to give you a Graphics object for the paint() method. The second (and much more important) reason is that the JVM can coalesce multiple repaint() calls into one paint() call. It cannot do this with direct calls to paint(). Therefore, calling repaint() will probably make your program perform much better than calling paint() explicitly.
Sound in an Applet: Code Example It is also possible, and really quite easy, to play sound files in a Java program. Note Prior to Java 2, it was easy to play sound in an applet, but problematic to do so in an application. Java 2 supports a new Sound API to enable easily playing sound in an application. That is beyond the scope of this book, however.
Java applets support only one file format for playing sound, apart from the new Sound API. This format is an .au file . To play sound in an applet, you create, either explicitly or implicitly, an AudioClip object, such as this one:
ac.play();
Listing 9.6 shows the HTML for an applet that plays sound, and Listing 9.7 shows the applet code itself for how to play a sound repeatedly in a loop. It illustrates the use of the start() and stop() methods to ensure that the sound file is played only while the Web page that contains the applet is being viewed. Figure 9.3 shows the applet displayed in the AppletViewer, but of course you'll have to run it to hear the applet play sound (for which you need speakers or headphones).
Figure 9.3 An applet that plays sound displayed in the AppletViewer (Sound.html).
<HTML> <TITLE> Example of an Applet that plays sound in a loop for Pure Java </TITLE> <BODY> <Applet code=SoundApplet.class height=200 width=300> </Applet> </BODY> </HTML>
Listing 9.7 An Applet That Plays a Sound File (SoundApplet.java)
import java.Applet.*; // You need to import the Applet package for an Applet. import java.awt.*; public class SoundApplet extends Applet // All applets must extend java.applet.Applet. { String soundFilename = "spacemusic.au"; AudioClip ac; public void init() // init() is the first user-defined method called in an applet. { ac = getAudioClip(getCodeBase(), soundFilename); // getAudioClip(URL, String) is an Applet method // for retrieving a sound file. // This is for applets only. To play sound // in a Java application, you should use the new // Java 2 Sound APIs. // getCodeBase() returns the URL of the Applet code. // Use this for .au files specific // to the Applet, and store the sound file // in the codebase directory. // Remember that codebase can be specified in // the HTML tags.
// Using a String variable for the filename // allows you to dynamically change it through // a
getParameter() call. }
public void start() // start() is called when you go to the Web page. { repaint(); // Call repaint() to update the applet's display. // Never call paint(). ac.loop(); // Start playing the sound file continuously // in the start method. // To just play the sound, call ac.play(). } public void paint(Graphics g) // Dynamically draw the text with whatever the // sound filename is whenever paint() is called, // which is called by your code (through repaint()) // and the JVM. { g.drawString("Now playing " + soundFilename, 40,50); // Draw the specified String at the given coordinates relative // to the upper-left corner of the applet, which is 0,0. } public void stop() // The browser calls stop() when you change to a // different Web page. { ac.stop(); // Be sure to stop playing the sound file when // the user leaves this Web page. } } // End class definition
SoundApplet Using Images in an Applet Applets provide facilities for displaying images. These images can be either GIF or JPEG images. Note Java actually provides fairly complex image manipulation APIs, such as ImageProducer and ImageConsumer, but these are beyond the scope of this book.
A Single Image in an Applet: Code Example Displaying an image in an applet (or application) is fairly simple. You must 1. Get the image. 2. Draw it on the applet's surface. Figure 9.4 shows an applet that displays a single image.
Figure 9.4 An applet that displays a single image .
Listings 9.8 and 9.9 show the HTML and the applet code for this applet, which displays a single image.
Listing 9.8 An Example of HTML for an Applet That Displays a Single Image (BookImage.html)
<HTML> <TITLE> Example of an Applet that displays an image for Pure Java </TITLE> <BODY> <Applet code=BookImageApplet.class height=400 width=450> </Applet> </BODY> </HTML>
Listing 9.9 An Example of an Applet That Displays a Single Image (BookImageApplet.java)
import java.Applet.*; // You need to import the Applet package for an Applet. import java.awt.*; public class BookImageApplet extends Applet // All applets must subclass java.applet.Applet { Label bookLabel; Image bookImage; // Need an Image object for images.
Label titleLabel; // Declare instance variables. public void init() // init(), like a constructor, is the first user-defined // method normally called in an applet by the browser. { bookLabel = new Label("Here's an Image for the book"); bookImage = getImage(getDocumentBase(), "4QpesherIsaiah_e.jpg"); // To get an image, // you use the getImage method. // getImage() requires a directory // name and a filename. // getDocumentBase() retrieves the // URL for where the Web page // came from. if(bookImage == null) System.out.println("getImage failed"); // If the getImage failed, report it // on the command line. titleLabel = new Label("4QPesherIsaiah"); setLayout(new BorderLayout()); // Override the default layout manager of the // Applet, which is FlowLayout, just like a Panel. add(bookLabel, "North"); add(titleLabel, "South"); repaint(); // Force the screen to be updated. // Always call repaint(), never paint(). // Extra repaint() calls need not hurt performance. } public void paint(Graphics g) // Do all drawing on the applet's
surface, including the drawing of // images, in paint(). { g.drawImage(bookImage, 30,50, this); // drawImage draws an Image on the // Component's surface, in this case // an instance of the BookImageApplet. // drawImage requires an Image object reference, // x and y coordinates, and an // ImageObserver instance. // The Applet itself is an ImageObserver. // The ImageObserver is the object to notify // when pixels of the image are available // to draw on the component. } } // End class definition BookImageApplet. The most important thing to learn from this listing is the drawImage method . There are several forms of this method in the java.awt.Graphics class , including versions that take a specific image size in pixels. In this simple form, you provide the Image object reference, the x and y coordinates for the upper-left corner of the image relative to the whole applet (regardless of the layout manager used), and an ImageObserver . Generally, the value of the ImageObserver object reference will be this (the object reference to the current objectin this case, the
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=78 (9 of 20) [6/2/2002 10:02:54 PM]
applet itself). Because the Component class implements the ImageObserver interface, most components can serve as an ImageObserver. It is the object that is notified of repaint() requirements. Notice that, because this applet does nothing to prevent it starting before the Image is fully downloaded, the ImageObserver in the drawImage method might conceivably be called several timesas more and more pixels are downloaded. In the case of a local system, the multiple calls are not as critical. However, they make a difference in a commercial applet downloaded from a Web server. Later we'll see a way to determine that the images are fully downloaded before they are displayed. ImageObserver is an interface in the java.awt.image package. This interface is implemented only by the java.awt.Component class. The interface specifies several public static final values that each represent a possible state of the object, such as ALLBITS, which shows that all the bits of the image are now present. The interface has only one method, imageUpdate() . This method accepts as parameters an Image object reference, one or more flags as defined in the interface, and the x and y coordinates of where the image should be drawn, as well as the image's width and height. Multiple Images in an Applet: Code Example There are multiple ways to display multiple images. You might choose to use animation, in which case you need to display images at different places on the applet's surface. Or, you might want to create something such as a screen saver, changing the images after a given period of time. In either case, you need to decide how long a given image stays on the screen. Listing 9.10 shows another alternative. In this applet, the user can change the image being displayed using a Next or Previous button, similar to a slide show. This applet demonstrates the use of several MediaTracker methods , which are run through a separate thread. These methods deal with downloading multiple images while not tying up the user interface. This is very important. Tip The example in this section shows how to use the MediaTracker to track the downloading of Images. If you use the Swing ImageIcon class, you automatically get a MediaTracker to monitor the downloading so that it will be easier to code.
You might need to download several images before you can use them, but you also might want the user to be able to interact with the applet before the download is complete. The approach shown by this applet can be used to allow a user to interact with your applet through the AWT thread, while the applet uses a separate thread to download the images. In this particular case, there isn't anything else to do while waiting, but this can vary depending on the applet's purpose and UI. Figure 9.5 shows the applet displaying one of several images in the AppletViewer. Listing 9.10 shows the HTML file for this applet, and Listing 9.11 shows the applet code for displaying multiple images using MediaTracker.
Figure 9.5 An applet that uses MediaTracker to download multiple images in a separate thread.
Listing 9.10 An Example of the HTML for an Applet Displaying Multiple Images and Using MediaTracker (SlideShow.html)
<HTML> <TITLE> Example of an Applet that uses MediaTracker to load multiple Images for Pure Java </TITLE> <BODY> <Applet code=SlideShowApplet.class height=600 width=600> </Applet> </BODY> </HTML>
Listing 9.11 An Applet That Uses MediaTracker to Download Multiple Images in a Separate Thread (SlideShowApplet.java)
// // // // // // // //
This Applet downloads multiple images and waits for the download to be complete before it displays them using a subclass of Canvas called ImageCanvas with its own paint() method. The way the Applet knows the images have all been downloaded successfully is with the MediaTracker class. It checks on the status of the downloading and can wait for it to complete on one or more Image objects. So if you have a problem downloading multiple images for an Applet, and no one else can help, maybe you can hire the MediaTracker.
import java.Applet.*;
import java.awt.*; import java.awt.image.*; // Need this package to support Images here. import java.awt.event.*; public class SlideShowApplet extends Applet implements Runnable, ActionListener { // This applet, of course, extends Applet. It also implements Runnable, // the interface to use if you want to do // multithreading in an applet since you cannot subclass Thread // (which is not recommended anyway), as well as // Applet. While unusual in this book, for simplicity this applet class // also serves as its own ActionEvent listener, implementing the // ActionListener interface by implementing actionPerformed(). // The MediaTracker should always run with a separate // Thread to prevent your UI being frozen. To do this, // your Applet should implement the Runnable interface, // providing a run() method for the Thread to execute. Image[] images; // Declare an array of images to hold multiple // Image objects. MediaTracker tracker; // Declare a MediaTracker. ImageCanvas icv; // This is a custom
component to use for displaying the // images. Like any custom component, it has its own // paint() method, which you have to call manually since // the JVM doesn't know when it should repaint the // component. Button btnNext; Button btnPrevious; int curImage = 0; final int DONE = (MediaTracker.ABORTED | MediaTracker.ERRORED | MediaTracker.COMPLETE);
public void init() { setLayout(new BorderLayout()); // Override Applet default LayoutManager. icv = new ImageCanvas(); // Instantiate the custom component to display images. btnNext = new Button("Next"); btnPrevious = new Button("Previous"); // This Applet allows the user to step through the images // using buttons. images = new Image[7]; // Instantiate the Image array tracker = new MediaTracker(this); // The MediaTracker instance will track image loading for // this ImageObserver, the Applet instance. // Get specific images and add them // to the MediaTracker's list // of images to track.
images[0] = getImage(getCodeBase(), "Paula3.jpg"); // getCodeBase returns the URL of the applet's class file, // usually a directory on the Web server. tracker.addImage(images[0], 0); // After you instantiate an image, tell the MediaTracker // about the image. MediaTracker only tracks the download // progress of images it knows about. images[1] = getImage(getCodeBase(), "Atomium.jpg"); tracker.addImage(images[1], 1); images[2] = getImage(getCodeBase(), "Peleton2.gif"); tracker.addImage(images[2], 2); images[3] = getImage(getCodeBase(), "Carr13.jpg"); tracker.addImage(images[3], 3); images[4] = getImage(getCodeBase(), "Stage15.gif"); tracker.addImage(images[4], 4); images[5] = getImage(getCodeBase(),
"Ph004.jpg"); tracker.addImage(images[5], 5); images[6] = getImage(getCodeBase(), "jurgen3.jpg"); tracker.addImage(images[6], 6); // First image is progressively rendered. if (images.length > 0) { icv.setImage(images[0]); } // The setImage method in the ImageCanvas class // sets the first image loaded as the first // one to appear. Note that setImage() // is a user-defined method, not an SDK // method. add(icv, BorderLayout.CENTER); add(btnPrevious, BorderLayout.NORTH); add(btnNext, BorderLayout.SOUTH); btnPrevious.addActionListener(this); btnNext.addActionListener(this); // Add the ImageCanvas component // to the Applet. Then add the two // Buttons and register listeners on them. (new Thread(this)).start(); // This is a compact way to start a Thread. // The constructor for Thread requires an object // that implements Runnable, as this Applet does. // The Thread instance is then made eligible to // execute using the start() method. // Print overall status information. // This is diagnostic info that will show up on the console. // Do not include it in a production Applet. try { while (!tracker.checkAll()) // checkAll() checks the download status of all the images // the MediaTracker object is tracking. If it returns // false, the images are not fully downloaded yet.
{ Thread.sleep(1000); // Pause this thread for one second before the MediaTracker // checks the download status again. } } // If all the images haven't loaded yet, // sleep some more and hope they finish soon. catch (Exception e) { e.printStackTrace(); } if (tracker.isErrorAny()) // This method returns true if
anything went wrong // while loading the images. { System.out.println("Not all images have been successfully " + "loaded."); Object[] list = tracker.getErrorsAny(); for (int i=0; i<list.length; i++) { System.out.println(list[i]); } } else { System.out.println("All images have been successfully loaded"); } for (int i=0; i<images.length; i++) { int s = tracker.statusID(i, false); // Look at each image and display its download status if ((s & MediaTracker.ABORTED) != 0) System.out.print("ABORTED "); if ((s & MediaTracker.COMPLETE) != 0) System.out.print("COMPLETE "); if ((s & MediaTracker.ERRORED) != 0) System.out.print("ERRORED "); if ((s & MediaTracker.LOADING) != 0) System.out.print("LOADING "); System.out.println(); } }
public void run() // The thread executes this code. { for (int i=0; i<images.length; i++) { try {
tracker.waitForID(i); } // Wait for each image to complete loading. catch (Exception e) { e.printStackTrace(); } repaint(); // Repaint the screen once you've got the image. } } public void actionPerformed(ActionEvent evt) // Process user clicks
of the Next // and Previous buttons. { String arg = evt.getActionCommand(); if ("Next".equals(arg)) { // If the user clicks the next button, move to the next image // and repaint the applet to update the image. if (curImage < images.length-1) { ++curImage; repaint(); } else; } else if ("Previous".equals(arg)) { // If the user clicks the Previous button, // move back one image and repaint the applet. // You could just as easily show the images in a loop // with no user intervention. In that case, you would // want to override the update() method to not erase the // background and also use double buffering to quickly // refresh the applet's display. if (curImage > 0) { curImage; repaint(); } else; } // Change the current image based on the button click // and repaint, which will cause the ImageCanvas to // paint a different image. } public void paint(Graphics g) { if (tracker.isErrorID(curImage))
{ icv.setImage(null); } else { icv.setImage(images[curImage]); } // In case of error, do nothing. // Otherwise, change the Image. btnNext.setEnabled((tracker.statusID(curImage+1, false)&DONE) != 0); btnPrevious.setEnabled( (tracker.statusID(curImage-1, false)&DONE) != 0); // Determine if a button
should be disabled or not. } // End definition of paint method. } class ImageCanvas extends Canvas { Image image; boolean clear; public void setImage(Image image) { this.image = image; clear = true; repaint(); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if (image == null) { g.setColor(Color.red); g.fillRect(0, 0, getSize().width, getSize().height); // Set the background color of the ImageCanvas. } else { if (clear) { g.clearRect(0, 0, getSize().width, getSize().height); clear = false; } // Clear the ImageCanvas drawing area. int w = image.getWidth(this); int h = image.getHeight(this); if (w >= 0 && h >= 0) {
g.drawImage(image, (getSize().width-w)/2, (getSize().height-h)/2, this); // Draw the image on the applet based on the image's // actual size. } // Get the size of the Image and draw it that size. } } // End ImageCanvas definition.
In this applet, I create a custom component, called an ImageCanvas. I use that to display the images. This component is told which image to use by the applet. When the applet starts, it loads seven images, all related to cycling or triathlon. Then, the applet creates a MediaTracker object to monitor the status of the downloading of the images. Note The getImage() call is asynchronous. That is, your program returns immediately after calling it, no matter how long it takes to actually complete downloading the image. That's why the actual image downloading can be monitored after the getImage() call.
The MediaTracker has each image of interest registered with it through the addImage() method. Then, the thread doing the loading just waits via the MediaTracker for all the images to load. When downloading is complete, it calls the repaint method, which calls the paint method independently of the ImageCanvas class. Remember, every component that needs its own custom painting needs its own paint() method. This applet shows nothing but two buttons and some images. If you had a GUI that needed to show images, but you wanted to let the user interact with a more complex GUI while the images were being downloaded, you would need to modify the code so that the AWT thread could be notified that the download was complete, perhaps by setting a flag in the applet's data members or by inserting a synthetic event (a user-defined event) into the event queue and listening for that event in the applet. Tip All the applets in this chapter could also be run as applications. All that is needed to do that is to add a main method to them. This method calls init(), start(), and paint(), and optionally stop() or destroy(). Also, this main method instantiates the applet and creates a Frame to which to add the applet and a WindowListener so that the Frame can be closed. In this way, any applet can easily also run as an application, if that's desired. Note, however, that getCodeBase() and getDocumentBase() work only if the applet is run as an applet.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
Images MediaTracker images registering with MediaTracker images (displaying multiple) in applets programming code 2nd 3rd 4th 5th 6th 7th 8th 9th images (displaying single) in applets programming code 2nd 3rd images (single) displaying imageUpdate() method interfaces ImageObserver 2nd java.awt.Graphics class drawImage method MediaTracker downloading multiple images Images images registering MediaTracker methods methodds addImage() methods drawImage drawString() imageUpdate() MediaTracker paint() 2nd repaint() start() applet sound stop() applet sound numbering applets objects AudioClip creating paint() method 2nd parameters of applets programming code 2nd 3rd setting values playing sounds programming applets code 2nd 3rd images (displaying multiple), code 2nd 3rd 4th 5th 6th 7th 8th 9th images (displaying single), code 2nd 3rd images, code 2nd parameters, code 2nd 3rd sound, code 2nd 3rd 4th 5th repaint() method sound in applets programming code 2nd 3rd 4th 5th playing in AppletViewer sounds
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229191088075188138007221042
All Books
Search
Java > PURE Java 2 > 10. Exceptions > The Throwable Class < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229191088075188143020244031
A Java developer normally doesn't do anything with Throwable, so I'll not consider this class. You have to have a class that is a subclass of Throwable, however, to throw an error or exception. All Throwable objects contain information on the execution stack for the executing Thread at the time the error or exception object was created. You also won't write any code to deal with Error or any subclasses of it. An Error stems from events like device failures or out-of-memory conditions. Of the subclasses of Exception, of which there are many, there are two main groups. There are RuntimeException and other exceptions. RuntimeExceptions are normally not dealt with in your code. Such an Exception probably represents a program bug. For example, if your code tries to access an array element that does not exist, such as myArrayElements[7], when there are only six elements in the myArrayElements object, this would cause an ArrayIndexOutofBoundsException. Or, you might generate a RuntimeException trying to use an object before the object is instantiated, such as adding a button to a panel before executing a button constructor on the Button. This will cause a NullPointerException. Note Because these sorts of exceptions really represent problems in your code, you should not try to catch them or deal with them, but only to resolve them before you distribute your code. So don't apply exception-handling code to any runtime exceptions, which represent exceptions in the Java runtime.
The other major group of Exceptions that Java can throw is a non-RuntimeException. That is, there are lots of
Exception subclasses that do not represent RuntimeExceptions, but are generated by other conditions in your program. You must deal with these sorts of Exceptions. Often these are Exceptions that a developer decided his or her code needed to throw under given circumstances. Java requires that you deal with these Exceptions. Such an exception might not represent a bug or logic problem, as such. Rather, it might merely represent an anomalous condition that you might want to try to deal with in your code. Even if you do not wish to deal with the condition, the Java compiler won't let you not do something about a thrown Exception. Java has a rule. It's the "Handle or declare rule." At compile time, the compiler verifies that all checked exceptions are handled or declared in your code. At runtime, the JVM puts your choice to work. This means that you either need to deal with a potential Exception, or tell Java to pass the Exception on up the call stack to your method's caller. Java will continue up the call stack, looking for some method that will deal with the exception. If it runs out of methods, the Exception is handled by the Java runtime by interrupting execution and displaying the exception on System.err. Handling Exceptions: try-catch-finally The mechanism that Java hasdefined for dealing with Exceptions is try-catch-finally. Here is the basic syntax:
try { // Put your code that might throw // an Exception inside a try block } catch(<SomeExceptionClass> excpObjRef) // Name a specific exception class // and an accompanying object reference. { // Put code here to deal with the Exception // type if it is thrown. } finally // A finally block is optional // If present, it is always executed unless // a "return" statement is executed // before the finally block can be executed. { // Do something that you want to do in this method NO MATTER WHAT } In the try block, you choose a method to call that may throw an Exception or a subclass of Exception. Caution If you fail to put code that may throw a checked exception inside a try block, and do not declare that the method throws an exception, the compiler will not compile your code.
The best way to know if a method can throw an exception is to check the API documentation. If the method or constructor might throw an Exception, it will say
someMethod ( <parameters>) throws SomeExceptionClass For example, the signature for the constructor for a FileInputStream is
public FileInputStream(String aFileName) throws FileNotFoundException If the signature of the method or the constructor says throws someException, this method falls under the "handle or declare rule." After you put code in a try block, you then have to decide what to do about the Exception. You have two options. First, you can catch the Exception using a catch block. Or, second, you can declare that your method throws an Exception (discussed later). In the latter case, the code need not be in a try block. Additionally, you might have a finally clause, in which you perform code that you want done all the time, whether you get an exception or not. For one try block, you might have essentially any number of catch blocks. Java processes catch blocks by examining the type of the Exception class listed in the catch block parameter list. The catch block whose Exception type matches the type of Exception thrown from the try block is executed. try-catch Code Example
Listing 10.1 Basic try-catch Block Example (TryCatch.java)
import java.io.*; import java.util.*; class FileAccessor { public boolean readAReadMe() { boolean result = false; try // Put code that could throw an Exception in a try block { File f = new File("Readme.txt"); // Create a File object FileInputStream fis = new FileInputStream(f); // Create a FileInputStream object result = true; // Show success } catch(FileNotFoundException fnf) { // Do something about the missing file System.out.println ("The file was not found"); } catch(IOException ioe) { // Do something more general about an IOException System.out.println ("Some IOException occurred"); ioe.printStackTrace(); } catch(Exception excp) { // Process another kind of Exception System.out.println [ccc]("Some non-IO kind of Exception occurred: " + excp.getMessage()); // Print the reason // for the exception excp.printStackTrace(); // Show the call stack before this point } return result;
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=81 (3 of 13) [6/2/2002 10:05:08 PM]
public class ExceptionDriver { public static void main(String[] args) { FileAccessor fa = new FileAccessor(); // Create a FileAcceessor object fa.readAReadMe(); // Call a method on this FileAccessor object System.exit(0); // Exit the program } } In the try block, you put code that might throw an Exception. The constructor for a FileInputStream might throw an Exception. If the file does not exist, you will get a FileNotFoundException thrown by the JVM. This exception object will be passed to the first catch block that has a compatible Exception subclass listed as the parameter type. In this case, there will be an exact match between the exception type and the class FileNotFoundException. If some other type of I/O exception occurs, this catch block will be skipped. If the file can be found but is unreadable, the second catch block will be called. Note One possible reason that Java might fail to find a file and, therefore, throw an IOException object occurs when you need to open a file on a diskette for writing. If the Write Protect tab is set to read-only, you will get an IOException that complains of an unreadable file.
You might get an IOException by trying to read from a file on a UNIX system for which you lack permission, like /etc/passwd. This condition will throw an exception object which instantiates a subclass of IOException other than FileNotFoundException. You haven't specifically planned on this condition, so just catch it with an IOException. If you don't have either of these two catch blocks, or if the type of Exception subclass is other than FileNotFoundException or IOException, the catch block that takes an object of type Exception will be called, with the specific exception object passed as a parameter. More generally, the exception object is passed as a parameter to the first catch block that has a parameter of the same class or a superclass. You might wish to think of these catch blocks as similar to overloaded methods. When a method is called in a try block that throws an Exception, the JVM looks for a catch block that has a compatible Exception subclass specified. If it finds none, and it does find a catch block with the base class Exception as the parameter type, it calls this catch block. The class Exception and all its subclasses inherit several methods from Throwable. Two of them are particularly useful in debugging exceptions: getMessage() and printStackTrace(). The method getMessage returns a String that tells what caused the exception. The method printStackTrace() prints the stack trace. Note Frequently a stack trace can be several screens'worth of data. Fortunately, to debug the exception, you only have to look at the location listed in the first line or two of the stack trace. It will generally give the name of the .java file and a line number within that file where the specific exception was thrown, so you can go to that point in your code and figure out what happened.
So to summarize, Java will search through the catch blocks that follow your try block and will use the first catch block that has a parameter type that is compatible with the type of the exception object which was thrown. As implied previously, this can and often will be a superclass of the specific exception object's type. Because Exception is the superclass of all other Exception classes, it can be used in place of any of the other Exception subclasses. When Java calls a catch block, any catch blocks for the same try statement will be skipped. Note It is important to code your catch blocks from the most specific to the most general. This approach will offer the best chance to provide the user with specific, helpful information about what went wrong, or properly handle the exception yourself.
Handling Specific Exceptions Assume that your application needs to have a configuration file to provide information about the environment the program should use, like where to store files. If you try to read this file and you get a FileNotFoundException, but you don't specifically catch a FileNotFoundException, you won't have any intelligent way to deal with it. If you do catch this type of exception, however, you can write code in the catch block to do something useful about it. Tip It might be useful to nest a try-catch combination in the catch block for a FileNotFoundException. You can nest try-catch constructs in a catch block. Suppose you need a configuration file for your program, and the program creates this configuration file for the client system the first time it runs. If the file doesn't exist, it might be the first time the program had been run on this system, because the file is built by the program as part of initialization. It would be quite unhelpful for the program to crash because it hadn't provided for a condition that is not unlikely to happen. To provide for this possible condition, you could put in a catch block to handle it, and the user will never know the difference.
If you don't think you can fix the problem the exception represents, you can at least provide an intelligent response. So, for example, if you got a custom-made exception, like a PrinterNotRespondingException object, you could display a pop-up dialog, like a JOptionPane, to tell the user to check the printer to see if it has a problem. If you do not catch specific exceptions, but only catch objects of type Exception, your program will not be able to readily resolve the problem on its own. You end up giving the user some unhelpful message like "Program error." It's almost always better to gracefully deal with exceptional conditions and, if possible, keep the program running. That will help your users resolve the problem. finally Clause Code Example There is an additional mechanism for dealing with an exceptionnamely, the finally clause. If you put a finally block in the same method with the try block, subsequent to the try block, you can put code in the finally block to deal with exceptions. Listing 10.2 shows how to use the finally clause.
Listing 10.2 Using finally with a try Block (ExceptionDriver2.java)
import java.io.*; import java.util.*; class FileAccessor2 { public boolean getConfigFile(String configFileName) { FileInputStream fis = null; // Never declare something like // this in a try block.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=81 (5 of 13) [6/2/2002 10:05:08 PM]
// It won't be visible anywhere // but in the try block. boolean result = false; try { fis = new FileInputStream(configFileName); // This could throw an exception object. // Do something with the config file. Properties sysProps = new Properties(System.getProperties()); sysProps.load(fis); System.setProperties(sysProps); // Add application properties // to system properties. System.getProperties().list(System.out); // List properties. result = true; // Set the result to true. } catch(FileNotFoundException fnfe) { System.out.println("File Not Found: " + configFileName); } catch(IOException ioe) { System.out.println("IOEException loading " + configFileName); } finally // This method is executed NO MATTER WHAT happened // in the try block. { try { if(fis != null) fis.close(); // Attempt to close the FileInputStream object. // This flushes any buffers and does an actual // write to disk of any cached data. } catch(IOException ioe) { // close() may also throw an exception, // so it needs to be placed within a try block // accompanied by a catch block. System.out.println( "Couldn't close FIS: " + ioe.getMessage()); } System.out.println("In the finally clause"); } return result; } // End of getConfigFile method. } // End of FileAccessor2 class definition. public class ExceptionDriver2 { public static void main(String[] args) { FileAccessor2 fa2 = new FileAccessor2(); boolean result = fa2.getConfigFile("ConfigProps.dat"); System.exit(0); }
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=81 (6 of 13) [6/2/2002 10:05:08 PM]
} Caution You should never declare any variables inside a try block if those variables need to be visible outside of it. In fact, if you do this, and attempt to access them outside the try block, the compiler will give you an error.
Any methods that might throw an exception, like a FileInputStream constructor, must be put inside a try block. You can also include statements inside a try block that don't throw an exception. The compiler doesn't care. The finally block is always called, in spite of any abrupt termination of a try or catch block (including a return statement). That means that whatever you put there should be something you always want done, no matter what, because it will take place whether the code in the try block succeeds or not. So the finally block is useful for things like closing streams, writing log records, setting status flags, and other types of processing that should happen at the end of a method no matter what happened before that point. It is legal to code a try block, one or more catch blocks, and one finally block in the same method. That is, catch and finally blocks can coexist. You must not embed a catch block inside its related try block. You can, however, nest try-catch pairs in a catch block. You might also need to put a try-catch combination inside a finally clause if you call a method in a finally clause that might catch an exception. Because the close() method of a FileInputStream can throw an IOException, the close() method call must be in a try block, with an accompanying catch block. This means that it is possible for the code in finally, even though it is supposed to happen on success or failure of the try block, to throw its own exceptions. Declaring a Method That Throws Exceptions: Code Example Java requires that you handle an exception or declare in your method's signature that it throws one or more Exception types. You would typically do the latter if your method is not the appropriate place to handle the exception. If you take this approach, Java searches up the call stack to find some block of code that will handle the exception. If your code does not ever handle it, it will be printed on System.err. Listing 10.3 shows a method declared to throw an exception.
Listing 10.3 Declaring a Method that Throws an Exception (ExceptionDriver3.java)
import java.io.*; import java.util.*; class FileAccessor3 { FileInputStream fis = null; public boolean readAReadMe() throws IOException, Exception { // This method may throw two // kinds of Exceptions // or one of their subclasses. boolean result = false; try // Put code that could throw // an Exception in a try block. { File f = new File("Readme.txt"); // Create a File object. fis = new FileInputStream(f); // Create a FileInputStream object. result = true; // Show success. } // Because the method is declared as throwing // exceptions, no catch blocks are needed.
finally { // If you have no catch blocks, you have to // have a finally block. if(fis != null) // File actually opened. fis.close(); // No try needed because the method // throws exceptions. } return result; } } // End of class definition for FileAccessor3.
public class ExceptionDriver3 { public static void main(String[] args) throws IOException, Exception { FileAccessor3 fa3 = new FileAccessor3(); fa3.readAReadMe(); System.exit(0); } } In a method that is declared as throwing an exception, you list all the exception classes that your method might throw at the end of the method signature. Or, you can list superclasses of those exceptions. I could have listed only Exception, but I also listed IOException to illustrate that you can list multiple, comma-delimited exceptions. When you code this in a method declaration, you are requiring a caller to handle the exception. It does not mean that the exception will be ignored. It only relieves your method of responsibility for handling the exception. You can code a try block without any catch blocks. If you have no catch blocks, however, you must have a finally clause. Because the readAReadMe method is declared as throwing an IOException, the close() method does not need to be in a try block. In fact, this method didn't need any try blocks, but I used one to illustrate that you can have a try block without any catch blocks, which then requires a finally clause. Creating Your Own Exception Classes: Code Example You can also create your own exception subclass. First, you create a subclass of Exception. I'm going to create a PrinterNotRespondingException. A user-defined Exception class is shown in Listing 10.4.
Listing 10.4 User-Defined Exception (PrinterNotRespondingException.java)
public class PrinterNotRespondingException extends Exception // Subclass Exception { String message; // Define a place to // put the reason for the exception. public PrinterNotRespondingException(String reason) // Define a constructor. { message = reason; // Store the reason for the exception in the // exception object. } public String getMessage() // Override getMessage and code
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=81 (8 of 13) [6/2/2002 10:05:08 PM]
// your own version. { return message; } } // End definition of PrinterNotRespondingException. This is the only time in which it is really proper to name a class by the name of a state instead of an entity, a role usually reserved for data members, not classes. An exception class generally subclasses (extends) Exception or a subclass of Exception, such as RuntimeException. Less commonly, you could subclass Throwable, but I recommend that you do that only if you have a specific reason not to subclass Exception, lest you confuse others about whether this is an exception or error. As a rule, the subclasses are defined in the package most appropriate to the exceptional condition. For example, SqlException is defined in the package java.sql. Exception subclasses need to provide some common elements. There needs to be a String to store the reason for the exception. Also, there needs to be a constructor for the exception, which accepts a String to put into the reason data member. You may optionally code a getMessage method as well to return the reason for the exception along with other information if you wish, or simply use the inherited version of getMessage(). Now that I've defined the class for the Exception subclass, I need a method that can throw this exception. Listing 10.5 illustrates this.
Listing 10.5 Using a User-Defined Exception (ExceptionDriver4.java)
class PrintMaker { // This method attempts to test the printer // to verify that it's ready. public boolean testPrinterStatus(String printerName) throws PrinterNotRespondingException // A method that can throw an // Exception needs to // declare this. The caller is // expected to handle the Exception. { boolean result = checkThePrinter(printerName); // This is a pretend method. if(result == false) throw new PrinterNotRespondingException{ printerName + "not responding in testPrinterStatus()"); // Instantiate a new PrinterNotRespondingException object // and "throw" it. This passes it to the // caller of testPrinterStatus(). return result; } // End testPrinterStatus method definition. public boolean checkThePrinter(String printerName) // This is a pretend // method to illustrate // exception throwing. { return false; } // End of class definition for PrintMaker.
PrintMaker pm = new PrintMaker(); try // Call a method that might throw an exception. { boolean result = pm.testPrinterStatus("Shakespeare1"); } catch(PrinterNotRespondingException pnre) { System.out.println("Failed on printer status" pnre.getMessage()); } } } Note
A method is declared to throw an exception with the throws keyword. The exception is actually thrown with the throw keyword.
A method is declared to throw a PrinterNotRespondingException. Because this method ought to fail if the printer doesn't respond properly, I certainly don't want to catch a PrinterNotRespondingException. Instead, I want to send to the caller a PrinterNotRespondingException and force them to handle it or declare it. To pass an exception to a caller, you must first create an Exception object using the constructor for the Exception subclass. Then you use the throw keyword to pass the exception object from this method to the JVM, which passes it to the caller to deal with. To provide for this, your method's signature must state that it throws an exception of the type the method throws or a superclass of the type that your method throws. So you could declare that your method "throws Exception" instead, but this would be less helpful to other developers using your code or trying to understand it. Caution Never instantiate the exception object before the time it is actually needed. If you create the exception object when your application starts, when you print the stack trace, the trace will likely contain misleading information. This is because the stack information will be from the stack at the time the object was created, not from the stack at the time the exception was being thrown.
Handle or Declare? How do you decide whether to use try-catch (handle the exception) or declare that your method throws an exception (declare)? Usually, it is best to use try-catch because your method is in the best position to know what is really wrong and do something intelligent about it. On the other hand, you might be in a low-level method and feel that any exceptions you get should be passed up to your caller. This is a decision left to the reader. If I were writing a class library of low-level routines, I might declare that my methods threw exceptions. Otherwise, I would recommend that you use try-catch blocks in your code.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
handling or declaring non-RuntimeExceptions RuntimeExceptions try-catch 2nd 3rd code 2nd try-catch-finally 2nd user-defined code 2nd exceptions catch blocks 2nd 3rd 4th try-catch, nesting debugging Error class Exception class 2nd 3rd code for declaring methods that throw exceptions 2nd code to create 2nd creating 2nd FileNotFoundException 2nd finally clause code 2nd 3rd handling or declaring non-RuntimeExceptions RuntimeExceptions try-catch 2nd 3rd try-catch code 2nd try-catch-finally 2nd IOException 2nd non-RuntimeExceptions class objects instantiating RuntimeExceptions class stack trace stack traces throw keyword Throwable class Error class Exception class 2nd 3rd Exception class, code for declaring methods that throw exceptions 2nd Exception class, code to create 2nd Exception class, creating 2nd Exception class, FileNotFoundException 2nd Exception class, finally clause code 2nd 3rd Exception class, handling or declaring Exception class, try-catch 2nd 3rd Exception class, try-catch code 2nd Exception class, try-catch-finally 2nd throwing API documentation try-catch handling with user-defined code 2nd FileNotFoundException 2nd finally clause code 2nd 3rd getMessage() method instantiating exception objects IOException 2nd keywords throw throws methodds printStackTrace()
methods declaring that throw exceptions code 2nd exceptions API documentation signature throws someException throws keyword methodsgetMessage() non-RuntimeExceptions class objects exceptions instantiating printStackTrace() method RuntimeExceptions class non-RuntimeExceptions class stack traces 2nd syntax try-catch-finally throw keyword Throwable class Error class Exception class 2nd 3rd code for declaring methods that throw exceptions 2nd code to create 2nd creating 2nd FileNotFoundException 2nd finally clause code 2nd 3rd handling or declaring try-catch 2nd 3rd try-catch code 2nd try-catch-finally 2nd throws keyword throws someException try blocks variable declaring try-catch 2nd 3rd code 2nd exceptions handling try-catch-finally syntax user-defined Exception class code 2nd user-defined exceptions code 2nd variables delcaring in try blocks
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 11. Multithreaded Programming in Java < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191087204177095044027135
Thread states Creating Threads Primary Thread methods Using the synchronized keyword Avoiding deadlocks with wait() and notify()
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
All Books
Search
Java > PURE Java 2 > 11. Multithreaded Programming in Java > Thread States < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191087204176053249081097
Thread States
A Java Thread object has four states :
q q q
InitializedEntered by calling a constructor from the Thread class RunnableEntered by calling the Thread class's start() method NonrunnableEntered by calling any number of methods, such as wait(), yield(), join(), or suspend() on the Thread object DeadEntered by completing the run() method for the Thread object or by calling stop()
Note A Thread can move from nonrunnable to runnable easily. A Thread that is dead (and eligible for garbage collection if all the references are gone) cannot return to a runnable state.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 11. Multithreaded Programming in Java > Creating Threads < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191087204176060154165120
Creating Threads
There are two ways to create a Thread object. Listing 11.1 shows the approach you should use most often (from an object-oriented design perspective). Creating Threads with Runnable Objects: Code Example You can create a Thread in two ways. You may create a Thread using an instance of a class that implements the Runnable interface. This is the best choice generally from an object-oriented design perspective. Or, you can subclass the Thread class to make an object that inherits from Thread. The former, generally superior approach is shown in Listing 11.1.
Listing 11.1 Creating a Thread with a Runnable Object (ThreadDriver1.java)
class BookFinder implements Runnable // Threads need a Runnable object for their constructor. // That is, they need an object that implements the // Runnable interface. The Runnable interface // has one method: public void run(). { Thread t; String bookTitle; public BookFinder(String title) // Make a Runnable instance. { bookTitle = title; t = new Thread(this); // The constructor for Thread takes an instance of a Runnable, // and at this point, there is an instance, the current // BookFinder instance. This form of the constructor tells the // Thread object where to find the run() method, // which is THE method that runnable Threads execute. t.start(); // Make the Thread Runnable - eligible to execute. } public void run() { System.out.println("Now searching the book database for" + bookTitle); try { Thread.currentThread().sleep(1000); // Use the static Thread method currentThread() // to get a reference to the currently executing Thread
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=85 (1 of 5) [6/2/2002 10:05:30 PM]
// // // // // }
and tell that Thread to sleep, or pause, for 1000 milliseconds. It may sleep longer but should sleep at least that long. This is because this Thread might be pre-empted while sleeping, so it might take over one second to get back to it.
// Catch possible Thread exception. catch(InterruptedException e) { // Nothing meaningful to do for this case, // so do nothing. } System.out.println("Found " + bookTitle); } } // End class definition of BookFinder. public class ThreadDriver1 { public static void main(String[] args) { BookFinder bf = new BookFinder("Pensees by Pascal"); // Create an instance of the BookFinder class that will // start a separate Thread to look for the book in // the library collection (simulated). } } In this example, a class called BookFinder implements the Runnable interface. This signals to the system that the BookFinder class has agreed to the contract to implement the run() method . Next, a thread is constructed, and the constructor is passed a Runnable instance. The code for a Thread object to execute must be defined inside a run() method definition. The run() method in a given class can call other methods, but there is no way to tell a thread to execute some other method instead of run(). You can put any object in the constructor call, as long as it implements the Runnable interface. To help make this clear, look at this code snippet:
Runnable runner = new BookFinder("An Unscientific Postscript by Kierkegaard")); Thread t = new Thread(runner);
The constructor call here could return an object of any class that you wanted to put there, as long as it implemented the Runnable interface. Note The capability to code a generic method that can return an instance of any class that implements the interface listed as the return type is very powerful. It enables, for example, the java.sql.DriverManager getConnection method to return any object of any kind that implements the Connection interface. This makes for very flexible code.
The Thread object is made executable by the start() method . When the thread is scheduled to run, the Thread object executes the run() method code. I've put code in the middle of this method to get the currently executing Thread and make it sleep at least 1000 milliseconds. The sleep() method expects a parameter in milliseconds. When the Thread object completes the run() method, it becomes dead, having finished its task. The method call
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=85 (2 of 5) [6/2/2002 10:05:30 PM]
sleep() might throw an exception, InterruptedException . If your thread is interrupted, there's nothing you can do, so there's usually little point in putting any code in the catch block. To make anything meaningful happen, you have to be able to tell the CPU to reschedule your thread immediately. Because you can't do that, there's really nothing to do in this method, except perhaps call sleep() again. Note There is a relationship between a Java Thread object and a thread , whether it is a green thread in the JVM or a kernel thread . The exact relationship, however, depends on multiple factors, including the JVM implementation and the OS. In general, you can think of the relationship between a Thread and a thread as being similar to that between an AWT component and its native peer. This means that your call to sleep() or wait() really affects a thread, whether it is a kernel thread (most likely) or green or something else. Because the exact relationship is implementation-dependent, I will use the term Thread even when it is most likely a thread of some kind that is affected.
Creating a Thread Subclass: Code Example There is another way to create a Thread that involves subclassing the Thread class itself. This is not a recommended technique, because it generally violates good OO design principles. The reason is that you almost never define classes that have a true IsA relationship with an "execution context." You should not, as Rumbuagh warns, inherit just to get behavior. If you do want to subclass Thread, Listing 11.2 shows an example.
Listing 11.2 An Example of Subclassing java.lang.Thread (ThreadDriver2.java)
class BookFinder extends Thread // Extend the Thread class and code a run() method. { String bookTitle; public BookFinder(String title) { bookTitle = title; } public void run() { System.out.println("Now searching the book database for" + bookTitle); try { sleep(1000); // Use the static Thread method currentThread() // to get a reference to the currently executing Thread // and tell that Thread to sleep, or pause, for 1000 // milliseconds. It may sleep longer but should sleep // at least that long. This is because this Thread // might be pre-empted while sleeping, so it might // take over one second to get back to it. } // Catch possible Thread exception. catch(InterruptedException e) { // Nothing meaningful to do for this case, // so do nothing.
public class ThreadDriver2 { public static void main(String[] args) { BookFinder bf = new BookFinder("Pensees by Pascal"); bf.start(); // Start the Thread executing. // This will cause the BookFinder run() method to execute // eventually, now that start() has made the Thread // runnable. } } Here, the run() method is put inside a subclass of the Thread class. Instead of implementing the Runnable interface, this class extends the thread and provides its own run() method, instead of another class providing it. In general, this approach is less desirable from an object-oriented design perspective. The odds are very low that you are going to define many classes that have an IsA relationship with an execution context. This is an example of incorrect inheritanceinheriting just to get out of writing some code, even though there is no logical relationship between the superclass and subclass. Therefore, it's usually much better to use the method shown in Listing 11.1. Since you generally already have to create a subclass of something anyway when working with multithreading, there's no point to subclassing Thread as well. You might just as well implement Runnable in the other subclasses you need to define.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
run () 2nd sleep () sleep() start () wait() objects. run() method 2nd Runnable objects Thread object creating 2nd 3rd sleep() method 2nd start() method subclassing java.lang.Thread code 2nd Thread object code 2nd 3rd Thread object code creating with Runnable objects 2nd creating with Runnable objects 2nd 3rd subclassing code 2nd 3rd threads relationship threads green kernel Thread object relationship wait() method
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 11. Multithreaded Programming in Java > Primary Thread Methods < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191087204179164071104019
start()Moves a thread from the initialized state to the runnable state. This does not make the thread execute. It makes the thread eligible for execution whenever it is scheduled.
Caution Operating systems differ in the way they schedule threads . Solaris 7, for example, uses pre-emptive multitasking, whereas Windows 95 uses round-robin time slicing. Because Java is platform-neutral, it can run on any number of systems, any of which can have a different scheme for scheduling threads. You can't predict how long a given thread will execute for before it is interrupted or pre-empted, even on a specific system. You cannot write code that can handle all cases. Therefore, your code should always be written with the assumption that your thread may stop executing temporarily at any moment.
Being runnable means that the Thread is now in the pool of runnable Threads, waiting to execute. It might be some time before it actually gets to run, depending upon a number of factors, such as what else is running on the system. Note Unlike some earlier JVMs, Java 2 uses native threads , if possible, rather than green threads by default. This means that in a system with multiple CPUs, various threads in your program may be scheduled on separate CPUs.
sleep()This causes the Thread to become nonrunnable for at least the length of the sleep() method's parameter, which specifies a time in milliseconds for the thread to pause, as long as the sleep() isn't interrupted. The sleep() method yields control of the CPU to any thread (of any priority) that the JVM wishes to schedule. yield()This method conditionally relinquishes control of the CPU. The condition is that there is a thread of equal or greater priority waiting to execute (that is, a Thread that is runnable, as opposed to a Thread in the wait state). So although a call to sleep() will make the thread cease executing at least temporarily, yield() might not cause the thread to pause its execution (if there are no qualified threads available). join()This method is used if you want some code to execute only after another thread has finished execution. This is not a very common method. You would use it this way:
Runnable r1 = new BookFinder("Against Apion by Josephus" Thread t1 = new Thread(r1); t1.start t1.join(); long timeGap = System.getCurrentTimeMillis();
The call to getCurrentTimeMillis()won't execute until t1 has completed executing BookFinder's run() method. You can optionally specify a time interval in the join() method call. This time gap, specified in milliseconds and, optionally in nanoseconds, tells how long to wait for the joined Thread to complete its work. If it is still not done after that interval, the caller of the join() method is allowed to resume execution. Note Most operating systems cannot slice time finely enough to wait an interval of a nanosecond meaningfully. Therefore, the nanoseconds parameter is generally not very useful. In addition, it is difficult to guess, even for one specific system, how much work could get done in 15 nanoseconds, let alone what would be an appropriate interval for all systems. Therefore, you should probably just skip this optional parameter.
suspend() and resume()This pair of methods lets you stop a thread from executing (and make it nonrunnable) immediately and then make it runnable again at a later time. This pair of methods is deprecated in Java 2. The reason is that the suspend() method can cause a deadlock. For example, a Thread t1 is using a resource and has an object lock (described later) on that resource. If t1 is suspended, it can neither execute nor give up the object lock. Another Thread that needs the same resource, and the object lock will never get it. t1 holds the lock and can't execute because it is suspended. To prevent this kind of deadlock, these two methods have been deprecated in Java 2. Do not use them. Instead, use better multithreaded programming algorithms.
stop()The stop() method causes a thread to immediately halt in its tracks and die. Unlike suspend(), the thread will release the resources it has locks on, but it will not clean up the state of objects it is working with. For example, if the Thread is using a database connection from a connection pool, it will not be able to give the connection back or notify any other object of its death. It is immediately terminated. This could leave output streams unflushed and unclosed. In view of its dangers, this method has also been deprecated in Java 2. If you want a thread to be able to terminate before it reaches the end of its run() method, you should provide a flag such as the following:
boolean stopRunning= false; and use this flag inside the run() method, perhaps like this: public void run() {
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=86 (2 of 3) [6/2/2002 10:05:35 PM]
All Books
Search
Java > PURE Java 2 > 11. Multithreaded Programming in Java > Using Synchronization < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191087204178239189050109
Using Synchronization
When there are multiple Threads running in a JVM, it is possible that multiple Threads will want to modify the same resource (object) at the same time. The simplest way to illustrate this is with the classic producer-consumer model. Imagine an array of float primitives:
This creates an array for holding six float primitives. In the producer-consumer model, a Thread, t1, tries to add floats to this array as fast as it can. Another Thread, t2, the consumer, tries to read and remove those floats as fast as it can. Typically, some kind of counter would be used to track which slot in the array last had a float put in it or taken from it. Listing 11.3 shows an unsynchronized (dangerous) way to implement this.
Listing 11.3 Unsynchronized Producer-Consumer Example (ThreadDriver3.java)
import java.util.*;
class Producer implements Runnable { Thread t1; DateHolder dates; public Producer(DateHolder d) { dates = d; // Get object reference to the Date Array. t1 = new Thread(this); // Create a new Thread using a Runnable object instance. t1.start(); // Make the Thread runnable, and eligible for execution. } public void run() { for(int i = 0; i < 100; i++) { // Add 100 elements to the DateHolder array. while(dates.counter < (dates.dateArray.length -1) ) // Test to be sure there is space in the DateHolder array.
{ dates.counter++; int j = dates.counter; dates.dateArray[j] = new Date(); // Put a Date object in the DateHolder array. } try { Thread.currentThread().sleep(100); } catch(InterruptedException e) { } // Give another thread a chance to run // by causing the current Thread to sleep briefly. // Must catch the possible InterrruptedException. } } } class Consumer implements Runnable { // This class is for reading Date objects from a Date array // as fast as possible. Thread t2; DateHolder dates; public Consumer(DateHolder d) { dates = d; // Get object reference to the Date Array. t2 = new Thread(this); // Create a new Thread using a Runnable object instance. t2.start(); // Make the Thread runnable, and eligible for execution. } public void run() { for(int i =0; i < 100; i++) { // Remove 100 elements to the DateHolder array. while(dates.counter > 0) // Test to be sure there are elements in the DateHolder array. { int j = dates. counter; Date d = dates.dateArray[j]; dates.counter--; System.out.println("Last date extracted was " + d); // Get a Date object out of the DateHolder array. } try { Thread.currentThread().sleep(100); // Make current Thread sleep briefly.
class DateHolder { // This class is an array for Date objects. // It is a resource that is shared // among multiple Thread objects. Date[] dateArray; int counter = 0; public DateHolder() { dateArray = new Date[100]; } } // End class definition for DateHolder. public class ThreadDriver3 { public static void main(String[] args) { DateHolder dueDates = new DateHolder(); // Simulate getting book due dates. Producer pro = new Producer(dueDates); // Start an object putting dates into the // DateHolder dateArray. Consumer con = new Consumer(dueDates); // Start an object removing dates from the // DateHolder dateArray. } } // End class definition of ThreadDriver3 This program shows a basic producer-consumer scenario. The problem is that there's a huge logic bug here. Both the Producer thread and the Consumer thread are updating the dateArray object and the counter. That's okay, in principle, unless a scenario such as the following occurs: 1. Thread t1 increments the counter. 2. Thread t1 becomes nonrunnable (perhaps it is pre-empted). 3. Thread t2 goes to the dateArray and removes the Date object at the position indicated by the counter. Because t1 might never have put anything there, this slot in the dateArray is null. 4. Thread t2 decrements the counter by 1. 5. Thread t1 puts a new Date object in the dateArray based on the current value of the counter. This is the wrong place to put it. This sort of scenario is completely plausible. You cannot determine when a thread will stop executing, either because it is pre-empted, uses up its time slice, or for any number of other reasons. The result is that t2 prints bogus data for a Date object (a null value). Thread t1 overwrites a valid Date object with a new value that
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=87 (3 of 8) [6/2/2002 10:05:41 PM]
Thread t2 might never see because the counter is now messed up. Java provides a means to deal with this. It's called synchronizing. Essentially, this means that no two threads can access the same object, if they both synchronize on it. To do this, you use the synchronized keyword. When you synchronize on a piece of code, or a method, you are given an object lock for the object in the code. As long as your thread owns that object or monitor lock, no other thread can access that object or primitive. As an analogy, picture a supply cabinet at work that has a lock with only one key. If I have the key, you can't access the cabinet. You have to wait until I return the key. Object locks work the same way. To enter a synchronized code block, a thread has to obtain the object lock. If another thread has the lock, that Thread does not give up the lock until it exits the synchronized code block. This is done automatically for you by the JVM. All you need to do is something like the following:
or
You can either make a whole method synchronized, or synchronize just a block of code inside a method. Note Synchronizing just one block of code can give you greater concurrency, because you hold the object lock no longer than absolutely necessary. At the same time, the performance of your program will be worse because extra system method calls are made for you when you synchronize on just a block of code, rather than on a whole method.
Synchronization Code Example Listing 11.4 shows an improved, and more OO, version of the program.
Listing 11.4 Using Synchronization with Threads (ThreadDriver4.java)
import java.util.*;
public Producer(DateHolder d) { dates = d; // Get object reference to the Date Array. t1 = new Thread(this); // Create a new Thread using a Runnable object instance. t1.start(); // Make the Thread runnable, and eligible for execution. } public void run() { for(int i = 0; i < 100; i++) { dates.push(new Date() ); // Add 100 elements to the DateHolder array. try { Thread.currentThread().sleep(100); } catch(InterruptedException e) { } // Give another Thread a chance to run. } } } // End class definition for Producer.
class Consumer implements Runnable { Thread t2; DateHolder dates; public Consumer(DateHolder d) { dates = d; // Get object reference to the Date Array. t2 = new Thread(this); // Create a new Thread using a Runnable object instance. t2.start(); // Make the Thread runnable, and eligible for execution. } public void run() { for(int i =0; i < 100; i++) { // Remove 100 elements to the DateHolder array. Date d = dates.pop(); try { Thread.currentThread().sleep(100); }
class DateHolder { Date[] dateArray; int counter = 0; public DateHolder() { dateArray = new Date[100]; } public synchronized boolean push(Date d) { while(counter < (dateArray.length -1) ) // Test to be sure there is space in the DateHolder array. { counter++; int j = counter; dateArray[j] = d; // Put a Date object in the DateHolder array. break; // Leave while loop. } return true; } public synchronized Date pop() { Date d = null; while(counter > 0) // Test to be sure there are elements in the DateHolder array. { int j = counter; d = dateArray[j]; counter--; System.out.println("Last date extracted was " + d); // Get a Date object out of the DateHolder array. break; } return d; } } // End class definition for DateHolder.
main(String[] args) { DateHolder dueDates = new DateHolder(); // Simulate getting book due dates. Producer pro = new Producer(dueDates); // Start an object putting dates into the // DateHolder dateArray. Consumer con = new Consumer(dueDates); // Start an object removing dates from the // DateHolder dateArray. } } // End class definition of ThreadDriver4. I've added two methods to the DateHolder class: push() and pop(). The push() method adds Date objects to the dateArray object , and the pop() method removes Date objects from the dateArray object. The push() method is called by thread t1 and the pop() method is called by thread t2. This is an improvement over the last version of the program. However, because push() and pop() are both synchronized, there is no way for the push() method to access the dateArray object or the counter until the pop() method has completed and returned the object lock to the JVM, and vice versa. There's no way for data to be corrupted or missed in this version. Using the synchronized keyword helps insure data integrity. Note To get the benefit of synchronization, all the methods that access the shared resources must be synchronized. If push() is synchronized and pop() is not, synchronization does not guarantee anything. The pop() method can freely modify the counter, for example, because it doesn't need the object lock to do so.
You cannot enter synchronized code without the object lock , so if the pop() method is being executed, and thread t1 calls the push() method, it will have to wait until it receives the object lock before it actually can execute the push() method.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
object synchronized code method pop() push() methods synchronizing multiple Threads synchronizing 2nd 3rd 4th 5th 6th code 2nd 3rd 4th 5th object lock synchronized code objects Date adding removing dateArray object dates, adding or removing pop() method push() method synchronized code object lock synchronizing code one block of methods Threads code 2nd 3rd 4th 5th multiple 2nd 3rd 4th 5th 6th Threads multiple code to synchronize 2nd 3rd 4th 5th synchronizing 2nd 3rd 4th 5th 6th
2002, O'Reilly & Associates, Inc.
Safari | PURE Java 2 -> Avoiding Deadlocks with wait() and notify()
All Books
Search
Java > PURE Java 2 > 11. Multithreaded Programming in Java > Avoiding Deadlocks with wait() and notify() < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191087204178231106121138
To avoid the deadly embrace, you need a way for a Thread to voluntarily release its object lock , at least temporarily, so that another thread can acquire a shared resource and complete its work. Java provides a mechanism for doing thisusing wait() and notify(). These two methods are inherited from the class java.lang.Object so any object can call wait() or notify(). Here's what these two methods do:
q
wait()Gives up the object lock(s) acquired for the current synchronized code block or method and becomes nonrunnable. notify()Tell a wait()ing thread that it can wake up and prepare to get the object lock back. This does not give the object lock to the wait()ing thread. It merely tells the wait()ing thread that it can wake up and prepare to get the lock. That process is done for you by the JVM. You don't write code to "wake up and prepare to get the object lock." It's important to note that you are only allowed to call wait() or notify() from synchronized code. Because these methods apply specifically to object locks, that makes perfect sense. If you are not in synchronized code, you don't have an object lock, so calling wait() would not give up an object lock. There is another method, notifyAll() that does the same thing as notify, except that it wakes up all
Safari | PURE Java 2 -> Avoiding Deadlocks with wait() and notify()
threads that are wait()ing. wait() and notify() Code Example Listing 11.5 shows a new version of our producer-consumer program that uses wait() and notify(). Note As you'll see in Listing 11.5, it's crucial that you put code into your synchronized method to verify that, when you wake up from the wait() call and get the object lock, you proceed to do work. The notifyAll() method might wake up your thread when that is not appropriate. You don't want to go ahead and run if that doesn't make sense.
import java.util.*;
class Producer implements Runnable { Thread t1; DateHolder dates; public Producer(DateHolder d) { dates = d; // Get object reference to the Date Array. t1 = new Thread(this); // Create a new Thread using a Runnable object instance. t1.start(); // Make the Thread runnable, and eligible for execution. } public void run() { for(int i = 0; i < 100; i++) { dates.push(new Date() ); // Add 100 elements
Safari | PURE Java 2 -> Avoiding Deadlocks with wait() and notify()
catch(InterruptedException e) { } // Give another Thread a chance to run. } } } // End class definition for Producer. class Consumer implements Runnable { Thread t2; DateHolder dates; public Consumer(DateHolder d) { dates = d; // Get object reference to the Date Array. t2 = new Thread(this); // Create a new Thread using a Runnable object instance. t2.start(); // Make the Thread runnable, and eligible for execution. } public void run() { for(int i =0; i < 100; i++) { // Remove 100 elements to the DateHolder array. Date d = dates.pop(); try { Thread.currentThread().sleep(100); } catch(InterruptedException e) { } // Give another thread a chance to run. } } } // End Class definition
for Consumer.
Safari | PURE Java 2 -> Avoiding Deadlocks with wait() and notify()
public DateHolder() { dateArray = new Date[1]; } public synchronized boolean push(Date d) { while(counter >= (dateArray.length -1) ) { try { wait(); } catch(InterruptedException ie) { } // There is no room in the dateArray, so // so give up the object lock. // Put this Thread into a wait (non-runnable) state // and wait for some space in the dateArray. } // This test needs to be done in a while loop. counter++; dateArray[counter] = d; // Put a Date object in the DateHolder array. notify(); // Tell the other Thread that is popping // to wake up and get ready to go. This Thread is about to // release the object lock. return true; } public synchronized Date pop() { Date d = null; while(counter < 0) // Test to be sure there are elements in the DateHolder array. { try { wait(); } catch(InterruptedException ie) { } // There are no elements in the dateArray, // so give up the object lock. // Put this Thread into a wait (non-runnable) state // and wait for some
Safari | PURE Java 2 -> Avoiding Deadlocks with wait() and notify()
elements in the dateArray. } // This test needs to be done in a while loop. d = dateArray[counter]; counter--; System.out.println("Last date extracted was " + d); // Get a Date object out of the DateHolder array. notify(); // Tell the push() Thread to wake up. return d; } } // End class definition for DateHolder. public class ThreadDriver5 { public static void main(String[] args) { DateHolder dueDates = new DateHolder(); // Simulate getting book due dates. Producer pro = new Producer(dueDates); // Start an object putting dates into the // DateHolder dateArray. Consumer con = new Consumer(dueDates); // Start an object removing dates from the // DateHolder dateArray. } } // End class definition of ThreadDriver5. Both the push() and pop() methods have had a call to wait() and notify() added to them. Notice that the call to wait(), in each case, is inside a while loop. It could be inside an if statement, but that would be a problem. If the pop method checks in an if statement to see if there are any elements in the dateArray and finds there are none, it calls wait(). Then, it wakes up from the push method's notify() call when the Thread for pop() gets an object lock. The Thread executing pop() proceeds, because now it has the object lock. There's no way of knowing at this point, however, if there are any elements in the dateArray object. This thread might have been notify()ed by some other thread doing a notify() or notifyAll(). That would cause the pop() method to try to take an element from an invalid position in the dateArray object. The wait() call must be done inside a while loop. That way, if there's no element to retrieve in the pop() method or no space left in the push() method, wait() will be called again and again until conditions are right to execute the rest of the method. The notify() method is the next to last statement in each method. The notify() call could be put almost anywhere in the method after the while loop. The notify() method does not cause the wait()ing thread to start executing. It merely moves it to a different queue where it can await the object lock, which will not be available until the method that calls notify() actually completes. So here's a potential scenario: 1. Enter the push() method with the object lock. 2. Test to see if it is valid to add an element to the dateArray object. 3. Potentially go into a wait state, becoming nonrunnable. 4. Potentially be awakened and given the object lock by another method that does a notify() or notifyAll() and then gives up the object lock. 5. Test again, to see if the Thread called wait(). If not, work can proceed.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=88 (5 of 7) [6/2/2002 10:05:49 PM]
Safari | PURE Java 2 -> Avoiding Deadlocks with wait() and notify()
6. When work can proceed, add an element to the dateArray object. 7. Call notify(). 8. Return true. 9. Exit the push() method and give up the object lock. 10. Let the system give the object lock to the thread running the pop() method. Caution This small sample program has one more potential problem that you should avoid in your code. It is possible in this scenario for the thread calling push() to wait() and the thread calling pop() to wait(). This would leave both threads wait()ing with no mechanism to wake up either thread. Although that is unlikely here, it is possible and unpredictable. One of the worst things about this kind of problem is that it might happen on system A, but not system B. To avoid this, you need to add extra code to your program to make sure that multiple methods, which synchronize on the same data, cannot all put their calling Threads into a wait() state at the same time. This problem will probably not happen here because the code uses an array. Other types of collections or data structures might create such a problem more readily.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
Safari | PURE Java 2 -> Avoiding Deadlocks with wait() and notify()
object locks releasing pop() method waiting push() method waiting threads deadlocks avoiding 2nd 3rd 4th 5th 6th 7th 8th 9th Threads object locks releasing wait() method deadlocks avoiding 2nd 3rd 4th 5th 6th 7th 8th 9th push() method and pop() method waiting
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229191087204181007080006210
All Books
Search
Java > PURE Java 2 > 12. I/O in Java > A Quick Overview of Stream < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191087204180226082209154
Safari | PURE Java 2 -> An Overview of the Main Stream, Reader, and Writer Classes
All Books
Search
Java > PURE Java 2 > 12. I/O in Java > An Overview of the Main Stream, Reader, and Writer Classes < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229191087204180225201092219
FileInputStreamUsed for reading files at the byte level SequenceInputStreamUsed for combining multiple files into one input stream PipedInputStreamUsed for reading a stream through a pipe from another Thread or the like DataInputStreamUsed for reading Java primitives, such as float, char, int, and boolean, as well as for reading in UTF strings ObjectInputStreamUsed for reading in both primitives and serialized objects PushbackInputStreamUsed for reading one or more bytes and then returning those bytes to the input stream, potentially to read them again LineNumberInputStreamUsed for reading a file one line at a time and getting the number of the line read, suitable for a program editor, for example StreamTokenizerUsed for reading in a stream in tokens BufferedInputStreamUsed for reading in large amounts of data, or buffering input for efficiency ByteArrayInputStreamUsed for reading in data from an array of bytes
q q
q q
The base class of all output streams is OutputStream, which is abstract. OutputStream has a few basic methods, such as write(), write(byte[] b), flush(), and close(). Some of the main OutputStream classes are
q q q
FileOutputStreamUsed for writing out files at the byte level PrintStreamUsed for writing out lines of output, often to the console PipedOutputStreamUsed for writing data to a pipe between Threads and the like BufferedOutputStreamUsed for writing out data in large blocks, using buffers for efficiency ObjectOutputStreamUsed for writing out primitives and serialized objects to a stream
Safari | PURE Java 2 -> An Overview of the Main Stream, Reader, and Writer Classes
q
Note The type of standard input (System.in in Java) is an InputStream. The type of standard output (System.out in Java) is PrintStream.
The base class for Reader classes is Reader, an abstract class with a small number of basic methods, such as read(), read(char[] c), reset(), mark(), and close(). Some important Reader subclasses include
q q
FileReaderUsed for reading files at the Unicode character level BufferedReaderUsed for reading large block of data, as characters, through a buffer CharArrayReaderUsed for reading a char array InputStreamReaderUsed for converting an InputStream to a Reader. This allows you to read whole characters from System.in, for example. LineNumberReaderUsed for reading whole lines with line numbers returned PipedReaderUsed for reading input through a pipe, between two Threads, for example
q q
q q
The Writer class is the abstract base class for all Writers. It has a small number of basic methods, such as write(), write(char[] c), flush(), and close(). The Writer subclasses include
q q
FileWriterUsed for writing files on a character-oriented level BufferedWriterUsed for writing out large blocks of data as Unicode characters using buffers CharArrayWriterUsed for writing out char arrays PipedWriterUsed for writing data to a pipe as chars, such as between two Threads PrintWriterUsed for writing output as lines of chars
q q
As you can see from this list, there are Reader and Writer classes to match most stream classes. Also, for most input stream or Reader classes, there is an output stream or Writer counterpart. Tip In general, you should use Reader and Writer classes rather than stream classes for character-based I/O. However, the Reader and Writer classes by default can be up to three times slower than the stream classes. To avoid this performance penalty, be sure to set the Reader or Writer class you use so that it will use buffering. This will offset the disadvantage of using Reader and Writer classes.
Safari | PURE Java 2 -> An Overview of the Main Stream, Reader, and Writer Classes
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
Safari | PURE Java 2 -> An Overview of the Main Stream, Reader, and Writer Classes
All Books
Search
Java > PURE Java 2 > 12. I/O in Java > Using Files in Java < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229191087204180235199184136
FileCan only be written to sequentially RandomAccessFileCan be read from or written to at any point in the file
To be able to use a file in Java, you create a File or RandomAccessFile object. Warning It is critical to remember that creating a File object such as
File myFile = new File("/export", config.dat"); does not have anything to do with whether or not this file exists. You can execute this constructor for a nonexistent file. Java does not throw an exception for a nonexistent file until you actually try to create a stream that uses the file.
File: Code Example The File class has three constructors and a number of useful methods. Here are the three constructors:
File("file name") File("Directory name") File("directory name", "Filename") You can, of course, define the String objects used for the directory name and filename elsewhere. In general, you probably would do so for flexibility in your code. You can find out a fair amount of information about a file. Listing 12.1 shows some of the methods in action.
Listing 12.1 An Example of Using the File Class (FileDriver1.java)
import java.io.*; // Import the java.io package. import java.util.*; class FileExplorer { File dirName; File theFile; byte[] data; public FileExplorer()
{ String temp = " Input to the file: 100-10-2234"; data = temp.getBytes(); // Convert the String into a byte array. dirName = new File("C:\\PureJava\\source\\chap12"); // Create a directory File object. theFile = new File(dirName, "tempdata.dat"); // Create a File object, specifying both // the directory for the file // and the filename. // Note the use of double backslashes in the path // name "\\". // This is, of course, only needed in Win32 // environments. try { FileOutputStream fos = new FileOutputStream(theFile); // The FileOutputStream takes a File in // one of its constructors. fos.write(data); // Write out a byte array of data.length // number of bytes. fos.flush(); // Be sure all data goes to disk from cache. fos.close(); // Close the stream. } catch(IOException e) { System.out.println("IOException " + e.getMessage()); } } public boolean printFileInfo() { try { FileInputStream fis = new FileInputStream(theFile); // Create a FileInputStream to read the file. fis.read(data); fis.close(); System.out.println(data); // Print the file contents. System.out.println("The file can be read: " + theFile.canRead()); // Is the file readable? System.out.println("The file can be written to: " + theFile.canWrite()); // Is the file writeable? System.out.println("The file exists: " + theFile.exists()); // Does the file exist? System.out.println("The absolute path of the file is: " + theFile.getAbsolutePath()); // What is the file's absolute path? System.out.println("The canonical file path is: " +
theFile.getCanonicalPath()); // What is the canonical path? System.out.println("The path is: " + theFile.getPath()); // What is the path of the file? System.out.println("The file name is: " + theFile.getName()); // What is the name of the file? System.out.println("Is the File object the absolute path: " + theFile.isAbsolute()); // Does the File object represent the // absolute path? System.out.println("The File object represents a file: " + theFile.isFile()); // Does the File object represent a file? System.out.println("The File object represents a directory: " + theFile.isDirectory()); // Does the File object represent a directory? System.out.println("The file is hidden: " + theFile.isHidden()); // Is the file hidden? Date modDate = new Date(theFile.lastModified()); System.out.println("The file was last modified: " + modDate.toString()); // When was the file last modified? System.out.println("The file is: " + theFile.length() + " bytes long"); // How long is the file? theFile.delete(); // You can delete a physical file // as well as call mkdir() on a file object // or renameTo() a new filename. // Finally, list the contents of this directory. String[] fileList = dirName.list(); for(int i = 0; i < fileList.length; i++) System.out.println(fileList[i]); } catch(IOException e) { System.out.println("IOException " + e.getMessage()); } return true; } } // End class definition for FileExplorer. public class FileDriver1 { public static void main(String[] args) { FileExplorer fe = new FileExplorer(); fe.printFileInfo(); System.exit(0); }
} This program illustrates a number of points. It shows how to create a basic byte- oriented stream, FileOutputStream, and also FileInputStream. It shows how to create a byte array from a string with the getBytes() method. It shows how to read to and write from a stream and, implicitly, from a file. It also shows several of the methods in the File class that provide information about the file. Tip The File method list() could be used with the method isDirectory() to recursively step through an entire file system and print out its contents.
Although calling delete() is not normally recommended, this program also shows the use of the delete() method. Note that if this call to delete() is removed, you can call the program over and over, and it will still end up with the same contents. It will not append or add any data beyond the original 32 bytes. RandomAccessFile Code Example Java also has a RandomAccessFile class, which is an important feature for file I/O. You might, for example, create a File object today and write through it to a physical file. In a Java program, if you use that file again tomorrow by writing to it, its original contents are thrown away. You essentially start from scratch. A RandomAccessFile does not work that way. You can write to it, close it, and write to it again, without losing any data. This is shown in Listing 12.2. In order to modify or append to a RandomAccessFile object's underlying physical file, you need to seek() to a specific location. Doing so positions the file pointer just before that location. Note There is no open()method or create() method for a file, as might be present in some system calls or other programming languages, such as C. Java creates and opens file for you in whatever manner the platform you are using requires, and your code does not have to know anything about it.
// RandomAccessFile example import java.io.*; class RandomProcessor { RandomAccessFile raf; String fileName = "NewTitles.txt"; int i = 0; // Better performance as an instance variable. String[] titles = { "New Titles in the Library", "Truth is Stranger than It Used to Be", "How Shall We Then Live?", "The Pilgrim\'s Regress"} ; // Define a String array. public RandomProcessor() { try { raf =new RandomAccessFile(fileName, "rw"); // Instantiate a RandomAccessFile object // for read and write.
// Specifying only "r" instead of "rw" means // read-only in this application. // Note the need to put the constructor in a try // block. } catch(FileNotFoundException fnfe) { System.out.println("File " + fileName + "not found" + fnfe.getMessage()); } } public boolean readSomeStrings() { try { raf.seek(0); for(i = 0; i < titles.length; i++) { // Read until end of file. String s = raf.readUTF(); System.out.println(s); } System.out.println (raf.readUTF()); } catch(Exception e) { System.out.println("Error reading file: " + e.getMessage()); } return true; } public boolean writeSomeStrings() { try { for(i = 0; i < titles.length; i++) { raf.writeUTF(titles[i]); Performing I/O in Java // Write some modified UTF-8 Strings to the file. } // Now, write data to a specific location // in the file: the end. raf.seek(raf.length()); // seek() goes to a specific byte location // in the file. The length() method, not to be // confused with the length data member of arrays, // returns the number of bytes in the file. // This particular combination writes at the end // of file. raf.writeUTF(titles[1]); }
catch(IOException ioe) { System.out.println("Error writing file: ioe.getMessage()); } return true; } } // End class definition RandomProcessor. public class FileDriver2 { public static void main(String[] args) { RandomProcessor rp = new RandomProcessor(); rp.writeSomeStrings(); rp.readSomeStrings(); System.exit(0); } } UTF strings, used in the preceding listing, will be explained later.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
" +
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
getBytes() isDirectory() list() open() open() method RandomAccessFile class code 2nd 3rd streams files 2nd
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 12. I/O in Java > Performing I/O in Java < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229190151042189083133205150
Byte-oriented I/O I/O with Java Primitives Write and read lines of text Write and read entire objects
Performing I/O Using FileInputStream and FileOutputStream: Code Example FileInputStream and FileOutputStream are the basic, concrete classes for doing I/O. Despite their names, you do not need to use them for writing to or reading from files as such. You should select the stream, Reader, or Writer that you want to use based on the granularity or level of abstraction you want to work at. You choose FileInputStream and FileOutputStream to work at the byte level. Listing 12.3 shows a simple example of using these two streams to write and read bytes.
Listing 12.3 Using FileInputStream and FileOutputStream (FileDriver3.java)
// Byte-oriented I/O with FileInputStream and FileOutputStream example import java.io.*; class ByteProcessor { FileInputStream fis; FileOutputStream fos; String String byte[] byte[] fileName = "BookInfo.txt"; book = "Pascal, Blaise, Pensees, PN 1901 P43 T64"; dataIn; dataOut;
public ByteProcessor() { try { fos = new FileOutputStream(fileName); // Instantiate a FileOutputStream for writing. // If a SecurityManager were instantiated, this // constructor call would cause a checkWrite() method // to verify that this class has permission for this. // Note the need to put the constructor in a try // block.
} catch(IOException ioe) { System.out.println("File " + fileName + "error" + ioe.getMessage()); } } public boolean writeBookCatalogInfo() { try { dataOut = book.getBytes(); // Create a byte array fos.write(dataOut); // Write a byte array to the FileOutputStream. // This will cause the file, if it does not exist, to be // created before it is written to. fos.flush(); fos.close(); fos = null; // Flush the stream to force the data to disk. // Close the FileOutputStream so the file can be read. // Then set the object reference to null to // encourage garbage collection. } catch(IOException e) { System.out.println("Error writing file: " + e.getMessage()); } return true; } public boolean readBookCatalogInfo() { try { fis = new FileInputStream(fileName); // Create a FileInputStream object on the file. // The physical file needs to exist when you do this. dataIn = new byte[dataOut.length]; fis.read(dataIn, 0, dataIn.length); // Read from the file from offset 0 for a given length. String temp = new String(dataIn); // Convert to a real String. System.out.println(temp); } catch(IOException ioe) { System.out.println("Error reading file: ioe.getMessage()); } return true;
" +
} } // End class definition PrimitiveProcessor. public class FileDriver3 { public static void main(String[] args) { ByteProcessor bp = new ByteProcessor(); bp.writeBookCatalogInfo(); bp.readBookCatalogInfo(); System.exit(0); } } Usually, you use FileInputStream and FileOutputStream as the basis of other stream classes when you wrap classes. Tip As you'll see throughout this chapter, it is common to wrap one stream or Reader or Writer class inside another. The idea here is that you start with the basic level of abstraction, such as bytes, using FileOutputStream and FileInputStream, and then you encapsulate this inside another I/O class that has a higher level of abstraction such as an ObjectOutputStream or ObjectInputStream.
Although the example shows the use of byte arrays for I/O, you can write individual bytes to a FileOutputStream one at a time and read them back in with a FileInputStream one at a time. That might be particularly useful if reading data created by another programming language, such as C. Performing I/O on Java Primitives Using DataInputStream and DataOutputStream: Code Example These classes are used if you want to work with Java primitive data types, such as float, int, and so forth. Instead of having to write four bytes to implicitly make an int in a file, you can simply do a writeInt() call. These two streams also support UTF strings. DataInputStream has a method called readUTF(), and DataOutputStream has a method called writeUTF(). Unfortunately, not all languages can be represented through Unicode. For example, the Korean alphabet and other pictographic languages cannot be expressed completely with only two-byte Unicode characters. To circumvent this limitation, Java can work with UTF strings. UTF stands for UCS Transformation Format. UCS stands for Universal Character Set. Unlike Unicode, which uses two bytes for every character, a UTF character is variable in length. The character begins with the number of bits required to uniquely represent the character, followed by the bit pattern for the character. This might be three bytes, or four bytes, or more. Java enables you to write such characters to or from a stream. You can also use readUTF() and writeUTF() to deal with plain English strings. If you code
writeUTF("Death to gotos"); under the covers, Java does the UTF translation necessary. As a rule, you use this approach for strings that can be expressed in Unicode, but this is also the method you use to write a String object if you were using a DataOutputStream or DataInputStream for other reasons. Listing 12.4 illustrates using a DataOutputStream and DataInputStream to write Java primitives and read them from a file.
Listing 12.4 Using DataInputStream and DataOutputStream (FileDriver4.java)
import java.io.*; class PrimitiveProcessor { DataInputStream dis; DataOutputStream dos; // Data for output. int outDay1 = 5; float outTemp1 = 75.5F; String cityName1 = "Milpitas"; float outTemp2 = 120.2F; String cityname2 = "Chicago"; // Data for input. int inDayNum = 0; float inTemp1 = 0.0f; float inTemp2 = 0.0f; String inCity1 = null; String inCity2 = null; String fileName = "Primitives.txt"; FileOutputStream fos; public PrimitiveProcessor() { try { fos = new FileOutputStream(fileName); dos = new DataOutputStream(fos); // You need an OutputStream for the constructor, so // wrap a FileOutputStream in a DataOutputStream. } catch(IOException ioe) { System.out.println("File " + fileName + "error" + ioe.getMessage()); } } public boolean writeTemperatureInfo() { try { dos.writeInt(outDay1); dos.writeFloat(outTemp1); dos.writeUTF(cityName1); dos.writeFloat(outTemp2); dos.writeUTF(cityname2); // There are specific write methods for every // Java primitive data type. dos.flush(); dos.close(); dos = null; // Flush the stream to force the data to disk. // Close the DataOutputStream so the file can be read. // Then set the object reference to null to
// encourage garbage collection. } catch(IOException e) { // Deal with any I/O-related errors // Almost all the methods of classes in java.io throw // exceptions System.out.println("Error writing file: " + e.getMessage()); } return true; } public boolean readTemperatureInfo() { try { FileInputStream fis = new FileInputStream(fileName); dis = new DataInputStream(fis); // "Wrap" the FileInputStream in a DataInputStream to // process data at a higher level of abstraction. // The DataInputStream constructor requires this. // Create a DataInputStream object on the file. // The physical file needs to exist when you do this. inDayNum = dis.readInt(); inTemp1 = dis.readFloat(); inCity1 = dis.readUTF(); inTemp2 = dis.readFloat(); inCity2 = dis.readUTF(); // Instead of having to read a certain // number of bytes manually to assume // int or float, you can simply read a whole // primitive, and Java will // extract the correct number of bytes from the file. // Note that you must extract or read the data types // in the same order you wrote them. If you write // a float and a char, // in that order, you must not do readChar() followed by // readInt() if you want // correct results. System.out.println("On day " + inDayNum + ", while it was"); System.out.println("a pleasant " + inTemp1 + " in " + inCity1); System.out.println("it was a nasty hot and muggy " + inTemp2); System.out.println("in " + inCity2); } catch(IOException ioe) { System.out.println("Error reading file: " + ioe.getMessage()); } return true; } } // End class definition PrimitiveProcessor
public class FileDriver4 { public static void main(String[] args) { PrimitiveProcessor pProcessor = new PrimitiveProcessor(); pProcessor.writeTemperatureInfo(); pProcessor.readTemperatureInfo(); System.exit(0); } } As this program illustrates, it is very common, and often required, that you wrap one kind of stream in another, because a DataOutputStream, for example, requires an InputStream object, or an object that is a subclass of InputStream, as input. As you can tell from the code, the DataInputStream and DataOutputStream classes are oriented towards writing Java primitives, plus methods for reading and writing strings. There are also methods for reading or writing the entire file at once, including all the primitives using a byte array. It might be useful to write primitives without writing whole objects, such as when you send a discrete value from a client to a server over a socket. You can use a DataOutputStream for that purpose. Reading and Writing Lines of Text: Code Example There are multiple techniques for writing and reading lines of text. This next example, Listing 12.5, shows a way to write out and then read in lines of text from a file. Although you can use stream classes for this, these are generally deprecated. You should use Reader and Writer classes instead, because they work at the Unicode character level, instead of the byte level, as discussed earlier.
Listing 12.5 Reading and Writing Lines of Text with BufferedReader and PrintWriter (FileDriver5.java)
import java.io.*; class TextProcessor { private FileWriter fw; private PrintWriter pw; private File textFile; private FileReader fr; private BufferedReader br; private String[] books = { "Rhetorica by Aristotle", "Against Apion by Josephus", "4QMMT author unknown", "Peloponesian War by Thucydides"} ; // Declare a String array of book titles from antiquity public TextProcessor() { //Do file initialization in the constructor try { textFile = new File("AncientTexts.txt"); // Define a new file fw = new FileWriter(textFile); // Connect the file to a FileWriter object for output pw = new PrintWriter(fw); // Wrap the FileWriter in a PrintWriter so you can write output // by lines, not characters
} catch(IOException e) { // There are many reasons this might fail, like the // write-protect tab on a diskette being set to // write-protect the disktte. System.out.println("Cannot create file because of " + e.toString()); } } public void writeOutput() { for (int i=0; i < books.length; i++) { // Step through the array, and write each // element to the file a s "line". pw.println(books[i]); } pw.flush(); // Force everything from the buffer to the physical file. pw.close(); // Close the writer. } public void readInput() { try { // Read the file into memory, one "line" at a time. // For this to work, the data in the file must be // organized as "lines" with carriage returns. fr = new FileReader(textFile); br = new BufferedReader(fr); // Open the file with a FileReader and wrap the // character-oriented reader in a line-oriented reader. for(int i =0; i < books.length; i++) { String line = br.readLine(); // Read a line and print it. System.out.println(line); } } catch(IOException e) { System.out.println("Read failed because of " + e.toString()); } } } // End class definition TextProcessor. public class FileDriver5 { public static void main(String args[]) {
TextProcessor textP = new TextProcessor(); textP.writeOutput(); textP.readInput(); System.exit(0); } } This program uses two main Reader and Writer classes: BufferedReader and PrintWriter. BufferedReader can read lines of text. The PrintWriter class is like the PrintStream class, except that PrintWriter works at the Unicode character level, rather than the byte level, as does the PrintStream class. Note For those with a C/C++ background who are looking for a way to produce formatted output with some method such as println(), Java does not support this task the way C does. There is no equivalent for the C function printf().
Tip You can use the BufferedWriter to make it easier to read a String from the command line as well. You wrap System.in in an InputStreamReader, which converts from bytes to chars, and then wrap the InputStreamReader in a BufferedReader object. Then you can read the whole command line at once by using the readLine() method of the BufferedReader class.
Reading and Writing Objects Java provides the capability to write an entire object, not just its pieces, to a stream so that you can send a whole object across the network. Tip Because Vectors allow you to insert any number of disparate objects, Vectors are often used to send data across a network. Usually, the client and server programs have agreed on the format of the Vector in terms of which slot holds which element, but that decision is up to you. This procedure is often used in e-commerce applications to send customer information in one object.
Object Serialization The mechanism that Java uses to send an entire object is called object serialization. This is a key technology in Java, and it shows up in other places, such as RMI. It can also be used to make an object persistent. When you serialize an object, Java creates a special version of your object, a copy of it, which includes information on each field. If you write an object to disk, and then view the file, you will see things like "java.lang.String" followed by the value of a String that is in your object. Java not only copies your object, but any dependent objects that your object contains as well. Caution Object serialization is a handy feature, but it should be used carefully. Serializing huge objects with lots of dependent objects can have a serious negative impact on network throughput.
Using object serialization, you can write an object to a stream and have another object read the serialized object in as
an object, not just a set of primitives or unrelated fields. Java ships the data members, but not the method code of your object. The receiving system must be able to locate the class definition. Caution Because a serialized object is a copy, changes you make to an object you read from a stream are not propagated to the original object without additional coding on your part.
In order to be allowed to serialize an object, you must implement the Serializable interface. The good news is that this interface contains no methods. All you actually have to do is put this interface in the class signature:
public class Book implements Serializable Most classes in the Java distribution are serializable. A few classes, such as Thread, are not (because it would make no sense to have them serializable). In addition, you can decide if a specific field in your class should not be serialized. There are at least two reasons not to serialize:
q q
If it makes no sense to serialize it (for example, a temporary String whose value need not be saved). If it is sensitive data you do not want exposed publicly. Serialization makes no distinction between public and private data members, so you might not want to serialize the credit card numbers of your customers.
To make a specific field within your object unserializable, you mark it as transient.
class Customer implements Serializable { private transient String creditCardNumber; private String CustName; } In this small class, when you serialize an object of type Customer, Java will send a copy of the current value of the custName String, but will only provide a place marker for the value of creditCardNumber, because the latter is marked as transient. Incidentally, static data members are not copied, but given default values when they are turned back into an object by the object reading the object stream. ObjectOutputStream and ObjectInputStream: Code Example There are two classes specifically designed for dealing with objects, ObjectOutputStream and ObjectInputStream. These streams can deal with objects and primitives. If all you need to do is deal with primitives, however, you should use DataInputStream and DataOutputStream because of the extra overhead incurred with an object stream. Listing 12.6 shows how to write an object to a stream and how to read an object from a stream.
Listing 12.6 Writing and Reading Objects Using ObjectOutputStream and ObjectInputStream (FileDriver6.java)
// Reading and Writing Objects in Java. import java.io.*; class ObjectProcessor { FileInputStream fis; FileOutputStream fos; ObjectInputStream ois; ObjectOutputStream oos;
Book theBook; //Serializable object String fileName = "Objects.txt"; // File for making serialized // object persistent. public ObjectProcessor() { try { fos = new FileOutputStream(fileName); oos = new ObjectOutputStream(fos); // You need an OutputStream for the constructor, so // wrap a FileOutputStream in an ObjectOutputStream. } catch(IOException ioe) { System.out.println("File " + fileName + "error" + ioe.getMessage()); } theBook = new Book("Lewis", "C.S.", "The Chronicles of Narnia", "PR6023.E926 C5", 5); // Create a Book object. This is serializable. } public boolean writeBookToStream() { try { oos.writeObject(theBook); // To write the object to a stream // just call writeObject with a reference // to a serializable object - pretty easy! oos.flush(); oos.close(); // Flush the stream to force the data to disk. // Close the DataOutputStream so the file can be read. } catch(IOException e) { // The object might not be serializable, or there might // be any number of file problems. System.out.println("Error writing object: " + e.getMessage()); } return true; } public boolean readBookFromStream() { try { FileInputStream fis = new FileInputStream(fileName); ois = new ObjectInputStream(fis); Book someBook = (Book)ois.readObject(); // Create an ObjectInputStream, wrapping a // FileInputStream. // Then do a readObject() call. This returns an object.
// In order to get it into the proper format for a Book // object, you must cast it to the data type Book. System.out.println(someBook.toString()); // Call the overridden version of toString for this // class. // Most, if not all, user-defined classes really ought // to have a toString() method defined. ois.close(); } catch(IOException ioe) { System.out.println("Error reading object: " + ioe.getMessage()); } catch(ClassNotFoundException cnfe) // This exception is thrown if the object that reads in // the serialized object can't load the class // definition via a class loader. { System.out.println("Error loading Book class: " + cnfe.getMessage()); } // The recipient object's JVM must be able to locate the class // of the object you are trying to read. return true; } } // End class definition ObjectProcessor. public class FileDriver6 { public static void main(String[] args) { ObjectProcessor op = new ObjectProcessor(); op.writeBookToStream(); op.readBookFromStream(); System.exit(0); } } class Book implements Serializable { // This class is marked as implementing the Serializable // interface, which has no methods. This is a flag to show the // objects instances of this class can be serialized. private String lastName; private String firstName; private String title; private String callNumber; private int numberOfCopies; public Book( String l, String f, String t, String c, int n) { // Perform explicit initialization in the constructor. lastName = l; firstName = f; title = t;
callNumber = c; numberOfCopies = n; } public String toString() { // This is a custom toString() method. It lets you // print the Book object in a meaningful form. String temp = "Author last name: " + lastName +"\n\r" + "Author First name: " + firstName + "\n\r" + "Title: " + title + "\n\r" + "CallNumber: " + callNumber + "\n\r" + "Copies: " + numberOfCopies; return temp; } } // End of class definition for Book. This program has three classes, a driver class, FileDriver6, a serializable class, Book, and a class that writes Book objects to a stream and reads them back from the file through an ObjectInputStream. Note the use of the overridden toString() in the Book class. Although this approach has not been used in the book, it is usually a good idea for a class to provide its own toString() method because the implementation from Object is probably not what you want to print for the String representation of an object. For more on this, see the discussion on canonical objects at http://www.artima.com/designtechniques/canonical.html. You can use ObjectInputStream and ObjectOutputStream to write any variety of objects, because the stream does not care about what kind of object is passed over it. You can serialize a Customer object, an Account object, or a Payment object. You can then mix them with primitives, if that is appropriate, for example, with an int to tell how many instancesof a given class to expect.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
reading writing text reading 2nd 3rd writing 2nd 3rd command lines Strings reading data types primitive I/O, performing 2nd 3rd 4th 5th DataInputStream I/O code to perform 2nd 3rd 4th DataOutputStream I/O code to perform 2nd 3rd 4th fields in objects transient unserializable transient FileDriver6 class FileInputStream I/O code to perform 2nd 3rd FileOutputStream I/O code to perform 2nd 3rd I/O Object serialization 2nd 3rd objects reading and writing performing byte-level 2nd 3rd DataInputStream, code 2nd 3rd 4th DataOutputStream, code 2nd 3rd 4th FileInputStream, code 2nd 3rd FileOutputStream, code 2nd 3rd Object serialization 2nd 3rd ObjectInputStream, code 2nd 3rd 4th ObjectOutputStream, code 2nd 3rd 4th objects, reading and writing primitive data types 2nd 3rd 4th 5th text, reading and writing 2nd 3rd text reading and writing 2nd 3rd interfaces Serializable methods printf() println() readUTF() 2nd toString() writeUTF() 2nd methodsd readLine() Object serialization 2nd 3rd ObjectInputStream I/O code to perform 2nd 3rd 4th ObjectOutputStream I/O
code to perform 2nd 3rd 4th objects canonical Web site fields transient unserializable reading Vectors writing performing I/O byte-level 2nd 3rd DataInputStream, code 2nd 3rd 4th DataOutputStream, code 2nd 3rd 4th FileInputStream, code 2nd 3rd FileOutputStream, code 2nd 3rd Object serialization 2nd 3rd ObjectInputStream, code 2nd 3rd 4th ObjectOutputStream, code 2nd 3rd 4th objects, reading and writing primitive data types 2nd 3rd 4th 5th text, reading and writing 2nd 3rd primitive data types I/O performing 2nd 3rd 4th 5th printf() method println() method PrintStream class PrintWriter class Reader class wrapping reading objects Strings from command lines text 2nd 3rd readLine() method readUTF() method 2nd Serializable interface serialization Object 2nd 3rd streams wrapping 2nd Strings reading from command lines strings UTF text reading 2nd 3rd writing 2nd 3rd toString() method transient fields UCS (Universal Character Set) UCS Transformation Format. Universal Character Set. UTF (UCS Transformation Format) strings Vectors objects
Web sites canonical objects wrapping Reader class streams 2nd Writer class Writer class wrapping writeUTF() method 2nd writing objects text 2nd 3rd
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229190144136115002222173109
Learning about a class's name and data members Learning about a class's methods Learning about an Interface Working with data and methods unknown to your code until runtime
Reflection is fairly straightforward to use; there is not much in the way of theory needed to use the Reflection APIs to discover information about an object. For every class in your application, the JRE provides an immutable Class object, which provides information about the class. You use the Reflection APIs with the Class object to obtain Constructor, Method, and Field (data member) objects. These objects can be used to retrieve information about the corresponding constructors, methods, and fields. A Class object might also be used to retrieve information about a Java interface, such as its methods and public, final, static data members.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
Safari | PURE Java 2 -> Discovering the Name and Attributes of a Class
All Books
Search
Java > PURE Java 2 > 13. Reflection > Discovering the Name and Attributes of a Class < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229190144136115014197008031
Use the getClass() method on an instance of an unknown classfor example, Class class = someInstance.getClass(); Use the .class suffix on a known classfor example, Class class = java.awt.TextEvent.class; Use the Class.forName method with a String that contains the name of the classfor example, Class class = Class.forName(stringNameOfClass);
The best approach to use depends upon whether the class is known at compile time (use the .class suffix), is known only at runtime (use Class.forName()), or is a mystery object reference (getClass()). Using Reflection to Retrieve Class Information: Code Example Listing 13.1 shows a program that retrieves basic information about a class, including its name, superclass, modifiers, and data members. In this specific case, I am going to use reflection on the java.awt.Frame class, but any class, including user-defined classes, can be used.
Listing 13.1 Displaying Class Name and Attributes Using Reflection APIs (Reflector1.java).
import java.lang.reflect.*; import java.awt.*; class ClassBrowser1 { Frame fr; public ClassBrowser1() { // Instantiate a Frame object. fr = new Frame("Reflecting Frame"); } public boolean browse() { // While I didn't do it here, you could just as easily // code this method as "browse(Object someObject)" // and use Reflection to discover the class of // "someObject." // Get the class object using an object instance. Class theClass = fr.getClass();
Safari | PURE Java 2 -> Discovering the Name and Attributes of a Class
// Get the superclass Class object. Class superClass = theClass.getSuperclass(); // Print the Name of the class and its superclass. System.out.println("Class name is " + theClass.getName()); System.out.println("Superclass name: " + superClass.getName()); // Get class modifiers. // The class java.lang.reflect.Modifier has methods to test for // several class modifiers and to list them all together. int mods = theClass.getModifiers(); if(Modifier.isPublic(mods)) System.out.println("Class is public"); if(Modifier.isProtected(mods)) System.out.println("Class is protected"); if(Modifier.isPrivate(mods)) System.out.println("Class is private"); // Another example. Class compClass = java.awt.Component.class; int compMods = compClass.getModifiers(); System.out.println(Modifier.toString(compMods)) ; // Get the interfaces for a given class. // Since java.awt.Frame does not implement multiple // interfaces, here's a class that does. Class serverClass = java.rmi.server.RemoteObject.class; Class [] remoteInterfaces = serverClass.getInterfaces(); // Now that you have the class'interfaces, print them. System.out.println("Class " + serverClass.getName() + "has these interfaces: for(int i = 0; i < remoteInterfaces.length; i++) System.out.println(remoteInterfaces[i].getName()); // Now show the fields of an object. Field[] classFields = theClass.getFields(); for(int j = 0; j < classFields.length; j++) { System.out.println("Field Name: " + classFields[j].getName()); System.out.println("Field data type: " + classFields[j].getType().getName()); // use getType() to get a field's data type // and make in human-friendly with getName(). int fieldMods = classFields[j].getModifiers(); // Get the modifiers for fields, like // final or static. System.out.println("Field modifiers: " + Modifier.toString(fieldMods)); } // getFields() returns the fields or data members of a class. // getType() returns their data type or class. // getModifiers() return info about them. return true; } }
");
Safari | PURE Java 2 -> Discovering the Name and Attributes of a Class
public class Reflector1 { public static void main(String[] args) { ClassBrowser1 cb = new ClassBrowser1(); cb.browse(); System.exit(0); } } Before you run this program, be aware that it produces more than one screen of output, so you might want to redirect the output to a file or pipe it to "more." In this example, there are a number of different reflection methods used. The most important is getClass(). This returns a Class object. To get the name of the class for which the Class object exists, you need to call the getName() method on the Class object. You can get the superclass by calling getSuperclass(). Tip By using the combination of getSuperclass() and getName(), you can walk your way up the class hierarchy for any class and list the names of the superclasses.
You can use getModifiers to get an int value that contains flags for the modifiers of the source Class object. You can use this to determine if a class is final, abstract, and so forth. You can use the getFields() method to get the data members or fields of a Class. The getType() method returns the data type of the data member, whereas getModifiers, when used with a field, returns an int value that describes the modifiers for the field. In the case of both a class and a field, you interrogate the results of getModifiers() with static final values in the java.lang.reflect.Modifier class.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
Safari | PURE Java 2 -> Discovering the Name and Attributes of a Class
getModifiers() method getName () method getName() method getSuperclass() method getType() method interfaces Class object code to obtain 2nd 3rd obtaining 2nd methods Class.forName getClass() 2nd getFields() getModifiers() getName() 2nd getSuperclass() getType() names of classes discovering 2nd objects Class code to obtain 2nd 3rd obtaining 2nd Reflection Class object code to obtain 2nd 3rd obtaining 2nd classes attributes, code to obtain 2nd 3rd attributes, discovering 2nd names, code to obtain 2nd 3rd names, discovering 2nd suffixes .class
2002, O'Reilly & Associates, Inc.
Safari | PURE Java 2 -> Getting Constructor and Method Information with Reflection
All Books
Search
Java > PURE Java 2 > 13. Reflection > Getting Constructor and Method Information with Reflection < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229190144136112013178123214
import java.lang.reflect.*; import java.awt.*; class ClassBrowser2 { Frame fr; public ClassBrowser2() { fr = new Frame("Reflecting Frame"); } public boolean browse() { // Get the class object using an object instance. Class theClass = fr.getClass(); // Get the constructor(s) of the class. Constructor[] theConstructors = theClass.getConstructors(); // Print the Name of the class and its constructor(s). System.out.println("Class name is " + theClass.getName()); String className = theClass.getName(); for(int i = 0; i < theConstructors.length; i++) { System.out.print(className + "("); Class[] theParmTypes = theConstructors[i].getParameterTypes(); for(int j = 0; j < theParmTypes.length; j++) { String parm = theParmTypes[j].getName(); System.out.print(parm); if(theParmTypes.length > 1
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=98 (1 of 3) [6/2/2002 10:11:31 PM]
Safari | PURE Java 2 -> Getting Constructor and Method Information with Reflection
&& theParmTypes.length > j+1) System.out.print(", "); // Print the Constructor signature with good formatting. } System.out.println(")"); System.out.println(" "); } // End of for loop for number of Constructor objects. // Now print the methods. System.out.println("Methods"); Method[] theMethods = theClass.getMethods(); for(int i = 0; i < theMethods.length; i++) { int mods = theMethods[i]. getModifiers(); System.out.print(Modifier.toString(mods)); // First print the method's modifiers. System.out.print(" " + theMethods[i].getReturnType().getName()); // Next print the return type. System.out.print(" " + theMethods[i].getName() + "( "); // Print the method name. Class[] theParmTypes = theMethods[i].getParameterTypes(); // Get an array of the parameters, if any, // for a given method. if(theParmTypes.length == 0) System.out.println(")"); else { for(int j = 0; j < theParmTypes.length; j++) { // Walk through the array of // parameters. Get the parm's // data type, and print the type. // Provide for the situation that there // might be multiple parameters // so that the print-out is formatted // nicely. String parm = theParmTypes[j].getName(); System.out.print(parm); if(theParmTypes.length > 1 && theParmTypes.length > j+1) System.out.print(", "); } System.out.println(")"); } // End else block. } // End outer for loop. return true; } } public class Reflector2 { public static void main(String[] args) { ClassBrowser2 cb = new ClassBrowser2(); cb.browse();
Safari | PURE Java 2 -> Getting Constructor and Method Information with Reflection
System.exit(0); } } This program shows the use of the getConstructors()method to retrieve a class's constructors and the getMethods() method to retrieve the methods of a class. It shows the use of getParameterTypes() to see the parameters of the constructors and methods, getReturnType() to retrieve the return type of a method, and getModifiers() to get the modifiers for a method. This particular example uses Frame. If you want a shorter output, you can use another class, such as Panel.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 13. Reflection > Discovering Interface Information < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229190144136112009150011210
import java.lang.reflect.*; class ClassBrowser3 { public boolean browse() { // Get the class object for an interface. Class theClass = java.sql.Connection.class; // Get the interface's name. System.out.println("Class name is " + theClass.getName()); String className = theClass.getName(); // Print the interface's modifiers. int mods = theClass.getModifiers(); System.out.println(Modifier.toString(mods)); // Get the interface's fields, if any. Field[] theFields = theClass.getFields(); for(int j = 0; j < theFields.length; j++) { System.out.println("Field Name: " + theFields[j].getName()); // Use getFields() to obtain an array of the fields, // if any, in the interface. You can then get the modifiers. // Of course, if you know the rules for a Java interface, you // know that the fields can only be public static final, // regardless of their data type. System.out.println("Field data type: " + theFields[j].getType().getName()); // Get all the modifiers for each field int fieldMods = theFields[j].getModifiers(); System.out.println("Field modifiers: " + Modifier.toString(fieldMods)); }
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=99 (1 of 3) [6/2/2002 10:11:36 PM]
// getFields() returns the fields or data members of a class. // getType() returns their data type or class. // getModifiers() return info about them. System.out.println("The interface has the following methods"); // Now print the methods. Method[] theMethods = theClass.getMethods(); // As with a class, getMethods() returns the methods of // the interface. for(int i = 0; i < theMethods.length; i++) { int theMods = theMethods[i].getModifiers(); // Get the modifiers of each method System.out.print(Modifier.toString(theMods)); // First print the method's modifiers. System.out.print(" " + theMethods[i].getReturnType().getName()); // Next print the return type. System.out.print(" " + theMethods[i].getName() + "( "); // Print the method name. Class[] theParmTypes = theMethods[i].getParameterTypes(); // Get the parameter types of each method if(theParmTypes.length == 0) System.out.println(")"); else { for(int j = 0; j < theParmTypes.length; j++) { String parm = theParmTypes[j].getName(); System.out.print(parm); if(theParmTypes.length > 1 && theParmTypes.length > j+1) System.out.print(", "); } System.out.println(")"); } // End else block. } // End outer for loop. return true; } } public class Reflector3 { public static void main(String[] args) { ClassBrowser3 cb = new ClassBrowser3(); cb.browse(); System.exit(0); } }
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
Safari | PURE Java 2 -> Modifying Data Members and Calling Methods
All Books
Search
Java > PURE Java 2 > 13. Reflection > Modifying Data Members and Calling Methods < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229190144136113017060026158
A zero-argument constructorUse the newInstance() method, catching the NoSuchMethodException (in case there is no zero-argument for the class). A constructor that takes argumentsCreate a Constructor object and then call the newInstance() method on the Constructor object.
Class Manipulation: Code Example Once you've got all the information on a class, you can use it to manipulate an instance of the class. Listing 13.4 shows how to construct an object, using our old friend java.awt.Frame, and then modify field values and calling methods.
Listing 13.4 Using the Reflection APIs to Create and Manipulate Objects (Reflector4.java)
import java.lang.reflect.*; import java.awt.Color; class ClassBrowser4 { Class theClass ; Object object; public boolean manipulateObject(String className) { // Get the class object using Class.forName(). try { theClass = Class.forName(className); // Print the Name of the class. System.out.println("Class name is " + theClass.getName()); object = theClass.newInstance(); } catch(InstantiationException ine) { // This means you failed to create an instance. // This might be due to security constraints, // memory constraints, or other causes.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=100 (1 of 4) [6/2/2002 10:11:43 PM]
Safari | PURE Java 2 -> Modifying Data Members and Calling Methods
System.out.println("Instantiation error for " + className + ": " +ine.getMessage()); } catch(IllegalAccessException iae) // You might not have the permissions to access // the class or a member or method of it. This // might depend on the reflection permission. { System.out.println("Illegal Access error for " + className + ": " +iae.getMessage()); } catch(ClassNotFoundException cnfe) // The class loader could not find your class definition. { System.out.println("Class not found " + className + ": " +cnfe.getMessage()); } // Now get and set field values. // This requires the use of the get() and set() methods // of the java.lang.reflect.Field class. These are not // methods of the target object, in this case, a Frame. Field xValue; int xInt = 50; try { xValue = theClass.getField("x"); System.out.println("The value of the field is: " + xValue.getInt(object)); xValue.setInt(object, xInt); System.out.println("The value of the field is: " + xValue.getInt(object)); // Get the specific field you are interested in. // Print its value. // Set it to a new value. } catch(NoSuchFieldException nsfe) // The field doesn't exist. Remember, this // is runtime discovery, so the compiler // doesn't know if the field is spelled right, // and so forth. { System.out.println ("No such field: x " + nsfe.getMessage()); } catch(IllegalAccessException Ille) { System.out.println("Illegal access: x " + Ille.getMessage()); } // Change to another class. java.awt.Frame fr = new java.awt.Frame(); theClass = fr.getClass(); // Get a class object for the Frame object.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=100 (2 of 4) [6/2/2002 10:11:43 PM]
Safari | PURE Java 2 -> Modifying Data Members and Calling Methods
Class[] parmTypes = new Class[] { String.class} ; // Set up a list of parms. setTitle() takes one String. Object[] args = new Object[] { "New Frame"} ; try { Method setTitleMethod = theClass.getMethod("setTitle", parmTypes); // Create a callable method statement. setTitleMethod.invoke(fr, args); // Call the setTitle() method on the // Frame object. } catch(Exception excp) { System.out.println("Failed in method call: " + excp.getMessage()); } fr.setSize(200,100); fr.setBackground(Color.blue); fr.setVisible(true); // Directly call some methods on the same // Frame object, setting its size, background // and visibility. try { Thread.currentThread().sleep(4000); } catch(InterruptedException inte) // Keep the Frame visible briefly so you can see the new title. {} return true; } } public class Reflector4 { public static void main(String[] args) { ClassBrowser4 cb = new ClassBrowser4(); cb.manipulateObject("java.awt.Point"); System.exit(0); } } This program shows how to do two things: get/set field values and call methods. Although the program has some knowledge of what variables and methods are available, you could, without much work, modify it to be completely blind to the available methods and data members. That would make it suitable for a GUI builder or debugger that does not know at compile time what kind of objects it will encounter in your code. The program starts off getting and setting a data member, x, in an object of type java.awt.Point. Then the program changes to work with java.awt.Frame. The reason for the change is that, although a Point object contains easy-to-access data members, Frame does not. On the other hand, it's easier to show the result of a method call on a Frame than on a Point, and the logic used here might be needed in a GUI builder tool when the user wants to change the title on the Frame. The major differences in this program from earlier examples in the chapter is the use of get and set methods on
Safari | PURE Java 2 -> Modifying Data Members and Calling Methods
an object. The java.lang.reflect.Field class provides several get and set methods. They cover getting and setting primitive values, such as getBoolean() and get(), for getting and setting values that require objects, such as a Button or a Date. Note One data type not covered here is an Object that is an array. The procedures for dealing with arrays are not much different from those shown previously in this chapter, with the addition of a getComponent() method for getting the value of a given array element.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229190144136113024063073200
Java, by default, uses TCP/IP for its networking protocol. Although networking across heterogeneous network protocols is beyond the scope of this book, be aware that you might need to find ways to communicate from a Java program using TCP/IP to another network protocol, such as LU 6.2, a common IBM mainframe protocol. Actually, Java supports two kinds of socket communication: TCP (Transport Control Protocol) sockets and UDP (User Datagram Protocol) sockets. This chapter covers the following:
q q q q q
Core networking concepts Getting information on IP addresses and host names Creating a simple TCP/IP client and server application Creating a simple UDP client and server application Working with URLs to access data on the Web
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
All Books
Search
Java > PURE Java 2 > 14. Java Network Programming > Core Networking Concepts < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229190144136118242069208126
A host generally has a name. For example, I once had a system called Dagobah. Every host, however, must have an address. This address is an IP (Internet Protocol) address, consisting of four period-delimited numbers, such as 234.255.255.255. (This is a randomly chosen number and is not intended to correspond to any real system.) IP addresses are used to uniquely identify a given host on a network, including the Internet network. Note In practice, large companies have only one number for outsiders to connect to and that will internally route messages to a specific internal IP address. This is desirable because the number of available IP addresses would otherwise quickly be exhausted.
A common mechanism for mapping between the host name and the host's IP address is domain name service (DNS). The next section will show this mapping in Java. You might think of an IP address as analogous to your company's phone number. If someone calls me at Sun through the main switchboard number, he won't reach me. He needs my phone extension, one way or another. My extension is analogous to a port number. Programs run on hosts at a given port number. A port is not a physical entity, like a place into which you plug cables. A port is a logical software construct. In fact, unlike my phone extension, which is assigned by the phone company (I assume), a port number is a random number chosen out of the air. As a rule, the lower numbers (up to about 1024) are reserved for well-known services. For example, SMTP, the email server, usually runs on port 25. HTTP, the protocol used by Web servers, generally runs on port 80. To be sure you are not in conflict with a system service, it is generally a good idea to pick a number greater than 2000. Other than that, the only real requirement is that all the parties in a network application agree on the same port number. For example, if the server program is running on Sisera at port 3456, all the clients trying to access the program using that port number must connect to the
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=103 (1 of 4) [6/2/2002 10:11:52 PM]
server program using the host name Sisera (or its equivalent IP address) and the port number 3456. Using another port will not work. It would be like trying to reach me at a nonexistent phone extension. To work with a port, you create a socket. A socket, like a port, is a logical software construct. Again, you can make TCP or UDP sockets. This chapter has examples of doing both. Before you see them, here is a basic guideline to use in deciding, if you have a choice, which type of socket and which type of communication to use.
q
If your application requires reliable host-to-host communication with failure notification if an error occurs, use TCP sockets. If your application can work with less than complete reliability, without error notification, use UDP sockets.
Caution Because many firewalls refuse to allow UDP packets through, UDP sockets are an inappropriate choice for an Internet-based application.
For example, in an e-commerce application, you want to be sure that messages go each way reliably and that you get notified of any network error. Therefore, you use TCP sockets for e-commerce, if you use sockets directly. If you are writing an application that sends the latest sport scores to client systems, it is probably not too serious an error if one message from the server does not arrive. If the next message is sent five minutes later, that is probably good enough. In this case, there's probably no reason to bear the much higher overhead of using TCP sockets. UDP sockets can be used. This is not to say that UDP sockets are unreliable, but they do not guarantee delivery of data in the same way a TCP socket does. They are not as reliable as TCP sockets only in terms of error notification. Note Many applications use sockets without even knowing it. For example, JDBC drivers (discussed in Chapter 17, "JDBC: Java Database Programming" ) often use sockets. ORBs often use sockets to ORB initialization. RMI (discussed in Chapter 15, "RMI" ) uses sockets to connect to the RMIregistry (typically port number 1099) and to connect to remote objects (the socket number varies). Object buses typically use them as well. So you often use sockets, even when you did not explicitly create them. In fact, any network communication in Java uses sockets in some way, whether it is visible to you or not.
The main classes that support networking are in the package java.net, although you often need classes for socket I/O from java.io.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
sockets applications using creating networks programming TCP e-commerce UDP (User Datagram Protocol) TCP sockets e-commerce UDP packets firewalls
2002, O'Reilly & Associates, Inc.
Safari | PURE Java 2 -> Working with Host Names and IP Addresses
All Books
Search
Java > PURE Java 2 > 14. Java Network Programming > Working with Host Names and IP Addresses < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229188114123200191050081003
import java.io.*; import java.net.*; class Addressor { InetAddress theHost; public boolean displayInetData() { try { theHost = InetAddress.getLocalHost(); System.out.println("Local host: " + theHost); } catch(UnknownHostException uhe) { } // This returns information on your system. If Java cannot // find the local host, your host lacks an IP address currently. // When you try to access information on a host you may // get an UnknownHostException if it is not found. // To make this code work more places, it will only deal with // the Local host, i.e., your system running the code. String hostName = theHost.getHostName(); String hostAddress = theHost.getHostAddress(); // Obtain a host name and address. Since you can make // an InetAddress object from any host you can connect to, // You could get one of these values for any host to // which you can connect, as long as you have either // the name or the IP address. System.out.println("Host Name: " + hostName); System.out.println("Host Address: " + hostAddress);
Safari | PURE Java 2 -> Working with Host Names and IP Addresses
try { System.out.println("Host address by host name: " + InetAddress.getByName(hostName)); // The getByName() method returns an InetAddress object // which you could use to get the IP address with // or getAddress() or getHostAddress(). System.out.println("Host address as String: " + hostAddress.toString()); } catch(UnknownHostException unhe) { System.out.println("The local host is invalid."); } return true; } } public class NetDriver1 { public static void main(String[] args) { Addressor addr = new Addressor(); addr.displayInetData(); } } This program shows you how to easily manipulate IP addresses. If you want to use the logical name of a given system, but only have the IP address, you can use getByName(), and then getHostName() to retrieve the logical name.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
Safari | PURE Java 2 -> Working with Host Names and IP Addresses
methods getByName() getHostName() networks programming hosts names 2nd java.net.InetAddress 2nd programming networks host names 2nd java.net.InetAddress 2nd
2002, O'Reilly & Associates, Inc.
Safari | PURE Java 2 -> Creating a TCP Client and Server Application
All Books
Java > PURE Java 2 > 14. Java Network Programming > Creating a TCP Client and Server Application < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229188113198233228023107191
First, run the server program. The server program does the following: 1. Creates a ServerSocket 2. Calls the accept() method on that ServerSocket 3. Gets a connection from a client 4. Grabs a reference to the Socket object returned from the accept() call 5. Opens an InputStream and/or OutputStream on that socket 6. Communicates with the client The accept() call blocks until some program connects to the port number that is related to the ServerSocket. Tip Because the accept() method blocks, you might wish to put code into your server program that accepts some kind of input from an administrative client program. That program can tell the server program to shut down gracefully, instead of just terminating. For example, if a client sends the String "Bye", the server program exits.
When a client runs, it does the following: 1. Creates a socket, using the server name/IP address and port number. This automatically causes the client to connect to the server, provided the server is listening for a connection with the accept() method. 2. The client opens an InputStream and/or OutputStream on the Socket object.
Safari | PURE Java 2 -> Creating a TCP Client and Server Application
3. The client communicates with the server. Socket connections automatically generate an InputStream and OutputStream. You simply obtain a reference to this stream, which already exists. TCP Socket with Socket and ServerSocket: Code Example The nature of the communication between the client and the server is totally defined by the programmer. You have to decide what kind of data should go from the client to the server and back again. Many Java applications, such as e-commerce programs that use sockets, send Vectors back and forth. Because a Vector can accept virtually any type of data (except for primitives, unless they are wrapped in a wrapper class), you might create a Vector that has the customer name in element zero, the customer address in element one, the phone number in element two, and the variable number of items that the customer wishes to purchase in the next several elements. Java makes no requirements about the content of the communication, as long as you use data types that are valid for the type of stream you wrap around the InputStream or OutputStream. Listings 14.2 through 14.4 show a sample TCP server and client combination. The client and server are going to send each other objects using an ObjectInputStream and an ObjectOutputStream. In the programs, note the order of getting the InputStream and OutputStream from the socket. Caution The order between client and server is critically important. If you ignore this and, for example, have both the client and server perform a getInputStream() first, your program will simply hang forever on both sides. Both the server and any clients should call getOutputStream() before calling getInputStream().
The server program is set up to handle any number of clients because each client gets a new server-side thread assigned to it. Listing 14.2 shows the Server, Listing 14.3 shows the client, and Listing 14.4 shows the BookQuery class.
Listing 14.2 TCP Server Program Using Sockets and ServerSockets (NetDriver2a.java)
import java.io.*; import java.net.*; class TCPServer { InetAddress theServer; int port = 5555; ServerSocket srvSock; public TCPServer() { try { srvSock = new ServerSocket(port); // Create a ServerSocket. } catch(IOException ioe) { System.out.println ("Error creating ServerSocket: ioe.getMessage());
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=105 (2 of 9) [6/2/2002 10:29:20 PM]
" +
Safari | PURE Java 2 -> Creating a TCP Client and Server Application
} } public boolean processConnections() { boolean exit = false; while(exit == false) try { Socket sock = srvSock.accept(); // Call accept() in a loop in order to // easily handle client connections on separate Threads. new SocketHandler(sock); } catch(IOException ioe) { System.out.println ("Error creating Socket: } return true; } } class SocketHandler implements Runnable { Socket theSocket; public SocketHandler(Socket s) { theSocket = s; Thread thr = new Thread(this); thr.start(); // Run this client session through a separate Thread. } public void run() { try { ObjectOutputStream oos = new ObjectOutputStream( theSocket.getOutputStream() ); ObjectInputStream ois = new ObjectInputStream( theSocket.getInputStream() ); System.out.println("Opened object streams"); // Get the built-in InputStream and OutputStream // from the Socket. BookQuery bq = (BookQuery)ois.readObject(); // Read a BookQuery from the client's socket stream. System.out.println("Book query: " + bq.toString()); // Display the BookQuery object. String result = "Seven books found"; oos.writeObject(result); ois.close();
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=105 (3 of 9) [6/2/2002 10:29:20 PM]
" + ioe.getMessage());
Safari | PURE Java 2 -> Creating a TCP Client and Server Application
oos.close(); theSocket.close(); // Close the Socket. // This does not affect the ServerSocket. } catch(IOException ioex) { System.out.println ("Error reading from client socket: " + ioex.getMessage()); } catch(ClassNotFoundException cnfe) { System.out.println ("Error findingobject stream class: " + cnfe.getMessage()); } } } public class NetDriver2a { public static void main(String[] args) { TCPServer svr = new TCPServer(); svr.processConnections(); } }
Listing 14.3 TCP Client Program Using Sockets (NetDriver2b.java)
import java.net.*; class TCPClient { InetAddress theHost; Socket aSocket; public TCPClient() { // Get the host name to use in connecting to the Server. try { theHost = InetAddress.getLocalHost(); // Get the INetAddress of the host. String hostName = theHost.getHostName(); // Get the name of the host. aSocket = new Socket(hostName, 5555); // Open a Socket using the host name and // the pre-decided port number. } catch(UnknownHostException uhe) {
Safari | PURE Java 2 -> Creating a TCP Client and Server Application
System.out.println("Could not connect to server"); } catch(IOException ioe) { System.out.println("Error creating Socket " + ioe.getMessage()); } }
public boolean sendBookQuery() { try { ObjectOutputStream oos = new ObjectOutputStream( aSocket.getOutputStream()); // Obtain a reference to the Socket's // OutputStream. // Then wrap the stream in an // ObjectOutputStream so you can // write objects across the network. ObjectInputStream ois = new ObjectInputStream( aSocket.getInputStream()); // Obtain a reference to the InputStream // of the Socket and then // wrap the InputStream in an // ObjectInputStream so you can read // objects and not just primitives // from the Socket. BookQuery query = new BookQuery( BookQuery.AUTHOR_SEARCH, "Lewis, C.S."); oos.writeObject(query); // Write this BookQueryObject to the network. String response = (String)ois.readObject(); System.out.println("Search results: " + response); oos.close(); ois.close(); aSocket.close(); // First close the ObjectOutputStream and // ObjectInputStream, and then close the Socket. // This is cleaner than the client merely terminating. } catch(IOException ioe) { System.out.println("Error getting Socket streams."); } catch(ClassNotFoundException cnfe) // readObject can fail if the class definition // for what you want to read in cannot be found. { System.out.println("Error accessing a class " + cnfe.getMessage()); }
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=105 (5 of 9) [6/2/2002 10:29:20 PM]
Safari | PURE Java 2 -> Creating a TCP Client and Server Application
return true; } } public class NetDriver2b { public static void main(String[] args) { TCPClient client = new TCPClient(); // Make an instance of the client // and call its bookQuery method to simulate // searching a library catalog. client. sendBookQuery(); } }
Listing 14.4 BookQuery Class Used by the Client and Server (BookQuery.java)
// This class is used to submit a book query to the library server. import java.io.*; public class BookQuery implements Serializable { public static final int AUTHOR_SEARCH = 10; public static final int TITLE_SEARCH = 20; public static final int KEYWORD_SEARCH = 30; // It is common to use public static int fields to test // against, especially in switch statements, // which use integral types. This allows the // compiler to check what you are comparing to, // and guarantees consistency for // setting or comparing values. int searchType = 10; String author; String title; String keyword; public BookQuery(int sType, String sValue) // This is the constructor for the BookQuery class. { switch(sType) // This is applicatoin code to implement the catalog search. { case AUTHOR_SEARCH : { searchType = sType; author = sValue; break; } case TITLE_SEARCH : { searchType = sType;
Safari | PURE Java 2 -> Creating a TCP Client and Server Application
title = sValue; break; } case KEYWORD_SEARCH : { searchType = sType; keyword = sValue; break; } } // End switch statement. } public String >toString() { String temp = "Search type: "; String type = null; switch( searchType) { case AUTHOR_SEARCH : // Simulate an author search of a card catalog. { type = "Author search"; break; } case TITLE_SEARCH : // Simulate a title search of a library catalog. { type = "Title search"; break; } case KEYWORD_SEARCH : // Simulate searching a library catalog by keyword. { type = "Keyword search"; break; } } // End switch statement. // Concatenate a hypothetical result // of a card catalog search in a String. temp += " " + type + " Author: " + author + " Title: " + title + " Keyword: " +keyword; return temp; // Return the String, whatever its contents. } } To run this application, start the server first. The server creates a ServerSocket on port 5555 on the current server host, which, of course, can (and probably will) be different from the client system. The server then enters a loop in which it does an accept() call. Whenever a client connects to the server program (by creating a socket for the servers host at port 5555), the server program creates a separate object which runs on its own thread to handle the client request. There is no code in the server to shut down in response to a specific client request or server-side administrative GUI event, but (as noted earlier) you should put one in your server code.
Safari | PURE Java 2 -> Creating a TCP Client and Server Application
Whenever a client starts up, it creates a socket on the appropriate host and a port (the system the client wants to reach, which is probably not the client's host). The client then gets an OutputStream and an InputStream on the socket. In both the server and client, you can wrap the streams from the socket in any kind of stream and, optionally, the Reader or Writer class you wish to use. So the InputStream can be wrapped in an InputStreamReader and then a BufferedReader to read input one line at a time. You can send anything you wish over a socket connection, from raw bytes to complex objects. Caution If possible, avoid sending very large objects and quite complex objects. As noted in the discussion of serialization in Chapter 12, "I/O in Java," passing large or complex objects can cause performance problems.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
Safari | PURE Java 2 -> Creating a TCP Client and Server Application
clients order between ObjectInputStream ObjectOutputStream TCP applications creating 2nd 3rd 4th 5th 6th 7th 8th 9th 10th TCP programs sockets and ServerSockets 2nd 3rd ServerSockets TCP server programs 2nd 3rd sockets ServerSockets TCP client programs sockets 2nd 3rd TCP server programs 2nd 3rd TCP client and server applications creating 2nd 3rd 4th 5th 6th 7th 8th 9th 10th client programs sockets 2nd 3rd server programs sockets and ServerSockets 2nd 3rd Vectors
2002, O'Reilly & Associates, Inc.
Safari | PURE Java 2 -> Using Datagram Packets and UDP Sockets
All Books
Search
Java > PURE Java 2 > 14. Java Network Programming > Using Datagram Packets and UDP Sockets < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229188113198235150052172080
// Simple Datagram/UDP Server. import java.io.*; import java.net.*; class UDPServer { DatagramSocket socket = null; DatagramPacket sendPacket; DatagramPacket recvPacket; byte[] outBuffer; byte[] inBuffer; InetAddress clientAddress; int clientPort; public UDPServer() { try { socket = new DatagramSocket(5555); // Create a new Datagram socket on the server. inBuffer = new byte[20]; // Create a buffer to get client input. outBuffer = new byte[500]; System.out.println( "Library server started on port 5555");
Safari | PURE Java 2 -> Using Datagram Packets and UDP Sockets
} catch(SocketException se) { System.out.println("Error creating server datagram socket: " + se.getMessage()); } } public boolean getClientRequests() { boolean loop = true; while(loop == true) { try { recvPacket = new DatagramPacket(inBuffer, inBuffer.length); // Create a DatagramPacket to receive client requests. socket.receive(recvPacket); // Wait for a client request. This code // blocks until it gets input. clientAddress = recvPacket.getAddress(); clientPort = recvPacket.getPort(); // Extract from the DatagramPackt where it came from. String text = new String(recvPacket.getData(), 0, recvPacket.getLength()); // Extract the contents of the DatagramPacket. if(text.equals("bye")) { loop = false; break; } // Provide a way for clients to stop // the server gracefully. String msg = "The book named _Till We Have Faces_" + "is overdue by five days"; outBuffer = msg.getBytes(); // Put the bytes in a byte array. sendPacket = new DatagramPacket(outBuffer, outBuffer.length, clientAddress, clientPort); // Create a DatagramPacket to send // back to the client, using the address and port // of the client, which was extracted // from the DatagramPacket the client sent. socket.send(sendPacket); // Send the DatagramPacket to the // client and start again. } catch(IOException ioe) { System.out.println("Error with Packets: " + ioe.getMessage()); } }
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=106 (2 of 5) [6/2/2002 10:29:44 PM]
Safari | PURE Java 2 -> Using Datagram Packets and UDP Sockets
// Since any kind of socket does I/O, it can // get an I/O exception. socket.close(); return true; } } // End class definition for UDPServer. public class NetDriver3a { public static void main(String[] args) { UDPServer server = new UDPServer(); boolean result = server.getClientRequests(); // Create a UDPServer object and have it wait on // client requests. if(result == true) System.out.println("Library server failed"); System.exit(0); } } Here's the client:
Listing 14.6 Using Datagram Sockets and Packets in a UDP Client (NetDriver3b.java)
// Simple Datagram/UDP Client. import java.io.*; importf java.net.*; class UDPClient { DatagramSocket socket = null; DatagramPacket sendPacket; DatagramPacket recvPacket; byte[] outBuffer; byte[] inBuffer; InetAddress serverAddress; int serverPort = 5555; public UDPClient() { try { socket = new DatagramSocket(); // Create a new Datagram socket. // The client does not need to use a specific // port number since this socket is on // the client machine. inBuffer = new byte[500]; // Create a buffer to get server input. outBuffer = new byte[20]; } catch(SocketException se) { System.out.println("Error creating server datagram socket: " +
Safari | PURE Java 2 -> Using Datagram Packets and UDP Sockets
se.getMessage()); } } public boolean getBookStatus() { try { String msg = "23456"; outBuffer = msg.getBytes(); // Put the bytes in a byte array. serverAddress = InetAddress.getLocalHost(); sendPacket = new DatagramPacket(outBuffer, outBuffer.length, serverAddress, serverPort); // Create a DatagramPacket to send // to the server, using the LocalHost address // and the port number of the server. socket.send(sendPacket); // Send the library card number to the server. recvPacket = new DatagramPacket(inBuffer, inBuffer.length); // Create a DatagramPacket to receive // the server's response. socket.receive(recvPacket); // Wait for a response from the server. // The client blocks until it gets input from the server. String text = new String(recvPacket.getData(), 0, recvPacket.getLength()); // Extract the contents of the DatagramPacket. System.out.println("Response from Server: " + text); socket.close(); } catch(IOException ioe) { System.out.println("Eror with Packets: " + ioe.getMessage()); } return true; } } // End class definition for UDPServer. public class NetDriver3b { public static void main(String[] args) { UDPClient client = new UDPClient(); boolean result = client.getBookStatus(); if(result == false) System. out.println("Library server failed"); System.exit(0); }
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=106 (4 of 5) [6/2/2002 10:29:44 PM]
Safari | PURE Java 2 -> Using Datagram Packets and UDP Sockets
}
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 14. Java Network Programming > Working with URLs in Java < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229188113198236007225145116
A protocol, such as HTTP, ftp, or RMI A unique address for some resource or resources
For example, the URL http://www.javasoft.com specifies the HTTP protocol and http://www.javasoft.com specifies resource address. The latter resource address should be unique among URL addresses. A URL object in Java always represents an absolute URL address, even if it is made using a relative URL address. A URL object provides methods to extract the host name, the directory, and so forth. If you want to use the URL for more than just reading data, you can call the URL class's openConnection() method. It is simple to make a URL object:
URL suned = new URL("http://suned.sun.com"); This creates the URL object suned. This is an absolute URL. That is, it contains enough information to get to the resource without any other information. It is also possible to create a URL object with a relative URL. For example:
URL suned = new URL ("http://suned.sun.com"); URL sunedUSA = new URL(suned, "/usa/centers.html"); The sunedUSA URL object is formed by adding a relative URL "/USA/centers.html" to an absolute URL "http://suned.sun.com". In the constructor, suned specifies a URL object that represents the base, absolute URL. The second field represents the additional directory and filename information being added to the absolute path. You can also specify a port number or a reference value. If you do not have the complete URL as a string, but have parts of it (such as the protocol, host name, and port number), there are two other constructors that take either parts of a URL or the individual pieces (that is, protocol, host name, port, directory, and filename). Note These are "write once, read many times" objects. After you have created them, you can get values of specific parts of a URL object, but you cannot modify them.
All the constructors for a URL object can throw a MalformedURLException. This means that the protocol for the URL is null or unknown; for example, htpt would cause a MalformedURLException.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=107 (1 of 5) [6/2/2002 10:29:53 PM]
As you will see in the sample code below, there are several methods for accessing the components of a URL object, such as getHost() and getPort(). You can read from or write to a URL. You can read either by just getting an InputStream from the URL object or by creating a URLConnection object, which will let you write to the URL as well. In the sample program here, the code uses a cgi-bin script, set up by Sun, to let you test your ability to write to a URL. You can read back the contents of the URL to get your input, which is reversed by the cgi-bin script, called backwards. This illustrates the capability of your code to do a POST operation, for example, to a CGI script, or, much better, to a servlet, directly using the HTTP protocol. A URLConnection object provides four main ways to access a URL:
q q q q
There are several commonly used methods for obtaining header information:
q q q q q q
Listing 14.7 shows a sample program that demonstrates how to create a URL object, interrogate it, read directly from a URL with the URL object, and read from and write to a URL with a URLConnection object.
Listing 14.7 Accessing URLs with the URL, URLConnection, and URLConnection Classes (NetDriver4.java)
// Simple URL handling program. import java.net.*; import java.io.*; class URLHandler { URL url; public URLHandler() { try { url = new URL("http://suned.sun.com/usa/centers.html"); } catch(MalformedURLException mal) // This exception occurs for invalid // or syntactically wrong URLs. { System.out.println("Bad Protocol: " + mal); } }
public boolean displayURLInfo() { try { System.out.println("Protocol: " + url.getProtocol()); System.out.println("Host name: " + url.getHost()); System.out.println("Port Number: " + url.getPort()); System.out.println("File name: " + url.getFile()); System.out.println("Reference: " + url.getRef()); // Print the contents of the URL. BufferedReader br = new BufferedReader( new InputStreamReader( url.openStream())); // Open an InputStream on the URL location. String urlContent = null; while ((urlContent = br.readLine()) != null) System.out.println(urlContent); br.close(); // Here's an alternative, HTTP-specific way to // work with a URL: URLConnection. URLConnection urlConn = url.openConnection(); // You could also use an HttpURLConnection. BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( urlConn.getInputStream())); while ((urlContent = bufferedReader.readLine()) != null) System.out.println(urlContent); bufferedReader.close(); // // // // // // // Now, create a URLConnection and write to the URL You would use this technique to do a POST, for example, to send data to a CGI script or, much better, a Servlet. This code uses a pre-made CGI script at the Javasoft website so that every reader can execute the code. String backwardsString = URLEncoder.encode ("Library card number:
12345");
URL cgiURL = new URL ("http://java.sun.com/cgi-bin/backwards"); URLConnection conn = cgiURL.openConnection(); // Create a URL object for the cgi script URL. // Then connect to the URL with a URLConnection. conn.setDoOutput(true); PrintWriter pw = new PrintWriter(conn.getOutputStream()); pw.println("string=" + backwardsString); pw.close(); // This wrote a String to the URL. BufferedReader buff = new BufferedReader( new InputStreamReader( conn.getInputStream()));
while ((urlContent = buff.readLine()) != null) System.out.println(urlContent); buff.close(); } catch(IOException ioe) { System.out.println("Error reading URL: " + ioe.getMessage()); } return true; } } public class NetDriver4 { public static void main (String[] args) { URLHandler handler = new URLHandler(); boolean result = handler.displayURLInfo(); System.exit(0); } } This program uses both absolute and relative URLs to create URL objects. It also uses URLConnection objects to read from and write to a URL. There are several classes in java.net for doing further manipulation of URL content, headers, and so forth. If, for example, you need to work with a special content type, you might subclass ContentHandler. To do more advanced work with URLs, check the java.net API documentation.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
networks URLs (uniform resource locators) 2nd 3rd 4th 5th uniform resource locators. URLConnection class URLs (uniform resource locators) accessing 2nd 3rd URLConnection object URLs (uniform resource locators) accessing URLs (uniform resource locators) 2nd 3rd 4th 5th accessing with URL and URLConnection classes 2nd 3rd object creating objects MalformedURLException URLConnection object, accessing through
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229188113198236010021214088
A brief overview of RMI architecture How to call a remote object's methods How to call a remote method using a client-side remote object How to perform remote activation
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
All Books
Search
Java > PURE Java 2 > 15. RMI > RMI Architecture Overview < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229188113198237002222163185
In RMI, a basic method call to a remote object follows this path: 1. The client calls a method of a remote object. 2. This call is intercepted by a stub, which is a client-side proxy for the remote object, which is generally on another physical host. Think of it as a remote reference to the remote object. 3. The stub passes the remote method call to the client-side Remote Reference Layer, RRL, which starts the process of preparing the parameters for passing to the remote object. The RRL calls the Transport Layer, which works with the RRL to marshal (convert arguments, method calls, references, and return values into a special stream needed by the wire protocol to send to another object) the method call with its parameters into a marshalling stream. 4. The Transport Layer sends the serialized parameters in the marshalling stream over the network using Java Remote Method Protocol, JRMP. 5. The Transport Layer on the server side receives the marshalling stream and begins the process of unmarshalling the marshalling stream. The Transport Layer calls upon the server-side Remote Reference Layer to unmarshall and deserialize the remote method parameters as necessary. 6. The server-side RRL passes the method call and its parameters to the skeleton, which is a server-side proxy for the remote object. 7. The skeleton forwards the method call with its parameters to the remote object.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=110 (1 of 3) [6/2/2002 10:30:03 PM]
8. The remote object executes its method and, optionally, sends back a result (as the return type from the method) to the client, back through the skeleton, to the RRL. The result goes on to the Transport Layer, across the network using JRMP, to the client-side Transport Layer, to the RRL, to the stub, and finally back to the client object that called the remote method back in step 1. RMI depends heavily upon object serialization, covered in Chapter 12, "I/O in Java." If you are unfamiliar with object serialization, you should read that section before proceeding with this chapter.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
(Remote Reference Layer) skeletons stubs Transport Layer 2nd RRL (Remote Reference Layer) skeletons Transport Layer 2nd server system on tier 2-n remote factory servers definition of skeletons stubs Transport Layer 2nd
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 15. RMI > Parameters for Remote Methods < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229188112070125245153199071
Serialized objectsThese are serialized copies of the local value the client uses as a parameter to the remote method. Remote referencesIf the parameter of a remote method is a remote object, defined and instantiated on the client side, RMI sends a remote reference (stub) as a marshalled parameternot a copy of the object.
This distinction is very critical to the performance of your RMI applications. If you send small values, such as a string that represents a birth date or telephone number, it will not result in a very significant data transfer of marshalled data. If, on the other hand, you send a very large, complex object, such as a jet airliner or nuclear attack submarine, the marshalled parameter might involve hundreds of thousands of bytes of marshalled data. Serialization copies every object and every dependent object. In the case of the jet airliner, for example, the plane body, every engine, every wheel, every window, every seat, every seat belt, every tray tableeverythingmust be sent by value. The result is quite a large object. Therefore, using local objects for parameters is probably acceptable for a simple e-commerce application but probably is not acceptable for a complex CAD drawing object. Tip Some developers find RMI too slow. Often, however, it is not RMI per se that is slow, but the application that is sending very large objects as local parameters. These are copied across the network by value, which is a very inefficient method. If you must deal with big objects, use remote objects instead of local objects for parameters. This will cut down the network traffic significantly.
If, on the other hand, an object is defined as a remote object in RMI terms, only the remote reference, or stu, will be passed over the network, greatly reducing the amount of data sent and improving the speed of execution. Tip RMI uses URLs to locate remote objects. When RMI is run on Windows clients, it can take noticeably longer than on Solaris clients. This is because Windows spends a great deal of time doing DNS name resolution. Name resolution is done much more quickly on Solaris. For this reason, if you plan to use RMI between server-side objects, I recommend that you do
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=111 (1 of 2) [6/2/2002 10:30:19 PM]
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 15. RMI > Finding Remote Objects < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229188112070126239039134056
Tip You can avoid using the RMIRegistry if you use the exportObject() method of UnicastRemoteObject (discussed in the following section).
This, in brief, is how RMI works. More information on the details of the Remote Method Invocation architecture may be found at http://www.javasoft.com/products/jdk/1.2/docs/guide/rmi/spec/rmiTOC.doc.html.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 15. RMI > Calling a Remote Object's Methods < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229188112070121072144063252
public interface someInterface extends Remote This interface must extend the java.rmi.Remote interface, which contains no methods but marks your interface as appropriate for RMI. In this interface, you define all the methods you want your remote object to have that will be visible to a remote client. Because you are going across address spaces, it is not sufficient to make the methods public. You need to do this additional work so that RMI can provide the mechanism for your remote client to find your remote object. The methods defined in a remote interface are all the methods you want callers to access. A remote object is like any other object in that it can have any number of private methods about which callers know nothing. The methods that go in a remote inter face are the business logic methods a caller needs your remote object to perform. For example, the remote interface for a library catalog might look like the code in Listing 15.1. I will discuss each piece of this application separately because each piece has its own important aspects.
Listing 15.1 A Sample Remote Interface (Borrowable.java)
// Remote interface definition for a library book. import java.rmi.*; import java.util.*; interface Borrowable extends Remote { // A remote interface must extend jva.rmi.Remote. public boolean isCheckedOut() throws RemoteException; // All the methods in a remote interface MUST // be declared as throwing RemoteException. public boolean checkOut(String cardNumber, Date d) throws RemoteException; public boolean checkIn(String cardNumber, Date d) throws RemoteException; } The Borrowable interface I designed has three methods that can be called by other objects. Any implementing classes can have as many other methods as necessary; they simply cannot have other methods that RMI will let clients call on the implementing object. Observe that each remote method is declared as calling a RemoteException. Doing this is required.
Implement a Remote Object Class Definition: Code Example The remote object you want to access, whether it is a shopping cart, or a bank account, or an airline reservation system, must have a class definition that follows these rules:
q q q
It must implement the related remote interface, which must extend the Remote interface. The class must extend java.rmi.server.UnicastRemoteObject. The class must have a coded constructor that is defined to throw a RemoteException (in case anything goes wrong while trying to instantiate it remotely). You cannot merely accept the default constructor the compiler would generate if no constructor was defined for the class.
Note Technically, you do not have to subclass UnicastRemoteObject. However, this class provides several helpful methods that you need. If for some reason you need to subclass some other class, you can still define a class to be a remote object. You need to define your own remote versions of hashCode(), equals(), and toString().
This class implements the business logic your remote object provides to callers (see Listing 15.2). Just as the methods in the interface are declared to throw a RemoteException, the methods in this class are declared to throw RemoteExceptions. Tip If, in fact, you know that your method can't throw a RemoteException, you can implement the method but omit the throws clause. Java does not allow you to throw more exception types than you have declared in a method's signature in an interface. However, it does not forbid throwing fewer.
// Remote object for a Book class. // This class allows for checking the status of a book, // checking out the book, and checking in the book. // It requires a library card number and a Date object. import java.util.*; import java.rmi.*; import java.rmi.server.*; public class Book extends UnicastRemoteObject implements Borrowable { // All RMI remote object classes need to implement // the related remote interface that defines their remotely // callable methods, and such classes should extend // from UnicastRemoteObject. private Date borrowDate; private Date returnDate; private String borrowerID; // Library card number. private String title; private boolean checkedOut = false; public Book(String bookName) throws RemoteException { // The constructor of a remote object must be defined
// as throwing a remote exception. title = bookName; } // Implement the methods in the remote interface. public boolean isCheckedOut() throws RemoteException { return checkedOut; } public boolean checkOut(String cardNumber, Date d) throws RemoteException { borrowerID = cardNumber; borrowDate = d; return true; } public boolean checkIn(String cardNumber, Date d) throws RemoteException { borrowerID = cardNumber; returnDate = d; return true; } } // End class definition for Book. As you can see, there is not much involved in creating a class definition for a remote object. You add the necessary imports and subclass UnicastRemoteObject. The class must also implement your remote interface (which declares the business logic methods you wish to call). The class definition is complete except for adding any private methods you need in addition to the remotely callable methods. The Book class has none, although in real life it probably would have several. Create a Remote Factory on a Server: Code Example Now that the class for the remote object is defined, it is necessary to define a class that can instantiate the remote object. Actually, this class has two jobs: instantiate the remote object and register the remote object with the RMIRegistry. The first task is just like instantiating any local object. Generally, this program runs on the same host as the remote object. The second task is accomplished through the use of either the bind() or rebind() method. (You can remove an object, or unregister it from the registry with the unbind() method.) Choosing between the bind() and rebind() method is easy. The bind() method binds an object to the RMIRegistry. If you call bind() a second time for an object that is already bound to the RMIRegistry, you get an exception. Alternatively, you can use rebind() to bind the remote object (actually, you put a stub in the RMIRegistry). Even the first time, it binds the object to the RMIRegistry and allows you to replace the existing object. You need to bind an object to the RMIRegistry any time you modify it. If you have done proper object-oriented design, that should not happen frequently, but it will happenespecially during unit testing. Here are all the methods for naming classes:
q q q q
bind(String, Remote)Binds the name to the specified remote object list(String)Returns an array of strings of the URLs in the registry lookup(String)Returns the remote object for the URL rebind(String, Remote)Rebinds the name to a new object and replaces any existing binding
Tip You can use the list() method in a manner similar to Java Reflection (see Chapter 14) and the CORBA dynamic invocation to discover objects in the RMIRegistry of another host.
The RMIRegistry stores a URL for the remote object to enable your client to find the remote object. The URL has the format
rmi://hostname:portNumber/logicalName The parts of this URL are explained in the following list:
q q q
hostname is the name of the physical host the object runs on. portNumber is the port number the object runs on (is bound to). logicalName is the logical name you gave in the bind()/rebind() call. The hostname defaults to the host the RMIRegistry is running on, and the default port number is the port number of the RMIRegistry (port 1099).
Caution The HotJava browser from Sun contains an RMIRegistry built in. If you have HotJava running on the same system as the remote object, you will potentially bind to HotJava's RMIRegistry, and not to the RMIRegistry you started separately. This can cause a conflict between the two RMIRegistrys for the 1099 port. What's more, the RMIRegistry in HotJava is transientnot persistent as is the normal RMIRegistry you start at the command line. Therefore, it is best to shut down HotJava, if it is running on the remote object host, before you run the remote factory code or start the RMIRegistry separately.
Caution Be aware that the default Java policy restricts port access for ports above 1024. Since the RMIRegistry's default port is 1099, this is a problem for your program, because it won't be able to access the registry. Be sure that your remote object uses a higher port number than the reserved port numbers. An even better idea is to create a custom RMI policy file that overrides this restriction. See Chapter 16, "Java Security," to learn how to create a policy file.
One other important issue is security. RMI is very particular about allowing a client to execute a method on a remote object. If you attempt to do this without a SecurityManager object in place, RMI will restrict you to only loading classes in the local classpath. This means that your RMI code works fine if it is on the same host but breaks if the remote object is on a host other than the one your client is on. So, instead of pretending this restriction does not exist, the remote factory class will instantiate an RMISecurityManager class instance that will permit some basic class loading. You will need to do this for a remote object running on another system so that there's no unit test-type example that won't work anywhere else. Listing 15.3 shows a simple remote factory for creating a Book remote object and binding it to the RMIRegistry.
Listing 15.3 Creating and Registering a Remote Object Using rebind() (RemoteDriver1.java)
import java.rmi.*; import java.rmi.server.*; // You need the java.rmi package to get the Naming service methods
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=113 (4 of 13) [6/2/2002 10:31:10 PM]
// and for the RMISecurityManager. // You need java.rmi.server for the support classes for the Book class, // which uses several java.rmi.server classes, especially UnicastRemoteObject. import java.net.*; // This class is used to instantiate and register a Book object. // Once that is done, this object is done with its work. class RemoteFactory1 { public RemoteFactory1() { System.setSecurityManager( new RMISecurityManager()); // Instantiate an RMISecurityManager to // allow proper RMI execution. } public void createRemoteBook() { try // You must do this in a try block because the // method class can throw Remote exceptions. { Book remoteBook = new Book("How Shall We Then Live?"); // Instantiate the remote object. To the factory // object, this is just another local object. Naming.rebind("RemoteBook", remoteBook); // Bind (or rebind) the remote object instance to // the RMIRegistry. // The rebind() method takes two parameters: // 1. A logical name to identify the object by. // 2. The object reference. // It is best to make the logical name clearly // relate to the object reference. // It is not, however, necessary. The logical name // for this object could be "SillyPenguin", but that // would not help anyone understand your code or // use your remote objects. } catch(RemoteException re) { System.out.println(re.getMessage()); } catch(MalformedURLException mue) { System.out.println(mue.getMessage()); } System.out.println("Remote Book object ready to go"); // Unlike a CORBA application, nothing is // needed to keep the remote factory class // from exiting. } } public class RemoteDriver1 { // This class launches the remote "factory" object. // It is, of course, not a true object factory.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=113 (5 of 13) [6/2/2002 10:31:10 PM]
public static void main(String[] args) { RemoteFactory1 rf = new RemoteFactory1(); rf.createRemoteBook(); } } If you have CORBA experience, you might be thinking that this looks an awful lot like CORBA, in which you also run a "factory" program to bind/rebind a newly created remote object. Also, like CORBA, RMI uses a client-side stub and a "server"-side skeleton. In general, you will find that RMI is simpler to use than CORBA and also has a lighter-weight framework. As you can see in the sample code, you do not need to do the narrowing of naming contexts and object references that is needed in CORBA. This is possible because RMI deals only with Java objects. Implement a Client Program: Code Example Now that you have a class defined for your remote object and a remote factory class to create the remote object and register it with the RMIRegistry, you need to create a client to request the services of the remote object. The mechanism for this is very important. You do not simply instantiate a remote object by calling its constructor:
Book aBook = new Book( "Pilgrim's Progress"); // WRONG; ILLEGAL // Don't do this!!! Instead, you access a remote object solely through its remote interface. In this case, you ask for an object of type Borrowable. This means that you are asking the JVM to give you an object, any object of any class, that implements the Borrowable interface. It might sound odd, but doing this provides you a very important benefit. This mechanism means that the developer who implements the remote object's class can change that class at will, modifying its logic, private data members, and methods significantly. As long as the interface definition does not change, the client code does not have to be modified one bit. Using an interface this way leads to very loosely coupled client-remote object relationships, which ease maintenance and reduce complexity in applications. Note This same pattern is used by Java elsewhere, such as in JDBC. In it, a driver is an interface implementation, and callers need know nothing about the database driver class itself. This pattern is also used in Enterprise Java Beans, where a caller of an EJB knows nothing about the class of the bean, but knows its home and remote interfaces. This pattern is a very powerful mechanism for decoupling objects. Consider it for use in your own applications to add flexibility and reduce maintenance headaches.
As noted earlier, the RMIRegistry stores the RMI URL for a remote object. The client needs that URL to find the remote object. The remote factory used the Naming class to bind a remote object to the RMIRegistry. The client uses the Naming class to look up a remote object in the RMIRegistry using the Naming.lookup() method. This method takes an RMI URL as a parameter. In Listing 15.4, the URL looks like this:
rmi://localhost/Borrowable Note that you do not specify the name of the class, but the logical name given to the object by the remote factory. After you have an object that implements the interface, you can call remote methods just as though the remote object is local. The work of sending marshalled data over the network is done for you under the covers.
Listing 15.4 Basic RMI Client Using Naming.lookup() (ClientDriver1.java)
// RMI Client
// This example assumes there is only one remote object // of interest. In many situations, you would need to provide // some identifier to get a specific instance. import java.rmi.*; // Package for java.rmi.Naming. import java.util.*; class RMIClient1 // This client can and likely will be a second-tier program. { String server = "localhost"; String rmiUrl = "rmi://"; String remoteClass = "RemoteBook"; // These are the pieces to use to find the Remote object. String libraryCardNumber = "1234"; public RMIClient1() { if (System.getSecurityManager() == null) System.setSecurityManager(new RMISecurityManager()); // Set up a Securitymanager for RMI. } public boolean useLibrary() { boolean result = true; // Initialize the return value. try { rmiUrl = rmiUrl + server + "/" + remoteClass; // Create a URL with rmi as the protocol. Borrowable remoteBook = (Borrowable) Naming.lookup(rmiUrl); // Request a remote object, any remote object, // which implements the Borrowable interface // registered in the RMIRegistry. // Now call some remote methods on the // remote object. System.out.println("The book is checked out: " + remoteBook.isCheckedOut()); boolean outResult = remoteBook.checkOut(libraryCardNumber, new Date()); if(outResult == true) System.out.println("Book checked out successfully"); else result = false; boolean inResult = remoteBook.checkIn(libraryCardNumber, new Date()); if(inResult == true) System.out.println("Book checked in"); else result = false; } catch(Exception e) { System.out.println("RMI error: " + e.getMessage());
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=113 (7 of 13) [6/2/2002 10:31:10 PM]
// The try block could produce a RemoteException // or a MalformedURLException. result = false; } return result; } }
public class ClientDriver1 { public static void main(String[] args) { RMIClient1 rc = new RMIClient1(); boolean result = rc.useLibrary(); if(result == false)
System.out.println("Error using the Library"); } } The client program does a lookup on the remote object through the Naming.lookup(*) method. This method asks the RMIRegistry, which resides on the host named in the RMI URL, for a reference to a remote object that matches the logical name RemoteBook. The reference returned is a reference to some object that implements the Borrowable interface. The reference must be cast to the interface type Borrowable. That's all the coding that is required for a basic RMI application. There are a few more steps, however, before you are finished. Generate a Stub and Skeleton RMI uses stubs and skeletons, which are proxies to the remote object. The stub is used on the client side, and the skeleton is used on the remote object's side. The stub and skeleton are generated for you by the rmic program. To run it, call rmic and give it a command line parameter that equals the name of the class for the remote object, in this case Book:
rmic Book This runs against the class file, not the source file. The output is two files: Book_Stub.class and Book_skel.class. If the remote object is running on a separate host from the client code (which is usually the case), the stub must be on the client's system, and the skeleton must be on the remote object's system. Because all the Java source files need the interface definition, the remote interface source file, Borrowable.java, needs to be available on both the client system and the remote object's system. Now that you know about the pieces of the RMI architecture, Figure 15.1 shows the entire architecture.
Figure 15.1 RMI architecture.
Start the RMIRegistry On Solaristype: rmiregistry (or rmiregistry & to run it in the background). On Windows type: rmiregistry ( start rmiregistry to run it in the background). You should start the RMIRegistry in the directory where the server-side code is located or in a directory that the RMIRegistry can find through theclasspath set in your environment. Start the Remote Factory Program Using a Policy File If you just try to run the program, you will get an AccessControlException because you lack SocketPermission for port 1099. I will discuss policy files in depth in Chapter 16. For now, suffice it to say that you need a policy file. Caution The sample programs include a policy file, RMI.jp. This policy file essentially grants everything to everyone. Normally, you do want to use such a liberal policy file, so make sure you use this file only to run the sample programs. Do not use it in a production environment. There, it defeats the whole point of Java's security mechanism in Java 2.
java -Djava.security.policy=RMI. jp RemoteDriver1 Run the Client The client is equally restricted by the RMISecurityManager object, which grants very little authority to code, so the policy file is needed here as well. To run the client, type
"Book checked out successfully" Note RMI is very particular about where the RMIRegistry is started, where the stub and skeleton are, and security permissions. Any number of exceptions might be encountered. If something goes wrong and you get an exception, verify that everything was started with the right arguments in the right directories. Also be sure that the RMI URL is correct.
files policy RMI.jp HotJava RMIRegistry binding to interfaces java.rmi.Remote remote code code to create 2nd java.rmi.Remote interface list() method method rebind() methods bind() business logic classes naming 2nd list() Naming.lookup() RMI clients, code 2nd 3rd of remote objects calling 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th rebind() remote objects, code to create 2nd 3rd remote objects, code to register 2nd 3rd naming classes methods 2nd Naming.lookup() method RMI clients code 2nd 3rd objects remote class definitions, code to implement 2nd 3rd 4th finding with URLs (uniform resource locators) methods, calling 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th rebind() method, code to create 2nd 3rd rebind() method, code to register 2nd 3rd SecurityManager policy files RMI.jp port access RMIRegistry programs remote factories starting 2nd rebind() method remote objects code to create 2nd 3rd code to register 2nd 3rd registering remote objects rebind() method, code 2nd 3rd registries RMIRegistry port access starting 2nd remote classes class definitions code 2nd remote factories on servers code to create 2nd 3rd 4th 5th 6th
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=113 (11 of 13) [6/2/2002 10:31:10 PM]
programs starting 2nd remote interface code remote interfaces creating code 2nd remote objects class definitions code to implement 2nd 3rd 4th finding URLs (uniform resource locators) methods calling 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th rebind() method code to create 2nd 3rd code to register 2nd 3rd RMI architecture business logic methods clients code to implement 2nd 3rd 4th Naming.lookup() method, code 2nd 3rd running 2nd factories remote, code to create on servers 2nd 3rd 4th 5th 6th HotJava RMIRegistry, binding to interfaces remote, code to create 2nd java.rmi.Remote interface methods business logic objects remote, calling methods 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th remote, code to implement class definitions 2nd 3rd 4th remote factories on servers, code to create 2nd 3rd 4th 5th 6th remote factory programs starting 2nd remote interface code remote interfaces code to create 2nd remote objects class definitions, code to implement 2nd 3rd 4th methods, calling 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th rebind() method, code to create 2nd 3rd rebind() method, code to register 2nd 3rd RMIRegistry port access starting 2nd SecurityManager object skeletons 2nd stubs 2nd UnicastRemoteObject class URLs part of RMI.jp policy file RMIRegistry HotJava binding to port access starting 2nd URLs
(uniform resource locators) remote objects, finding running clients 2nd security SecurityManager object SecurityManager object servers remote factories code to create 2nd 3rd 4th 5th 6th skeletons RMI (Remote Method Invocation) 2nd starting remote factory programs 2nd RMIRegistry 2nd stubs RMI (Remote Method Invocation) 2nd UnicastRemoteObject class URLs (uniform resource locators) part of remote objects finding
2002, O'Reilly & Associates, Inc.
Safari | PURE Java 2 -> Calling a Remote Object with a Remote Object Parameter: Code Examples
All Books
Search
Java > PURE Java 2 > 15. RMI > Calling a Remote Object with a Remote Object Parameter: Code Examples < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229188119006253239208134086
// Remote Interface for Cards // Use as a remote object on the client side of an RMI // conversation (in this example). import java.rmi.*; import java.util.*; public interface Issueable extends Remote // A remote interface should extend Remote. { // Provide some class constants for a library card. public static final int UNLIMITED_BOOKS = 1; public static final int ONE_YEAR_CHECKOUT = 2; // Declare remote business methods. public boolean setExpiration(GregorianCalendar gc) throws RemoteException; public boolean setPrivilges(int privilege) throws RemoteException; public String getCardNumber() throws RemoteException; // All the remote methods are declared as // throwing a remote exception.
Safari | PURE Java 2 -> Calling a Remote Object with a Remote Object Parameter: Code Examples
public void setFines(float newFine) throws RemoteException; public float getFines() throws RemoteException; } The reason for this interface definition, again, is solely to provide for treating the class that implements it as a definition for making remote objects. To accomplish this, the class must implement the Remote interface. The class that implements the Issueable interface in your application is called LibraryCard. 2. Now you need to create a class that implements the remote interface shown in step 1. This class definition is needed on the client side, while the stub you generate must be accessible in some fashion to the caller on the server. The code for this class is shown in Listing 15.6.
Listing 15.6 Class Definition for LibraryCard, a Remote Object Class (LibraryCard.java)
// Client-side remote object // LibraryCard class. import java.rmi.*; import java.rmi.server.*; import java.util.*; class LibraryCard extends UnicastRemoteObject implements Issueable { // Extend UnicastRemoteObject and implement // remote interface. String cardNumber; GregorianCalendar expirationDate; int[] privileges; float fines; // Note that, like all remote class definitions, // the constructor for a remote object must // be declared to throw a RemoteException. public LibraryCard(String cardNumb) throws RemoteException { cardNumber = cardNumb; privileges = new int[4]; } // Now code business methods. // These methods also are declared as throwing // remote exceptions, which are sent in this case // to the server-side remote object. public boolean setExpiration(GregorianCalendar gc) throws RemoteException {
Safari | PURE Java 2 -> Calling a Remote Object with a Remote Object Parameter: Code Examples
expirationDate = gc; return true; } public boolean setPrivilges(int privilege) throws RemoteException { privileges[0] = privilege; // Allow for one privilege at this time. return true; } public String getCardNumber() throws RemoteException { return cardNumber; } public void setFines(float newFine) throws RemoteException { fines = fines + newFine; } public float getFines() throws RemoteException { return fines; } } 3. Modify the server-side remote object to use the client-side remote object. This is fairly straightforward and involves the following steps:
r
Change the Borrowable interface method declarations to accept parameters of type LibraryCard rather than a string. Change the Book class to implement the new methods, extracting data from a LibraryCard by calling its remote methods rather than working with a copy of the data sent by the client. Modify the RemoteDriver to use the new version of the Book class.
These small changes can be seen in Borrowable2.java, Book2.java, and RemoteDriver2.java. 4. Modify the client-side program to use the new LibraryCard remote object. This is a small change. First, instantiate an object of type LibraryCard. Because this class extends UnicastRemoteObject, it is automatically exported for you by RMI when you call a remote method. Next, you change the method calls to use the object reference. (In this case, as far as RMI is concerned, it really points to a stub.) The new client is shown in Listing 15.7.
Listing 15.7 RMI Client That Passes a Remote Object Parameter (ClientDriver2.java)
// RMI Client // This example assumes there is only one remote object // of interest. In many situations, you would need to provide
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=114 (3 of 7) [6/2/2002 10:31:20 PM]
Safari | PURE Java 2 -> Calling a Remote Object with a Remote Object Parameter: Code Examples
// some identifier to get a specific instance. import java.rmi.*; // Package for java.rmi.Naming. import java.util.*; class RMIClient2 { String server = "localhost"; String rmiUrl = "rmi://"; String remoteClass = "RemoteBook2"; // These are the pieces to use to find the Remote object // on the "server." LibraryCard libCard; public RMIClient2() { if (System.getSecurityManager() == null) System.setSecurityManager( new RMISecurityManager()); // Set up a SecurityManager for RMI. // Otherwise, you will get a SecurityException // when you attempt to access a server-side // remote method. try // Wrap the remote calls in a try block, // because they may throw a RemoteException. { libCard = new LibraryCard("1234"); } catch(RemoteException re) { re.printStackTrace(); System.out.println(re.getMessage()); } // Instantiate a "remote object" here to be used // as a parameter in a remote method call. } public boolean useLibrary() { boolean result = true; try { rmiUrl = rmiUrl + server + "/" + remoteClass; Borrowable2 remoteBook2 = (Borrowable2) Naming.lookup(rmiUrl); // Request a remote object, any remote object, // which implements the Borrowable interface. // Now call some remote methods on the // remote object. System.out.println("The books is checked out: " +
Safari | PURE Java 2 -> Calling a Remote Object with a Remote Object Parameter: Code Examples
remoteBook2.isCheckedOut()); boolean outResult = remoteBook2.checkOut(libCard, new Date()); This time, pass a remote object reference as a method parameter. This should not marshal a copy of the LibraryCard object but just send its stub. if(outResult == true) System.out.println ("Book checked out successfully"); else result = false; boolean inResult = remoteBook2.checkIn(libCard, new Date()); if(inResult == true) System.out.println("Book checked in"); else result = false; } catch(Exception e) { System.out.println("RMI error: " + e.getMessage()); // The try block could // produce a RemoteException // or a MalformedURLException. result = false; } return result; // // // // } }
public class ClientDriver2 { public static void main(String[] args) { RMIClient2 rc = new RMIClient2(); boolean result = rc.useLibrary(); if(result == false) System.out.println("Error using the Library"); } } 5. Create stubs and skeletons for everything. Run rmic to create stubs and skeletons for both the Book and LibraryCard remote classes. At runtime, the Book2 class must be able to find the stub for the LibraryCard class, just as the ClientDriver2 class must be able to find the stub for the Book2 remote class. 6. Start the RMIRegistry. Start the RMIRegistry in the chap15 directory or set your classpath to be able to find the chap15 directory. Next, start the RMIRegistry. It must be able to find the stubs and skeletons for both Book2 and LibraryCard.
Safari | PURE Java 2 -> Calling a Remote Object with a Remote Object Parameter: Code Examples
java -Djava.security.policy=RMI.jp RemoteDriver2 8. Run the client ClientDriver2. If all goes well, you should see this on the client's command line:
java -Djava.security.policy=RMI.jp RemoteDriver2 Remote Book object ready to go In the window for the server side and in the client side window, you will see this:
java -Djava.security.policy=RMI.jp ClientDriver2 The book is checked out: false Book checked out successfully Book checked in As you can see from this code, it is possible to define a remote object on the client side of an application and send it to the server side by reference. The RMI does not have to send a marshalled copy. In the previous example, using this technique probably did not improve performance noticeably. If, however, I replace the LibraryCard class with a complex CAD drawing or a complex object (such as a 747 airplane, a nuclear submarine, or a hotel), this procedure becomes an important way to speed up performance.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
Safari | PURE Java 2 -> Calling a Remote Object with a Remote Object Parameter: Code Examples
class definitions code 2nd objects remote class definitions, code 2nd parameters, code 2nd 3rd parameters, code to call 2nd 3rd 4th 5th 6th 7th parameters remote objects code to call 2nd 3rd 4th 5th 6th 7th remote interfaces Issueable code 2nd remote object class class definitions code 2nd remote objects parameters code 2nd 3rd code to call 2nd 3rd 4th 5th 6th 7th RMI clients remote object parameters, code 2nd 3rd objects remote, code to call with parameters 2nd 3rd 4th 5th 6th 7th remote objects parameters, code to call 2nd 3rd 4th 5th 6th 7th
2002, O'Reilly & Associates, Inc.
Safari | PURE Java 2 -> Creating a Remote Object from a Client: Remote Activation Code Examples
All Books
Search
Java > PURE Java 2 > 15. RMI > Creating a Remote Object from a Client: Remote Activation Code Examples < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229188119006248220007020001
There are times, however, when you don't want to depend on a "server-side" program to take care of setting up a remote object for you. You might want to do this to save memory, for example, or to decouple the client application from the server application. The means to do this is the new Java 2 feature called activation. Remote activation provides the framework to create a remote object on-the-fly from the client. To support remote activation, you use rmid, the RMI daemon, to control remotely activating an object. As in previous procedures, the RMIRegistry must be running. The RMIRegistry runs at the same time as rmid. The steps required for activation are similar to normal RMI programming with the addition of a setup class. The setup class "sets up" the activation environment for you, and then the client can make calls to create remote objects. These are intercepted by rmid, which then uses the setup info to instantiate the remote object. Here are the steps to perform object activation in RMI. Any steps that are the same as steps discussed earlier in this chapter are not explained again. For information on them, see the previous sections of this chapter. 1. Create a remote interface. 2. Implement the remote interface. Three changes must be made to the remote object's class definitionin this case, Book:
r
Change
import java.rmi.server.*; to
import java.rmi.activation.*;
r
Safari | PURE Java 2 -> Creating a Remote Object from a Client: Remote Activation Code Examples
ActivationID actID, MarshalledObject mo) throws RemoteException { // Register the object with the activation // system and export it on an anonymous port // by calling the superclass constructor. // The MarshalledObject would be a serialized // object passed to the Book class. super(actID, 0); // No MarshalledObject passed, so use 0. // Do other constructor-related stuff. } Listing 15.8 shows the activatable version of Book.
Listing 15.8 An Activatable Remote Object (ActivatableBook.java)
// // // //
Remote object for a Book class. This class allows for checking the status of a book, checking out the book and checking in the book. It requires a library card number and a Date object. java.util.*; java.rmi.*; java.rmi.activation.*; Instead of importing java.rmi.server.*, to get things like UnicastRemoteObject, you import java.rmi.activatin.*; java.io.*; class ActivatableBook extends Activatable implements Borrowable
// All RMI remote object classes need to implement // the related remote interface that defines their remotely // callable methods. Activatable classes should inherit // from Activatable, not UnicastRemoteObject. private Date borrowDate; private Date returnDate; private String borrowerID; // Library card number. private String title; private boolean checkedOut = false; public ActivatableBook(ActivationID actID, MarshalledObject mo) throws RemoteException // Look at this constructor. It does not just take // initialization data. In fact, the data to // initialize the activatable object is hidden // inside the MarshalledObject mo. { // Register the object with the activation // system and export it on an anonymous port // by calling the superclass constructor. // The MarshalledObject would be a serialized // object passed to the Book class. super(actID, 0); // No MarshalledObject passed, so use 0. try { title = (String)mo.get();
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=115 (2 of 9) [6/2/2002 10:31:57 PM]
Safari | PURE Java 2 -> Creating a Remote Object from a Client: Remote Activation Code Examples
} catch(ClassNotFoundException moge) // This exception is thrown if the // RMIClassLoader can't find your class // definition at runtime. { System.out.println("MarshalledObject get failed: " + moge.getMessage()); } catch(IOException ioe) { System.out.println("MarshalledObject creation failed: " + ioe.getMessage()); } } // Implement the methods in the remote interface. public boolean isCheckedOut() throws RemoteException { return checkedOut; } public boolean checkOut(String cardNumber, Date d) throws RemoteException { borrowerID = cardNumber; borrowDate = d; return true; } public boolean checkIn(String cardNumber, Date d) throws RemoteException { borrowerID = cardNumber; returnDate = d; return true; } } // End class definition for ActivatableBook. 3. Create a setup class. The setup class, which is what the Javasoft documentation calls this class, creates the information needed by the activatable class. The setup class passes the information about the activatable class to rmid, the activation daemon, registers a remote reference (an instance of the activatable class's stub class) and a moniker (chosen by the developer) with the RMIRegistry, The setup class exists to create the activation information. It does not need to create the actual remote object nor does it need to stay in memory. Here are the steps to create a setup class: 1. Make the appropriate imports. 2. Install a SecurityManager. 3. Create an ActivationGroup instance. 4. Create an ActivationDesc instance. 5. Declare an instance of your remote interface and register with rmid. 6. Bind the stub to a name in the RMIRegistry. Listing 15.9 shows a setup class for the Book remote object class.
Safari | PURE Java 2 -> Creating a Remote Object from a Client: Remote Activation Code Examples
// This is a setup class for remote activation of an RMI object. // It does not as such make the remote object. // Rather, it creates the framework for the client // to make the remote object on-the-fly. import java.rmi.*; import java.rmi.activation.*; import java.security.CodeSource; import java.util.Properties; import java.net.*; import java.io.*; class RemoteSetup { String location = "http://localhost:8080/"; // Note the trailing /. This is essentially // a directory name and must end with this character. MarshalledObject mo = null; Properties sysProps; public RemoteSetup() { System.setSecurityManager( new RMISecurityManager()); // Instantiate an RMISecurityManager to // allow proper RMI execution. // Even if you did not otherwise need it, // Activation requires a SecurityManager to be // installed in the JVM at runtime. } public void prepareRemoteBookActivation() { try { // First, get a copy of the system properties. // Since a java.security.policy parameter was // supplied on the command line to run this // program, that property is included. // Activation needs to know where the policy file // resides. This means, since policy files // are only used in Java 2 that Activation can // only be used in Java 2. sysProps = (Properties)System.getProperties().clone(); // Clone the system properties so that your code // doesn't trash the existing system properties. // Now, make an ActivationGroupDesc, // an ActivationGroupID, // and an ActivationGroup. ActivationGroupDesc.CommandEnvironment actCmdEnv = null; ActivationGroupDesc bookGroup = new ActivationGroupDesc(sysProps, actCmdEnv); System.out.println("Made an ActivationGroupDesc"); // Create an ActivationGroupDesc (activation // descriptor) which contains the security
Safari | PURE Java 2 -> Creating a Remote Object from a Client: Remote Activation Code Examples
// policy info. // Now register the ActivationGroupDesc // with the Activation system and get an ID // for it. ActivationGroupID actGrID = ActivationGroup.getSystem().registerGroup( bookGroup); System.out.println("Made an ActGrpID"); // Next, explicitly create an ActivationGroup. ActivationGroup.createGroup(actGrID, bookGroup, 0); // This method takes three parms: // ActivationGroupID id // ActivationGroupDesc desc // long incarnation System.out.println("Made a group"); // Next, create an ActivationDesc object. mo = new MarshalledObject("How Shall We Then Live?"); System.out.println("Made a marshalledobject"); // Create a MarshalledObject to pass parameters // to the Book constructor. ActivationDesc desc = new ActivationDesc (actGrID, "ActivatableBook", location, mo); System.out.println("made an act desc"); // Give rmid enough info to instantiate // the activatable object. Borrowable activatableBook = (Borrowable)Activatable.register(desc); System.out.println( "Ready to activate a Book"); System.out.println("Activatable contents: " + activatableBook.toString()); Naming.rebind ("ActivatableBook", activatableBook); Bind (or rebind) the remote object instance to the RMIRegistry. The rebind() method takes two parms: 1. A logical name to identify the object by 2. The object reference It is best to make the logical name clearly relate to the object reference. It is not, however, necessary. The logical name for this object could be "SillyPenguin", but that would not help anyone understand your code or use your remote objects. } catch(RemoteException re) { System.out.println(re.getMessage()); } catch(ActivationException acte) { System.out.println(" Activation failed: " + acte.getMessage()); acte.printStackTrace(); } catch(IOException ioe) // // // // // // // // // // //
Safari | PURE Java 2 -> Creating a Remote Object from a Client: Remote Activation Code Examples
{ System.out.println("MarshalledObject creation failed: + ioe.getMessage()); ioe.printStackTrace(); } System.out.println ("Remote Book object ready to go"); System.exit(0); // Exit the setup program. } } public class RemoteDriver3 { public static void main(String[] args) { RemoteSetup setup = new RemoteSetup(); setup.prepareRemoteBookActivation(); } } In this code, the activation group descriptor, ActivationGroupDesc object, provides the RMI daemon, rmid, with all the information necessary to contact the appropriate JVM to create the object (either the currently running JVM or a new one started by rmid). After you have an ActivationGroupDesc object, you can register it with the activation system and get an ID for it. You can use the ActivationGroupDesc object and the ID to create an ActivationGroup. After creating an ActivationGroup object, the program creates an ActivationDesc (Activation descriptor) object to provide rmid with the necessary information to instantiate the activatable object. There are several constructors that the code might use. This form uses the activation group ID, the name of the activatable class, the location of the activatable class, the MarshalledObject data and whether the activatable object can be restarted. 4. Create a client to activate the activatable object. There is very little a client actually has to do differently when using activation as opposed to the mechanics of using an existing remote object on another host. In fact, all that is different about the client in Listing 15.10 is the name of the remote class to lookup().
Listing 15.10 Client for RMI Activation (ClientDriver3.java)
"
// // // //
RMI Client This example assumes there is only one remote object of interest. In many situations, you would need to provide some identifier to get a specific instance.
import java.rmi.*; // Package for java.rmi.Naming. import java.util.*; class RMIClient3 { String server = "localhost"; String rmiUrl = "rmi://"; String remoteClass = "ActivatableBook"; // These are the pieces to use to find the Remote object. String libraryCardNumber = "1234"; public RMIClient3() { if (System.getSecurityManager() == null) System.setSecurityManager(new RMISecurityManager()); // Set up a Securitymanager for RMI. } public boolean useLibrary()
Safari | PURE Java 2 -> Creating a Remote Object from a Client: Remote Activation Code Examples
{ boolean result = true; try { rmiUrl = rmiUrl + server + "/" + remoteClass; Borrowable remoteBook = (Borrowable) Naming.lookup(rmiUrl); // Request a remote object, any remote object, // that implements the Borrowable interface. // Now call some remote methods on the remote object. System.out.println("The book is checked out: " + remoteBook.isCheckedOut()); boolean outResult = remoteBook.checkOut(libraryCardNumber, new Date()); if(outResult == true) System.out.println("Book checked out successfully"); else result = false; boolean inResult = remoteBook.checkIn(libraryCardNumber, new Date()); if(inResult == true) System.out.println("Book checked in"); else result = false; } catch(Exception e) { System. out.println("RMI error: " + e.getMessage()); // The try block could produce a RemoteException // or a MalformedURLException. result = false; } return result; } }
public class ClientDriver3 { public static void main(String[] args) { RMIClient3 rc = new RMIClient3(); boolean result = rc.useLibrary(); if(result == false) System.out.println("Error using the Library"); } } 5. Run rmic on the activatable class:
Safari | PURE Java 2 -> Creating a Remote Object from a Client: Remote Activation Code Examples
java -Djava.security.policy=RMI.jp ClientDriver3 This technique allows you to write RMI programs that can dynamically create a remote object on another system to do work for you on another host. In each of these sample programs, I have run all the code from one directory. This will not be the usual situation. As you have seen, both the client and server need to know about the interface to the remote object. It is possible, however, to dynamically download the remote stub from one system to another. Before you decide to do this, be aware that it is a bit tricky. It is not hard, but it does require several steps that must be done exactly. If you run into trouble attempting remote downloading of stubs, see http://www.javasoft.com/products/jdk/1.2/docs/guide/rmi/codebase.html This is more of a configuration issue than a coding issue, so I am not going to step through an example. Briefly, though, here's what you will need to do to remotely download a stub on demand: 1. Do all the normal compilation and run rmic on the remote object class(es). 2. On the client machine, make absolutely certain that the stubs cannot be found in any place that the classpath points. If you overlook this, you will get the stubs from the classpath and not the ones you want to download. 3. Start the RMIRegistry in a directory that will not let the RMIRegistry find the stubs through the classpath. 4. Put the stubs in a place the client can reach, often a directory on a Web server for which the client has or can easily get the URL. 5. Start the server with the java.rmi.server.codebase property to point to the exact location of the code. In the case of a Web server-based applet, the codebase property must point to the codebase of the applet. 6. Start the client. If the server knows the correct location of the stubs, you've specified everything correctly, and the RMIRegistry was started in the correct location (/temp or c:\temp is a good choice), everything should work. If it does not, consult the preceding URL for troubleshooting. This is a handy technique. Be warned, however, that the RMI-USERS mailing list (run by Sun) probably has more posts about problems encountered while doing this procedure than about any other topic. Tip You can find lots of helpful information on using RMI on the RMI-USERS mailing list. You can subscribe by sending the message subscribe RMI-USERS your name to [email protected].
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
Safari | PURE Java 2 -> Creating a Remote Object from a Client: Remote Activation Code Examples
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229188118040237157024066186
Using policy-based permissions Creating a policy file Signing code Creating a digital certificate
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
All Books
Search
Java > PURE Java 2 > 16. Java Security > Using Policy-Based Security < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229188118040236183222028127
FileInputStream fis = new FileInputStream("AddressBook.dat"); would cause checkRead() to be called, and by default the read operation would be disallowed. You would get a SecurityException. By default, the SecurityManager class prohibits most everything. For example, the SecurityManager class in most Web browsers prohibits an applet from reading files, writing files, loading native libraries, modifying thread groups, or connecting to a host other than the host from which the applet was downloaded. The SecurityManager class was limited in that it usually granted or withheld privileges from all code. There was no easy way to dynamically grant permission to one applet to read from a host's file system while denying another applet this privilege. The browser vendor would have had to know ahead of time which classes to grant privileges to and which classes to withhold privileges from. That is clearly impractical. So if the SecurityManager granted permission for some operation, it generally granted it for all classes. The SecurityManager class is generally used only in Web browsers. It can be used by applications to limit their own actions, but this is less common. To have a SecurityManager present, you must explicitly instantiate it, with code like this:
System.setSecurityManager(new SecurityManager()); Browsers typically call code like this when the JVM is started. Once a SecurityManager object (or an instance of a subclass of SecurityManager) has been instantiated in a JVM, it cannot be removed or replaced, for obvious reasons. If you could replace it on-the-fly, the SecurityManager class would not provide much security. This security framework causes code to run in what is called the sandbox. You can do whatever you want in the sandbox, but you cannot get outside of it. Generally all the
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=118 (1 of 5) [6/2/2002 10:33:02 PM]
restrictions just mentioned apply to code in the sandbox. This could be a major issue if a user wanted to print from an applet or if an applet wanted to write a configuration file on the user's system. Even if the end user wanted this, the applet was unable to do so because it was a security violation and the SecurityManager object would not allow it. To mitigate this, JDK 1.1 introduced the concept of a signed class. Signing code had the effect of granting your applet more privileges. For example, if I write an applet and digitally sign it (to be described later), and you download it, you are given the option to let it execute. If you allow a signed applet to run in a pre-Java 2 environment, the applet could do anything it wanted to do. However, if you allowed signed code that was from a malicious source to run, it could prove harmful to your system. Java 2 introduces a new middle ground: fine-grained, policy-based permissions. A permission is a specific category of resource or operation that you wish to allow. For example, you would use SocketPermission to allow a program to access more sockets than it can by default or to access sockets in ways not permitted by default. SocketPermission needs to be expanded for RMI to work in Java 2, as you can see in Chapter 15, "RMI." Many permission classes have subpermissions. For example, java.lang. RuntimePermission has a number of permissions that are granted through it, arranged hierarchically. For example, RuntimePermission allows you to grant queuePrintJob, by which you give permission to print to a program running on your system. Or, you might grant readFileDescriptor so that a program can read a file on your system. The Java 2 security model is based on protection domains. A protection domain correlates roughly to a running program. At runtime, the permissions granted through all the relevant policy files for a protection domain are placed into one large policy object. This policy object exists solely for the JVM to consult it for permissions. To be very explicit about the role of a policy file, a policy file resides on a "client's" system. If a SecurityManager object is instantiated, methods that make security checks invoke methods of the SecurityManager object. The SecurityManager in Java 2 does not simply call checkRead() or loadLibrary() and return a boolean result, as in JDK 1.0 or 1.1. Instead, the Java 2 SecurityManager checks the list of permissions that have been granted through any relevant policy file. Note Permissions are cumulative. If there are three policy files accessed for a given client-side program, all the permissions will add together into a comprehensive set of permissions. There is no such thing as taking away a permission. If one policy file grants a permission that the other policy files do not grant, the permission is granted. There is no "anti-permission" syntax.
Before looking at how to create a policy file, you need to know where to find a policy file. If you go to the directory in which the SDK or JRE resides (JAVA_HOME in the example), you should be able to navigate to the appropriate directory:
In this directory are two very important files. The first is java.security, which specifies where to find policy files, like this:
policy.url.1=file:${ java.home}/lib/security/java.policy policy.url.2=file:${ user.home}/.java.policy These two lines point to the location of the policy files the system expects by default. The first points to java.home/jre/lib/security/java.policy. This is the default Java policy file, and it grants precious little. The second policy file, called .java.policy, lives in the user.home directory. On UNIX, that is the $HOME directory. On Windows, this is generally C:\, the root directory. This latter policy file would be defined by the developer and given to the end user to put in the user.home directory. It is also possible to create your own policy file elsewhere, on an application-specific basis, for example. That would be a good choice if you want just your application to have a given set of permissions. For an application as opposed to an applet, you could make this policy file part of the distributed code, installed with a product such as InstallAnywhere. To tell Java to use this custom policy file, you would type this at the command line:
java -Djava.security.policy=myPolicyfile.jp MyCode This will run your code using this policy file. I did this consistently in the RMI examples in Chapter 15. So there are three places for a policy file: jre/lib/security (pretty limited), user.home (there is no default version), and a custom policy file in a directory of your choice. Let me emphasize again that you can code a policy file that works just fine with your applet or application. That's not enough. You must get that file installed on the client's machine in the right place. This means that for a Java 2 applet, you must do the following: 1. Code a policy file for the applet. 2. Get the applet on a Web page that causes the Java 2 plug-in to run. (This is the only way I know to run Java 2 in Netscape or Internet Explorer, as of September 1999.) This means the proper applet tags are used to trigger the use of the plug-in. 3. Have the user install the policy file in an appropriate place. For an applet, the most likely place is user.home. Be sure to warn the user of the significance of this. That policy, because of its location, will apply to every applet or application that runs on the client's system. 4. Have the user bring up the applet on a browser that is configured to use the Java 2 plug-in. One final important point about permissions is that they can be granted to one of three groups:
q q q
All code Code signed by one or more individuals Code from a specific code domain, such as java.sun.com or http://www.ibm.com
You can grant permissions to any code signed by Ken Litwak, or to any code from
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=118 (3 of 5) [6/2/2002 10:33:02 PM]
java.sun.com. For more information on policy files, refer to the following URL: http://java.sun.com/products/jdk/1.2/docs/guide/security/PolicyFiles.html
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
SecurityManager object SecurityManager class methods 2nd permissions signed class Web sites Java policy file information
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 16. Java Security > Creating a Policy File < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229188117082107220232025076
policytool On most systems, this will bring up the policy tool, along with an error message, such as Could not find Policy file: C:\Windows\.java.policy. Just click OK. It's not really a problem. Figure 16.1 shows the policy tool when you first start it.
Figure 16.1 The main Policy Tool screen.
To make this practical, I will step through making a policy file. 1. Click Add Policy Entry. This brings up a dialog box in which you can select permissions, as shown in Figure 16.2.
Figure 16.2 Adding a permission to a policy file.
2. You have a choice now. You have to decide whether to grant the permission to code signed by someone or code from a specific domain, or grant the permission to all code. In order to grant permission to all code to use these permissions (probably not the usual approach), type nothing in either text field and click Add Permission. 3. Choose permissions to grant. Click Permission, and then select FilePermission. You can either choose Target Name and type a specific filename in the field to the right or choose All Files. Click All Files. Note Although it's less common than using SDK-defined permissions such as ReflectPermission, it is possible to create your own Permission object by subclassing java.security.Permission and coding your own implies() method.
4. Permissions often (but not always) have actions associated with them. FilePermission has several actions. Click the Actions list box, as shown in Figure 16.3, and select the last choice, read, write, delete, execute. Then click OK.
Figure 16.3 The Add New Permission dialog box.
5. Repeat this cycle to grant a Runtimepermission to queuePrintJob. 6. Click Done. 7. Ignore the error that the signer does not have keys yet. 8. Save the file in a directory. I am putting it in C:\Purejava\source\chap16\custom.jp. You will not be prompted to save the file. You need to explicitly select the File menu and then choose Save. The suffix jp is not required, but I think it is helpful to identify this as a Java policy file. 9. Exit the policy file. Now that we have the policy file, we need to sign some code.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
permissions adding policy tool screen screens policy tool security Permission object creating permissions policy files, adding to policy files creating 2nd 3rd 4th 5th permissions, adding policy tool screen tools policy screen
2002, O'Reilly & Associates, Inc.
All Books
Java > PURE Java 2 > 16. Java Security > Sample Code for Digital Signing < BACK
158122020158178182047242169203138024176246252243131023021084157229188117082104007015134026
// This program has little in the way of security-related code. // The program's purpose is to provide code that will need more permissions // than the default java.policy file grants so that you can't run this // program successfully without granting it additional permissions. import java.io.*; class PrivilegedFileAccessor { private FileWriter fw; private PrintWriter pw; private File textFile; private FileReader fr; private BufferedReader br; private String[] books = { "Rhetorica by Aristotle", "Against Apion by Josephus", "4QMMT author unknown", "Peloponesian War by Thucydides"};
public PrivilegedFileAccessor() { try { // Attempt some normally restricted actions. // Without proper permissions, these // constructors will throw security exceptions. // You should NOT catch SecurityException. textFile = new File("AncientTexts.txt"); fw = new FileWriter(textFile); pw = new PrintWriter(fw); // This code attempts to create // a File object, a FileWriter object, // and a PrintWriter object. // Under the default permissions, // this will not be allowed. } catch(IOException e) // Catch any IOExceptions that occur. // Never catch Security-related exceptions. { System.out.println("Cannot create file because of "
graphics/ccc.gif
e.toString()); }
} public void writeOutput() { for (int i=0; i< books.length; i++) { pw.println(books[i]); // Write out the books to a file // using a PrintWriter. } pw.flush(); pw.close(); // Flush the buffer to force everything to disk // and close the writer. } public void readInput() { try { fr = new FileReader(textFile); // Attempt to open a reader on the file. br = new BufferedReader(fr); // Wrap the FileReader in a BufferedReader // for much easier and more efficient I/O. for(int i =0; i < books.length; i++) { String line = br.readLine(); System.out.println(line); // Read a line from the file and // print it on standard out. } } catch(IOException e) { System.out.println("Read failed because of " + e.toString()); } } } // End class definition PrivilegedFileAccessor. public class PrivilegeDriver { public static void main(String args[]) { System.setSecurityManager(new SecurityManager()); // Install a SecurityManager object. // Java 2 will require a policy file or // you will not be permitted to run this // but will instead get a security exception. PrivilegedFileAccessor pfa = new PrivilegedFileAccessor(); pfa.writeOutput(); pfa.readInput(); System.exit(0); } } This same program with different class names appears in Chapter 12, "I/O in Java." The real difference between the two versions is the single line of code I added to main :
System.setSecurityManager(new SecurityManager()); This installs a SecurityManager object in the JVM running this program. Because the default policy file does not grant permission to do reads or writes to files, this code will fail. Here's the output from simply executing like this:
java PrivilegeDriver Exception in thread "main" java.security.AccessControlException: access denied java.io.FilePermission AncientTexts.txt write) at java.security.AccessControlContext.checkPermission(AccessControlC xt.java:195) at java.security.AccessController.checkPermission(AccessController.j 403) at java.lang.SecurityManager.checkPermission(SecurityManager.java:54) at java.lang.SecurityManager.checkWrite(SecurityManager.java:958) at java.io.FileOutputStream.<init>(FileOutputStream.java:96) at java.io.FileOutputStream.<init>(FileOutputStream.java:62) at java.io.FileOutputStream.<init>(FileOutputStream.java:132) at java.io.FileWriter.<init>(FileWriter.java:43) at PrivilegedFileAccessor.<init>(PrivilegeDriver.java:26) at PrivilegeDriver.main(PrivilegeDriver.java:75) This is unacceptable, because we want this program to be able to read and write files, even if it still restricted in other ways. So we will use the policy file created earlier to run the code:
java -Djava.security.policy=custom.jp PrivilegeDriver This time the correct output is printed on the console:
Rhetorica by Aristotle Against Apion by Josephus 4QMMT author unknown Peloponesian War by Thucydides In case you have trouble making this work, here is a list of the contents of custom.jp that does work:
/* AUTOMATICALLY GENERATED ON Mon Sep 06 20:48:07 PDT 1999*/ /* DO NOT EDIT */ grant { permission java.io.FilePermission "<<ALL FILES>>", "read, write, delete, execute"; permission java.lang.RuntimePermission "queuePrintJob"; }; Tip If you want to check for a given permission, you can call
AccessController.checkPermission(some permission object); The method accepts a java.security.Permission object that you construct on-the-fly. You might do this if you wanted to know what was allowed by the JVM before attempting it and getting an exception. You might then inform the user that a permission is lacking, which prevents you from doing what your program is supposed to do.
All Books
Java > PURE Java 2 > 16. Java Security > Procedure for Digitally Signing Code < BACK
158122020158178182047242169203138024176246252243131023021084157229188117082110220055039140
You can actually verify a digital signature in Java in three ways. You can use
q q q
jarsigner APIs in your code Permissions in a policy file that are granted to a specific signer or set of signers
The certificate that provides verification of a digital signature can be stored in the keystore database on a host. You can retrieve a certificate from the keystore using
keytool -export You can also add a certificate from someone else to your keystore using
keytool -import A keystore database may contain both trusted certificates and key/certificate pairs, which consist of the private key and the public key certificate. A key is essentially a random bit-pattern used for hashing or encrypting data. Keys should be totally random, but in practice they are pseudo-random. Many algorithms exist to make them more random, but that is beyond the scope of this book. The keytool can generate private keys and matching public key certificates, import certificates to the keystore, export certificates from the keystore, and manage your keystore. keystore is password-protected.
A key is essentially a binary value used to encrypt or decrypt data. The exact method that is used with the key to encrypt or decrypt data depends on the encryption algorithm used. There are several algorithms, such as Blowfish, in use. The best algorithms are published so that encryption experts can look for holes in the algorithms that wold allow a hacker to break the key's code easily. It is essential that keys be kept secret. If you obtain my keys, you can decrypt confidential data I've encrypted or encrypt data, pretending to be me. Keys come in two flavors: symmetric and asymmetric. Asymmetric keys require much more processing but are much more secure. Typically, data encrypted with tools such as SSL uses symmetric keys. Keys can be public or private. I use my private key to encrypt data for you. You use my public key to decrypt it. You can use my public key to encrypt data, and that encrypted data can only be decrypted using my private key. Several steps are required in the code-signing processsome by a developer, and some by a user of the developer's code. Here are the steps: 1. Put the class file(s) for your application or applet into a jar file. A jar file, discussed more in Chapter 19, "Utility Classes," is similar to a zip file but is platform-neutral. Java provides support to put files into a jar file and then extract them again. To create a jar file containing our PrivilegeDriver.java file, type the following on the command line:
jar cf Driver.jar *.class You can put any kind of resource that is intelligible to Java in a jar file. Here we are putting only .class files into the jar file. 2. Assuming that you do not already have a private key, you use keytool to generate a private key to use in signing code and a public key for others to use in verifying your signature. Type the following on the command line (you would change the values for your own application in real life):
keytool -genkey -alias JavaMaster -keypass javam123 -keystore purejava -storepass purej123 This is all typed as one command on one line. After you press Enter at the end of this command, you will be asked several questions. These values are placed inside the certificate generated by this command. The more unique and unpredictable your answers, the harder your private key will be for an attacker to compute. This command has several parts:
r r r
genkeyWhat you want to donamely, generate a key pair. aliasA name, usually for a role in your organization. This is the name used in the policy file for a code signer. keypassA password for the private key you are creating. This password must be used when accessing the keystore for this private key. keystoreThe name of the keystore database you want to use. storepassThe password for the keystore database you are creating.
r r
Tip For greater security when generating keys, do not use the keypass or storepass option. Instead, allow the keytool to prompt you for a password when you access keystore.
When the command completes successfully, it generates a public/private key pair for you and a self-signed certificate that contains the public key and the data values you entered. 3. Now that you have a key pair and a certificate, you can sign your jar file by typing this:
Press Enter. You will be prompted for the key and keystore passwords. Caution When you do this, the passwords you are prompted for show up as clear text on the screen. So be sure that you do not type this in easy view of others and that you do not leave it on a screen others can readily see if you step away momentarily. Otherwise, your passwords may be compromised, and with them the whole point of signing code.
4. Now you export the self-signed certificate. To allow someone else to use your signed code, the other person needs a copy of your certificate. To get a certifi cate to send to that individual, type this at the command line:
keytool -export -keystore purejava -alias JavaMaster -file JavaMaster.cer You will be prompted for the keystore password. Once again, when you type it, the password will appear in plain text on the screen. All of these actions are performed by the developer. The next few steps need to be performed by the user who wants to execute your program. 5. Import the certificate as a trusted certificate into the user's keystore. So far, there is only a keystore for the developer. In this step I will create a second keystore in the same directory to show you how this is done. If you import to a keystore that does exist, the keytool will create a keystore by that name for you and import the certificate as a trusted certificate into the new keystore. Type this command:
keytool -import -alias User -file JavaMaster.cer -keystore userstore In this version of the command, no password is supplied, so you will be prompted for one. I will use user123. The keytool tool will print out the certificate information and ask you to verify that this should be a trusted certificate. Type yes. Here's what appears on my screen:
C:\Purejava\Source\chap16>keytool -import -alias User -file JavaMaster.cer -key store Userstore Enter keystore password: user123 Owner: CN=Ken Litwak, OU=Sun, O=SunEd, L=Milpitas, ST=CA, C=US Issuer: CN=Ken Litwak, OU=Sun, O=SunEd, L=Milpitas, ST=CA, C=US Serial number: 37d4a303 Valid from: Mon Sep 06 22:30:43 PDT 1999 until: Sun Dec 05 21:30:43 PST 1999 Certificate fingerprints: MD5: 5D:59:57:10:7B:BC:0F:E5:D7:F7:52:21:28:09:14:C9 SHA1: A4:FF:6F:2B:EE:D4:26:2D:26:88:BC:98:87:A7:FB:7E:D3:2F: 1B:42 Trust this certificate? [no]: yes Certificate was added to keystore In a real application, you would likely compare the "fingerprints" in the certificate to the fingerprints supplied from some other trusted source. If they match, the certificate has not been altered. If the fingerprints do not match, you should not trust this certificate. If the JavaMaster needs to obtain the fingerprints so that they can be compared with those on the certificate the user got, the JavaMaster could type
keytool -printcert -file JavaMaster.cer 6. The next step is to set up a policy file based on the code signer's signature and certificate. Acting as the recipient of signed code and the certificate, you need to create a policy file that specifies the code signer and the keystore. Caution The default expiration period for a certificate is 90 days. Unless the person who generated the certificate modified this, you may need to get a new certificate to use signed code periodically.
policytool On the initial screen, you can see the familiar text fields for the policy file and keystore. This time, fill in the keystore with the name Userstore. Be sure you start the policy tool in the directory that contains this keystore database. To do this, choose Change Keystore from the main Edit menu. In the text field for the keystore, type the URL for the keystore, like so:
file:/C:/purejava/source/chap16/Userstore This will likely be different on your system. The keystore type is the same as the default, so leave this text field blank. Figure 16.4 shows the policy tool screen for entering the keystore.
Figure 16.4 Creating a policy file that uses a keystore for signed code.
Caution The keystore location is specified with a URL, so make sure you use forward slashes (/) even if you are working in Windows, where you would expect to use \ to specify a path.
Now click Add Policy Entry. Here you will specify that you are granting permission to code signed by JavaMaster. That is, you are granting permissions to code that resides in a jar file signed by JavaMaster. Figure 16.5 shows the dialog to add the FilePermission to the policy file.
Figure 16.5 Adding a code signer to a policy entry.
Now, as you did before, choose FilePermission and grant the right to read, write, delete, and execute on all files, as shown in Figure 16.6.
Figure 16.6 Granting FilePermissions.
Click Done and then OK. Choose Save As from the File menu and type the name Userpolicy.jp. 7. Now execute the program with the new policy file:
java -Djava.security.policy=Userpolicy.jp -cp signedDriver.jar PrivilegeDriver This executes the PrivilegeDriver class's main method from the PrivilegeDriver.class file in the signedDriver.jar file. You use the cp option to specify the classpath of the class file, which in this case is the jar file signedDriver.jar.
If you have typed everything correctly, the program should run, just as in the first version earlier in this chapter. If not, you should go through the steps again and verify that you typed everything exactly as shown here, except that you may need to change paths.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
public keys security CA (certificate authority) certificates default expiration period fingerprints sending code digitally signing 2nd 3rd 4th 5th 6th 7th 8th digital signatures verifying FilePermissions granting jar files signing keys 2nd keypass option storepass option keystore databases keystores location keytool policy files keystores for signed code Verisign Web site signatures digital verifying signed code policy files signing code digitally 2nd 3rd 4th 5th 6th 7th 8th jar files slashes back (\) forward (/) symmetric keys tools keytool URLs / (forward slash) keystores location verifying digital signatures Verisign Web site Web sites Verisign
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 16. Java Security > Creating Custom Permissions < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229188122103024109240045190
import java.io.*; import java.security.*; class PermMaker { public boolean checkPermissions() { FilePermission filePerm = new java.io.FilePermission ("<<ALL FILES>>", "execute"); // Create a FilePermission object that represents execute // permission on all files. You can have lots of other // possibilities here. Any file or path is acceptable, as // well as other actions like "write", "read", and "delete". AccessController.checkPermission(filePerm); // This will simply return if your program has the permission // or throws an AccessControlException if you do not have the // permission or if the permission values are incorrect or // invalid. ReservePermission rp = new ReservePermission("Professor"); // This same permission must be in a policy file on the user's // system.
// For example, if the program requires a ReservePermission for // a professor in order to reserve a book, then the protection // domain will be checked by this method call at runtime to // verify that such a permission exists. // To test this, run the program both with and without the // policy file Book.jp or another policy file which contains // the ReservePermission with the name "Professor". return true; // You won't get here unless you have the // permission. } public boolean reserveBook(String title) { // Do logic to reserve a book. System.out.println(title + " has been put on three-month reserve"); return true; } } // End class definition for PermMaker. public class PermissionDriver { public static void main(String[] args) { PermMaker pm = new PermMaker(); boolean result = pm.checkPermissions(); if(result == true) pm.reserveBook("Paradise Lost"); System.exit(0); } } // End PermissionDriver class. // Create a Permission subclass that subclasses BasicPermission // since ReservePermission is a simple permission. // It is possible to subclass java.security.Permission as // well if you need actions as well as names in your permission. class ReservePermission extends java.security.BasicPermission { String name; String action; // Although actions are not used by this // permission class, you still need to code // two constructors, one that accepts actions. public ReservePermission(String name) { super(name); // Call the BasicPermission constructor with one parm. } public ReservePermission(String name, String actions) { super(name, actions); // Call the parent constructor with // two parameters. }
} To execute this program and see what happens without the policy file, type
java PermissionDriver This will throw an AccessControlException. To bypass this exception, type the following on the command line:
/* AUTOMATICALLY GENERATED ON Sun Nov 07 23:15:00 PST 1999*/ /* DO NOT EDIT */ grant { permission java.io.FilePermission "<<ALL FILES>>", "execute"; permission java.io.FilePermission "<<ALL FILES>>", "write"; permission ReservePermission "Professor"; }; I recommend that you experiment with this program and policy file (or a backup copy of them!). See what happens if you remove the checkPermission call for FilePermission and you run the program without the policy file. This mechanism means that you can write a program that requires your own permissions. If the user does not have those permissions, he or she cannot run your program properly. In this way, you protect assets from improper access by unauthorized users. Here's the output that I get on my system:
java -Djava.security.policy=Book.jp PermissionDriver Paradise Lost has been put on three-month reserve
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Java > PURE Java 2 > 16. Java Security > Message Digests < BACK
158122020158178182047242169203138024176246252243131023021084157229187015222149239193037038
Message Digests
So far, this chapter has been concerned with permissions. The last topic to be covered is totally unrelated to permissions. A message digest, which is not unique to Java, is a hash value for a message. More specifically, a message digest is the following:
q q q
A unique binary hash value for a specific message. No two messages will produce the same message digest value. A value computed using a message digest algorithm (not covered here) A hash value that cannot be used to determine the original message. Actually, it is possible to get the original message from the message digest, but statistically speaking, the sun will burn out before then, so for all practical purposes, it is impossible.
Because a specific message digest will be computed only for a specific message, you can use a message digest to verify that a message sent to you has not been changed. Be aware, however, that in and of itself, a message digest proves nothing for the text of a message. Consider the following scenario: 1. You create a message and a message digest for it and send it to the head librarian over the Internet, asking the library to buy a certain book. 2. I intercept your message. I change the message to ask the librarian to have the library subscribe to a comic book series. I compute a new message digest value and send the message and digest value to the librarian. 3. The librarian gets the message and the digest. The library's software computes the message digest for the message I sent, and the computed digest matches the digest I sent. 4. The librarian, not having read this chapter, assumes the message is valid because the message digest is valid, and sends you a note, rejecting your goofy request. The point is that, to make a message digest valid, the sender must keep a copy of the digest that the sender can compare to the digest that the recipient of a message got. This is the only way in which a message digest will be useful. MessageDigest Code Example Listing 16.3 shows how to create a MessageDigest object and compute a message digest. You'll then need to figure out how you want to send it to the recipient so that the recipient of your message and message digest can compare the digest she got to the one you supplied separately. If they match, you and she know that the message was not tampered with along the way.
Listing 16.3 Creating a MessageDigest (DigestDriver.java)
import java.io.*; import java.security.*; class DigestMaker { public boolean buildDigest() throws java.security.NoSuchAlgorithmException {
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=123 (1 of 3) [6/2/2002 10:37:33 PM]
MessageDigest md5Digest = MessageDigest.getInstance ("MD5"); // Get an initialized MessageDigest object. Note the use of // a factory-type method, getInstance(). Once you have // a MessageDigest object, you can use it to compute a digest // value. // MD5 is a specific message digest algorithm. try { File ancientTextFile = new File("AncientTexts.txt"); // Get a file on which you can compute a digest. FileInputStream fis = new FileInputStream(ancientTextFile); byte[] message = new byte[ (int) (ancientTextFile.length())]; // Create a byte array the size of the message you want // to make a message digest for. fis.read(message); // Read the whole file into the byte array // called message. md5Digest.update(message); // The update method updates the // MessageDigest object with the specified // byte array (the message contents). byte[] theMsgDigest = md5Digest.digest(); // Perform the final hash computation and related // operations such as padding. System.out.println("The computed message digest is: " + theMsgDigest); System.out.println("The algorithm used to create the " + "message digest is "); System.out.println(md5Digest.getAlgorithm()); // Display the MessageDigest algorithm // You can make your own MessageDigest with a // different algorithm. // This exception is thrown if the algorithm // in the MessageDigest.getInstance() method // is unknown to the system. You can write your // own algorithms. } catch(IOException ioe) { // There's a problem with the file. System.out.println(ioe.getMessage()); ioe.printStackTrace(); } return true; } } // End class definition for DigestDriver.
public class DigestDriver { public static void main(String[] args) throws java.security.NoSuchAlgorithmException // It is rare to put a throws clause in the definition of // main, but main is calling a method that could throw // this exception. This could also be in a try-catch // structure but is done this way to show that it can be done.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=123 (2 of 3) [6/2/2002 10:37:33 PM]
{ DigestMaker dm = new DigestMaker(); boolean result = dm.buildDigest(); System.exit(0); } } // End DigestDriver class.
java DigestDriver The computed message digest is: [B@726f3341 The algorithm used to create the message digest is MD5 After running this, you would send the AncientTexts.txt file along with your message digest to someone else, and the recipient could compare his or her message digest value to the version you saved of it. If they match, you know the file was not tampered with in transit.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 17. JDBC: Java Database Programming < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229187015222149226095090245
Essentially, a vendor is required to implement all the interfaces in java.sql, and your method calls request objects that implement those interfaces.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Java > PURE Java 2 > 17. JDBC: Java Database Programming > Creating a Database Connection < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229187015222148010228174032
It is the responsibility of the DriverManager class to maintain a list of instances of JDBC drivers available to a given JVM at runtime. Essentially, this list provides information about the object reference of a driver and the subprotocol, or database type, that it supports. When a program requests a database connection, the DriverManager goes through the list, asking each driver if it can process the getConnection() request. The search is based on the database URL supplied in the getConnection() call, which is discussed later in this chapter. The DriverManager tells the first driver that satisfies the caller's request to provide the caller with a connection to the database. The DriverManager really doesn't understand the database URL at all. It just gives it to a driver and asks if the driver can deal with the URL properly or not. A JDBC driver gets itself added to the list maintained by the DriverManager using the registerDriver() method. Generally, but not always, this method is called inside a driver's static initializer block, which often calls a private constructor and then calls registerDriver(). A static initializer, as you may recall from Chapter 4, "Data Types," provides code outside the scope of a method. This code is called as soon as the class definition is loaded by the classLoader . A Java program does not generally expressly call the constructor of a JDBC driver. After the driver is registered, it is ready to use. Caution DriverManager calls the first suitable driver in its list to provide a connection. Therefore, if you have multiple drivers loaded, any of which can satisfy a given connection request, be sure that they are in preferential order. You can do this either by loading the driver classes in the order you want to use them or by using the jdbc.drivers system property on the command line:
Here are the steps to create a database connection: 1. Load the database driver. 2. Request a connection using an appropriate database URL. For example, you might code this:
Class.forName("MyDriver.class"); Connection conn = DriverManager.getConnection(dbURL); The forName() method of the class Class causes the named class to be loaded, whether it is a database driver or something else. For a JDBC driver, this call results in the driver's class being loaded. The driver's class also instantiates and registers itself with the DriverManager class. The DriverManager.getConnection() method shows the power of Java interfaces. This method's return type is Connection. A Connection, however, is not a class but an interface. This means that the code is asking for an object, any object whatsoever, as long as it implements the Connection interface. This method frees your code from having to know anything about how the vendor implements the class that implements the Connection interface. You don't care. All you care about is getting a database connection object back. An interface can free you from being tightly coupled to a given
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=126 (1 of 15) [6/2/2002 10:37:49 PM]
class. In this code, I can change the driver class or the implementation of the Connection interface, and the caller never knows the difference. No changes are needed to the client code. If the classLoader cannot find the driver class, you will get a ClassNotFoundException. In this case, check your import statements to be sure that you are pointing to the correct package for the driver. If the getConnection() method fails, you will get a SQLException. After you have a connection, you can go on to get Statement objects, run queries, update tables, and the like. The key to getting the right connection is the database URL. Here is a string that specifies this:
jdbc:sybase:U=Me,P=newpass,S=Doghouse The protocol should be jdbc. The subprotocol identifies the database type. This could be oracle, sybase, msql, jdbc:odbc, and so forth. The parameters following the second colon are optional and depend on the specific driver's requirements. They often consist of user ID, password, and host name. Tip Database connections are fairly expensive to create, so many enterprise-level applications create connection pools in which many connection objects live for use by clients. If a connection requires a user ID and password, it is customary to have the DBA set up roles rather than specific users in the database system. Then, a real user ID is matched in the second tier to a role, and a connection for that type of role is given to the client to use.
Note Many relational database systems might be used here. To enable the greatest number of readers to access the same system, the examples use InstantDB, a 100% pure Java RDBMS, available from http://www.instantdb.co.uk. The code, however, except for the driver, is generic and should work with any RDBMS. When you run the code with a different RDBMS and driver, a problem might occur, because InstantDB does not like a semicolon at the end of statements. Although many JDBC programs commonly use the semicolon, it is not mandated by the SQL standard or the JDBC standard. If your system complains about how statements end, try putting a semicolon at the end of the SQL statement itself (not the Java statement).
Tip Java does not parse SQL code when it compiles it. All that Java does is check that you have a valid string. If you want to have the SQL parsed before you try executing it, use SQLJ, standardized by Oracle. SQLJ first parses and validates your SQL and then wraps it in JDBC code that you can put in any program.
Listing 17.1 shows a simple JDBC program that illustrates the basic steps you need to take in your program: 1. Create a database URL. 2. Load the driver. (Any decent driver instantiates and registers itself with DriverManager). 3. Get a database connection. 4. Create a Statement/PreparedStatement/CallableStatement. 5. Use the statement to execute SQL (or other kinds of statements the database driver and database understand). Tip
Many legacy systems have IMS databases. An IMS database, developed by IBM, is hierarchical and does not understand SQL. There is now, however, a JDBC driver that works with IMS. It lets you call IMS directly from a JDBC statement, just as you would do from a C or COBOL program.
Database Connection and Table Creation: Code Example In order to access a database system, you must connect to it. In order for you to work on data, it must be in a table. Hence, Listing 17.1 shows you how to get a connection to an RDBMS and then shows you how to use DDL in JDBC to create a table. The program then shows you how to use the SQL Insert statement through JDBC to insert data into a table.
Listing 17.1 Database Connection Example Using JDBC (DBDriver1.java)
java.sql.*; //Import all the JDBC classes. following imports are just for InstantDB. jdbc.idbDriver; jdbc.*; java.net.URL; java.io.*; java.util.*;
class TableMaker { Connection con=null; Statement stmt; public boolean createCatalog() { boolean result = true; String dbURL = "jdbc:idb:c:\\purejava\\source\\chap17\\purejava.prp"; // The format of this URL is dictated in part // by Java and in part by InstantDB. // Protocol: jdbc // Sub-protcol: idb // Driver-specific part: location of properties file // used by InstantDB. // The dbURL generally tells Java // the database the driver is for, the user id // and password, the database and the host the // database resides on. try { // Enable DriverManager logging. DriverManager.setLogStream(System.out); // Print DriverManager info to the console. // This is deprecated but useful. Class.forName("jdbc.idbDriver"); // Load the JDBC driver class into // memory. The driver instantiates itself // and registers itself with DriverManager. con = DriverManager.getConnection (dbURL); // Get a database connection from the // appropriate driver. } catch (Exception e) { System.out.println("Failed to
load jdbc.idbDriver driver."); result = false; } // Catch all Exceptions. You could optionally // catch specific Exceptions including SQLException. try { stmt = con.createStatement(); // Now that you have a Connection, create // a Statement object so you can send // SQL calls to the JDBC driver. } catch (SQLException e) { System.err.println("problems connecting to " + dbURL); result = false; } try { // Execute SQL commands to create table, // and insert data. // The Statement method execute() is used // for DDL and CallableStatements. // The method executeUpdate() is for // insert, update, and delete. stmt.execute("DROP TABLE BookCatalog"); stmt.execute("CREATE TABLE BookCatalog ("+ "CallNumber char(4) PRIMARY KEY NOT NULL," + // Not a realistic call number. "Author char (30),"+ "Title varchar(60),"+ "Notes varchar(60)," + "Copies int)"); stmt.executeUpdate( "INSERT INTO BookCatalog " + "values ('Lew1', 'Lewis, C.S.'," + "'Till We Have Faces','Fiction', 3)"); stmt.executeUpdate( "INSERT INTO BookCatalog " + "values ('Col1', 'Colson, Charles'," "'How Now Shall We Live?'," + "'Non-Fiction. Contemporary Issues', 4)"); stmt.executeUpdate( "INSERT INTO BookCatalog " + "values ('Mid1', 'Middleton, J. Richard',"+ "'Truth is Stranger than It Used to Be'," + "'Non-Fiction. Postmodernism', 4)"); stmt.executeUpdate( "INSERT INTO BookCatalog " + "values ('Lew2', 'Lewis, C.S.'," + "'Out of the Silent Planet', null, 2)"); con.close(); } catch (Exception e) { System.err.println("problems with SQL sent to "+ dbURL+ ": "+e.getMessage());
public class DBDriver1 { public static void main(String[] args) { TableMaker tm = new TableMaker(); tm.createCatalog(); System. exit(0); } } Caution You must be sure, if you decide to use InstantDB, to set up the properties file correctly. See the following setup notes for more information.
The properties file provided with the examples is called PureJava.PRP. The main part you might have to change, depending on your system, includes these lines:
!relativeToProperties=1 tablePath=c:\\instantdb\\tables tmpPath=c:\\instantdb\\tmp systemPath=c:\\instantdb\\system indexPath=c:\\instantdb\\index These statements need to be changed to reflect your directory structure if it is not as shown. I created an instantdb directory on my C: drive under Win98. Whether you put it there, or under /export/software/instantdb, is not important. What is important is that the properties file has the correct paths in it. Your classpath must also include
c:\instantDB\Classes\idb.jar;c:\instantDB\Classes\idbf.jar;c:\ instantDB\Classes\idbexmpl.jar In addition, you will probably need to change this line in the program
String dbURL ="jdbc:idb:c:\\purejava\\source\\chap17\\purejava.prp"; to match your directory structure. Since this is the structure I used for testing, and the directory structure will doubtless be different for the site you download the code from, you will most certainly need to change this. Note If you encounter trouble, check the documentation that comes with InstantDB, especially the install.htm file. If you continue to have difficulties, you may want to contact the makers of InstantDB (http://www.instantdb.co.uk/). I got this to work on my system in about 30 minutes. If you already have another RDBMS, you might want to use that instead.
Caution
If you have something like Microsoft Access, you should not use that for the examples here. Access is far too feature-poor to be useful for learning most JDBC features. For example, Access can't do prepared statements. The same can be said of MiniSql.
This program drops the BookCatalog table (if it exists). Depending on your RDBMS, this might be a problem if the table doesn't exist yet. In this case, you should remove the statement and then recompile the program before running it again. The program then creates a table to hold book information and inserts four rows into the table. Note This chapter assumes that you understand the basics of RDBMSs and SQL. If you do not know SQL, there are many books on the subject, such as the Practical SQL Handbook.
The most common SQL statement you might see in a program is Select, which is used to retrieve one or more rows from a table in an RDBMS. A row is a record that consists of one or more columns, or discrete values. When you execute a select statement, you get back a ResultSet object. Like Driver, Connection, and Statement, ResultSet is an interface. Your code knows nothing about the class that implements the ResultSet. Note You cannot pass a ResultSet object from, for example, the tier-two program that owns the connection to a tier-one client. The problem is that the ResultSet is valid only in the context in which it is created. What's more, the tier-one program receiving the ResultSet doesn't know the class of the ResultSet implementation. That means it can't actually map the object. If you need to pass a ResultSet, you must define a class that maps to the columns of the ResultSet, filling in objects of that class with information from successive rows of the ResultSet. You then store these objects in another object. This is frequently done using a Vector, an array built from a Vector, or some other Collection object, such as a TreeMap. You pass this collection to the tier-one client.
A ResultSet represents rows and columns. You step through the rows of the ResultSet with the next() method. You obtain the values of each column with a getxxx() method, where xxx refers to a specific Java datatype, like int, float, String or Object. The java.sql.ResultSet definition lists several getxxx() methods to choose from. You pick the one that maps the SQL data type correctly to the Java datatype. Don't try to use getFloat() and put its value in an int. Suppose that you want to examine the contents of a ResultSet but do not know the schema of the table it came from. You can do this successfully using ResultSetMetadata, another interface in java.sql. After you have obtained a ResultSet, you can get ResultSetMetaData by coding
ResultSet rs = stmt.executeQuery("select * from Employee"); ResultSetMetadata rsmd = rs.getMetadata(); After you've got the ResultSetMetaData, you can create a report that prints out the contents of the table. The ResultSetMetadata object can tell you how many columns there are, what the names of the columns are, the data type of each column, the size of each column name, and so forth. You can then use this information to access each column of each row to get its data. The getxxx() methods allow you to specify either a column number or column name to access a given column. You can access the columns in any order, but it is best to access them in a left-to-right fashion. I recommend that you only use the column name to access a column in a getxxx() method. The reason is that some RDBMSs allow you to drop a column. That can mean, for example, that what was column 4 becomes column 3. If you use column numbers to access data, you might end up printing a report based on hardcoded column numbers that displays job title where gender used to be. When you use column names, if a column is dropped, you get a SQLException. The exception is inconvenient, but it prevents you from using incorrect data. Prior to JDBC 2.0, which is part of Java 2, you could only go forward in a ResultSet. That is, you called next() to get to the first row, and then next() to get to each successive row until you reached the end of the ResultSet. When you got to
the end of the ResultSet, that was it. You could not go backwards. Also, if you used the same ResultSet object reference again, the first ResultSet, as you might imagine, was destroyed. A small but growing number of JDBC drivers support JDBC 2.0. I'll say more about this later, but for right now, the examples assume the JDBC 1.0 type of ResultSet. Note Most of the new JDBC 2.0 features are in interfaces specified in javax.sql. They are considered standard extensions that a JVM vendor can choose to implement or not implement. Therefore, it's safer, as of September 1999, to use the approach that is sure to work with any JDBC driver. This includes treating ResultSets as a read-once set of rows.
JDBC supports three types of SQL statements (although your driver might not support all three):
q q q
Before I explain each of these and show you how to use them in a Java program, let's look at how a SQL statement is treated. The Java program passes the SQL statement as a string to the JDBC driver. The JDBC driver, after some processing (which varies with the driver and the driver type), sends the SQL statement on to the database engine. At the database engine, the statement is parsed, compiled, and optimized. The output of this process is a query plan, which describes how the database engine will actually go about getting the requested data. Several factors go into the contents of the query plan, such as whether useful indexes exist for the table, the database engine's own capabilities (such as DB2/MVS's sequential pre-fetch capability), and other factors. This query plan is then cached, either in the database engine or the driver instance. This is all invisible to the Java program, except that the process takes time that affects the performance of the Java program. The premise of a PreparedStatement in Java, just as for a prepared statement in SQL, is that, if you need to execute the same statement over and overchanging only parameter values, you shouldn't be required to go through the parse-compile-optimize cycle over and over. Instead, a PreparedStatement is parsed, compiled, optimized and has a query plan generated for it just once. Here's an example. In a Customer table, you have a list of customer names whose data needs to be changed to show that these customers get a discount on shipping. You use the customer name to find the customer's row, and then you update the shipping charges percentage column. You might issue 10,000 update statements, each one being parsed, compiled, and optimized. With a PreparedStatement, however, you only need to prepare the statement once. That action parses, compiles, and optimizes the statement, and it also generates the statement's query plan. Now, each time you want to use the PreparedStatement that contains the SQL update statement, you use only the setxxx() methods to change the value of the customer name and submit the statement. In general, you should find the performance of PreparedStatements superior to dynamic statements, which must be complied every single time. Caution Not all databases systems support PreparedStatements. Some lightweight databases, such as Microsoft Access, do not understand PreparedStatements. MiniSql also does not understand a PreparedStatement. If your RDBMS does not know how to use a PreparedStatement, I advise you to get a real database engine. Always verify that both your RDBMS and your JDBC driver can handle PreparedStatements. If you attempt to use a PreparedStatement on a driver or RDBMS that does not understand a PreparedStatement, a SQLException will be thrown.
A CallableStatement is Java's mechanism for handling a stored procedure. A stored procedure is generally one or more SQL statements stored as one function in a compiled form inside a database engine. You simply call the stored procedure as you call a method. As in a PreparedStatement, you use question marks (?) to specify the position of parameters are replacing. Because stored procedures vary in how a vendor implements them or how you call them, they will be the least-portable kind of statement across RDBMSs. Note Although the SQL 1992 standard permits stored procedures to take parameters of types in, out, and inout, Java only allows types in and out. You must call a special method, registerOutParameter() to use a parameter of type out,
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=126 (7 of 15) [6/2/2002 10:37:49 PM]
because it is contrary to the way Java normally passes parameters or processes parameters.
ResultSet and ResultSetMetadata: Code Example JDBC provides interfaces for both the ResultSet, that contains the rows you retrieved from the database, and for ResultSetMetadata, which describes the ResultSet. The metadata can be used to find out several pieces of information about the rows you got back, such as how many columns there are, the data type of each column, and the name of each column. In fact, you could use this data to query a table you knew nothing about and create a report showing column names and displaying the contents of the rows without ever having seen the table schema (the definition for the table). Listing 17.2 shows how to obtain and process a ResultSet including ResultSetMetadata.
Listing 17.2 Example of Processing a ResultSet with next() and get Methods (DBDriver2.java)
java.sql.*; //Import all the JDBC classes. following imports are just for InstantDB. jdbc.idbDriver; jdbc.*; java.net.URL; java.io.*; java.util.*;
class TableReader { Connection con=null; Statement stmt; public boolean readTable() { boolean result = true; String dbURL = "jdbc:idb:c:\\purejava\\source\\chap17\\purejava.prp"; // The format of this URL is dictated in part by // Java and in part by InstantDB. // Protocol: jdbc // Subprotcol: idb // Driver-specific part: location of properties file // used by InstantDB. try { // Print DriverManager info to the console // This is deprecated but useful. Class.forName("jdbc.idbDriver"); // Load the JDBC driver class into // memory. The driver instantiates itself // and registers itself with DriverManager. con = DriverManager.getConnection (dbURL); // Get a database connection from the // appropriate driver. } catch (Exception e) { // ClassNotFoundException if the // classloader can't find your driver class. // SQLException if you simply couldn't // connect. In the first case, // check your classpath. // In the second case, validate
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=126 (8 of 15) [6/2/2002 10:37:49 PM]
// the contents of the database URL // (and for InstantDB the properties file too). System.out.println ("Failed to load jdbc.idbDriver driver."); result = false; } try { stmt = con.createStatement(); // Now that you have a Connection, create // a Statement object so you can send // SQL calls to the JDBC driver. } catch (SQLException e) { System.err.println("problems connecting to " + dbURL); result = false; } try { // Retrieve all the rows from the table with // a ResultSet and print the contents of // the ResultSet. ResultSet rs = stmt.executeQuery( "SELECT * from BookCatalog"); // Get the metadata. // You must have a ResultSet object // to get a ResultSet. ResultSetMetaData rsmd = rs.getMetaData(); System.out.println("Column Number" + "\tColumn Name" + "\t\t Column Type"); for(int i = 1; i <= rsmd. getColumnCount(); i++) { System.out.println(i +"\t\t" + rsmd.getColumnName(i) + "\t\t\t" + rsmd.getColumnTypeName(i)); } // getColumnCount() returns the number of columns. // getColumnName()_returns the name of the column. // getColumnTypeName() returns the SQL column. // type by name System.out.println("Call #" + "\tAuthor" + "\t\tTitle" + "\tNotes" + "\tCopies"); while(rs.next() == true) { System.out.print(rs.getString("CallNumber")); System.out.print("\t" + rs.getString("Author")); System.out.print("\t" + rs.getString("Title")); System.out.print("\t" + rs.getString("Notes")); System.out.println("\t" + rs.getString ("Copies")); } con.close(); } catch (Exception e) { System.err.println("problems with SQL sent to "+ dbURL+
": "+e.getMessage()); result = false; } return result; } } // End class definition for TableMaker.
public class DBDriver2 { public static void main(String[] args) { TableReader tr = new TableReader(); tr.readTable(); System.exit(0); } } As this example shows, you use a ResultSet object, meaning an object that implements the ResultSet interface, to get the rows back from the SQL query that uses Select. Before the program interrogates the ResultSet contents, it creates a ResultSetMetaData object and prints some basic information from a ResultSet. This code gets the count of columns, the names of the columns, and the names of the SQL data types for each column. You can use this to create a report. If you look at the various getxxx() methods, you can see that the code uses getString() to return the value from both charand varchar-type columns. On the other hand, to retrieve an int, you use getInt(). It is important to map the column data type to an appropriate Java data type. In some cases, one Java data type will map to multiple SQL types. You do not have to get every column. I could have gotten just the Title column. Note If you decide to use column numbers rather than column names, be aware that column numbers, unlike numbers in an array, start at 1, not 0.
It is possible to retrieve data from a column as an unknown type by using getObject(). These select statements are dynamic statements. Each is separately parsed, compiled, and optimized to generate a query plan, and then executed. As stated earlier, a PreparedStatement will generally be more efficient. So Listing 17.3 shows how to create and use a PreparedStatement. When you create a PreparedStatement, you need to supply parameters, as mentioned earlier. You set the values of those parameters with setxxx() methods, which correspond to the getxxx() methods described earlier. To set an arbitrary data value you can use setObject(). Here is the basic way to create a PreparedStatement:
PreparedStatement updateCopies = con.prepareStatement( "UPDATE BookCatalog SET Copies = Copies+1 WHERE Author = ?"); To set the parameter values, you use a setxxx() statement like this:
updateCopies.setString(1, "Lewis, C.S."); The 1 in the setString() call refers to the parameter number in the PreparedStatement. It has nothing to do with the column number of the value. The second value, in this case a String, is the value that should be substituted for the ? before the SQL statement is executed. PreparedStatement: Code Example Although the next sample program shows the PreparedStatement being done only three times, typically it is probably not worth the overhead for just a few executions. The exact break-even point for doing dynamic statements rather than PreparedStatements depends on the specific JDBC driver, but I'd guess you need to do the PreparedStatement at least 10 times to make it worth the trouble. To execute the PreparedStatement, you would code this:
int rowsUpdated = updateCopies.executeUpdate(); If you use a SQL insert, update, or delete, you should get an int back that tells you how many rows are affected. You should always test this value. If it is less than 1, the SQL statement did nothing, which probably indicates an error.
Listing 17.3 Using a PreparedStatement Object in JDBC (DBDriver3.java)
java.sql.*; //Import all the JDBC classes. following imports are just for InstantDB. jdbc.idbDriver; jdbc.*; java.net.URL; java.io.*; java.util.*;
class PreparedWorker { Connection con=null; PreparedStatement updateCopies; Statement stmt; public boolean updateTable() { boolean result = true; String dbURL = "jdbc:idb:c:\\purejava\\source\\chap17\\purejava.prp"; // The format of this URL is dictated in part by // Java and in part by InstantDB. // Protocol: jdbc // Sub-protcol: idb // Driver-specific part: location of properties file // used by InstantDB. try { // Print DriverManager info to the console. // This is deprecated but useful. Class.forName("jdbc.idbDriver"); // Load the JDBC driver class into // memory. The driver instantiates itself // and registers itself with DriverManager. con = DriverManager.getConnection (dbURL); // Get a database connection from the // appropriate driver. } catch (Exception e) { System.out.println ("Failed to load jdbc.idbDriver driver."); result = false; } try { // First, display data before changing it. // This uses the same code as in DBDriver2.java. stmt = con.createStatement(); ResultSet rs = stmt.executeQuery( "SELECT * from BookCatalog"); System.out.println("Call #" + "\tAuthor" + "\t\tTitle" +
"\tNotes" + "\tCopies"); while(rs.next() == true) { System.out.print(rs.getString("CallNumber")); System.out.print("\t" + rs.getString("Author")); System.out.print("\t" + rs.getString("Title")); System.out.print("\t" + rs.getString("Notes")); System.out.println("\t" + rs.getString("Copies")); } // Now that you have a Connection, create // a PreparedStatement to save statement // execution time. updateCopies = con.prepareStatement( "UPDATE BookCatalog SET " + "Copies = Copies+1 WHERE Author = ?"); // Note that you write the SQL statement before you // try to execute it. } catch (SQLException e) { System.err.println("problems connecting to " + dbURL); result = false; } try { // A PreparedStatement provides execute(), // executeUpdate() and executeQuery(). Since we are // using an insert statement, use executeUpdate(). updateCopies.setString(1, "Lewis, C.S."); int rowsUpdated = updateCopies.executeUpdate(); if(rowsUpdated < 1) System.out.println("Failed to update BookCatalog " + "table for number of copies"); updateCopies.setString(1, "Colson, Charles"); rowsUpdated = updateCopies.executeUpdate(); if(rowsUpdated < 1) System.out.println("Failed to update BookCatalog " + "table for number of copies"); updateCopies.setString(1, "Middleton, J. Richard"); rowsUpdated = updateCopies.executeUpdate(); if(rowsUpdated < 1) System.out.println("Failed to update BookCatalog " + "table for number of copies"); // Now do a select to show that the change // took place. ResultSet rs = stmt.executeQuery( "SELECT * from BookCatalog"); // We are not using a scrollable ResultSet, // so we need to get a new ResultSet. System.out.println("Call #" + "\tAuthor" + "\t\tTitle" + "\tNotes" + "\tCopies"); while(rs.next() == true) { System.out.print(rs.getString ("CallNumber")); System.out.print("\t" + rs.getString ("Author")); System.out.print("\t" + rs.getString ("Title")); System.out.print("\t" + rs.getString ("Notes"));
System.out.println("\t" + rs.getString("Copies")); } con.close(); } catch (Exception e) { System.err.println("problems with SQL sent to "+ dbURL+ ": "+e.getMessage()); result = false; } return result; } } // End class definition for TableMaker.
public class DBDriver3 { public static void main(String[] args) { PreparedWorker pw = new PreparedWorker(); pw.updateTable(); System.exit(0); } } When you run this code, you see the contents of the BookCatalog table, followed by updates (via a PreparedStatement) to the BookCatalog table, followed by a query to show the newly updated contents of the BookCatalog table. As noted before, you use a setxxx() method to put a parameter value into the PreparedStatement. You would probably want to use a PreparedStatement in a loop. You then retrieve, from a file or some other source, the changing parameter values for each loop.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
creating 2nd 3rd 4th 5th 6th 7th PreparedStatement code 2nd 3rd 4th 5th ResultSet code 2nd 3rd 4th 5th ResultSetMetadata code 2nd 3rd 4th 5th tables BookCatalog code to create 2nd 3rd 4th 5th 6th 7th 8th schema DriverManager class 2nd 3rd DriverManager.getConnection() method files properties setting up in InstantDB forName() method getConnection() method getFloat() method getInt() method getObject() method getxxx() method 2nd InstantDB properties files setting up Web site InstantDB Web site interfaces Connection javax.sql javax.sql interfaces JDBC callableStatement databases connection pools connections, code 2nd 3rd 4th 5th 6th 7th 8th connections, creating 2nd 3rd 4th connections;creating 2nd 3rd PreparedStatement, code 2nd 3rd 4th 5th ResultSet, code 2nd 3rd 4th 5th ResultSetMetadata, code 2nd 3rd 4th 5th tables, code to create 2nd 3rd 4th 5th 6th 7th 8th InstantDB Web site InstantDB Web site javax.sql interfaces Microsoft Access PreparedStatement 2nd 3rd code 2nd 3rd 4th 5th ResultSet code 2nd 3rd 4th 5th objects;passing ResultSetMetadata code 2nd 3rd 4th 5th ResultSetMetaData tables, schema SQL (Structured Query Language) statements supporting methods DriverManager.getConnection() forName() getConnection() getFloat() getInt() getObject()
getxxx() 2nd next() 2nd registerDriver() registerOutParameter() ResultSet setxxx() Microsoft Access JDBC (Java Database Connectivity) next() method 2nd objects ResultSet passing parameters procedures (stored, SQL (Structured Querey Language) parsing SQL code PreparedStatement 2nd 3rd code 2nd 3rd 4th 5th procedures (stored, SQL (Structured Querey Language) parameters properties files setting up in InstantDB question mark (?) registerDriver() method registerOutParameter() method ResultSet code 2nd 3rd 4th 5th methods next() method objects passing ResultSetMetaData tables, schema ResultSetMetadata code 2nd 3rd 4th 5th ResultSetMetaData tables schema Select statement setxxx() methods SQL procedures (stored) parameters statements supported by JDBC (Java Database Connectivity) SQL code parsing statements CallableStatement PreparedStatement 2nd 3rd code 2nd 3rd 4th 5th Select SQL (Structured Query Language) supported by JDBC (Java Database Connectivity) Structured Query Language. tables BookCatalog creating code 2nd 3rd 4th 5th 6th 7th 8th schema Web sites InstantDB 2nd
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 17. JDBC: Java Database Programming > Using Callable Statements < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229187009188107200083018187
Suppose that a stored procedure is written called SHOW_NEW_BOOKS, which shows new books by category and date range. Your Java program is going to call this stored procedure. JDBC uses a CallableStatement to call the stored procedure. A CallableStatement includes the name of the stored procedure and, optionally, parameter names. Like a PreparedStatement, the parameters are indicated by ? in the CallableStatement. Assume that the SHOW_NEW_BOOKS stored procedure takes two parameters: the subject category ID and the date from which to begin displaying titles. The date is in the form of a java.sql.Date, a subclass of java.util.Date. Here's how the CallableStatement might look:
java.sql.Date beginDate = java.sql.Date.valueOf("1999-01-01"); // Since you need the java.sql.Date, not the java.util.Date, make // that explicit, in case your program also imports java.util. // Otherwise, the compiler might not know which one you mean, and you // could get a compile error. CallableStatement cs = con.prepareCall("{ call SHOW_NEW_BOOKS(? ,?)}"); cs.setString(1,"Contemporary Issues"); // Set subject parameter. cs.setDate(2,beginDate); // Set date to start search from. ResultSet rs = cs.execute(); A stored procedure can return a ResultSet or a specific result and modify the parameters you submit. These parameters must be registered with
registerOutParameter(int parameterIndex, int sqlType) This is necessary only if you want to have the stored procedure modify this value. If the stored procedure returns a ResultSet, you use the familiar getxxx() methods to retrieve values from the rows in the ResultSet. A stored procedure can be quite handy if multiple applications call the same SQL statements to accomplish the same purpose. Your organization can have your group's best SQL programmer put together a well-tuned stored procedure.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Java > PURE Java 2 > 17. JDBC: Java Database Programming > JDBC 2.0 Techniques < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229187008249134006099217046
Scrollable ResultSetsYou can move both forward and backward in a ResultSet. Programmatic updates to a ResultSetYou can modify the contents of a ResultSet using Java method calls. This automatically updates the underlying relational table. Batch updatesYou can submit multiple DML statements, like insert or update, at once. User-defined data typesYou can define your own data types and map them to Java classes. SQL3 data typesYou can use new SQL3 data types, such as blobs (Binary Large Objects) and clobs (Character Large Objects).
q q q
Scrollable ResultSet JDBC 2.0 provides support for a scrollable ResultSet. Using this, you can do the following:
q q q q
Skip down to a row. Skip up to a row. Go to the top of the ResultSet. Go to the bottom of the ResultSet.
You can move around to arbitrary locations in the ResultSet without having to query again. This feature was not in JDBC 1.0 because not enough vendors supported it at that time. Tip You can use scrollable ResultSets to determine how many rows are in your ResultSet before processing it. All you do is call last() on the ResultSet to get to the last row. Next, get the row number. That will tell you how many rows are in your ResultSet.
Scrollable ResultSet: Code Example No treatment of JDBC 2.0 would be complete without an example of how to use this important feature. This feature means that you no longer have to do a query twiceonce to count the rows and a second time to process them. To use a scrollable ResultSet, you have to require that a Statement object support scrolling, doing something similar to the following:
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); The createStatement() call is similar to the one you've already seen, except that it adds two new arguments. The first argument specifies the scrolling for the ResultSet and is a public static final int defined in the ResultSet
TYPE_FORWARD_ONLYCan only scroll the ResultSet forward, which is the default. TYPE_SCROLL_INSENSITIVECan scroll in any direction, but the ResultSet is generally not modified if others modify the underlying table. TYPE_SCROLLCan be scrolled in any direction. The ResultSet is generally modified if others modify the underlying table.
The second parameter specifies whether the ResultSet can be updated. Here are the two possible values:
q q
After this, you can get a ResultSet as usual and can move around with methods like absolute() to go to a specific row, relative() to go to a row relative to the current one, previous() to go backwards one row, and the like. Listing 17.4 shows an example of how to create and use a scrollable ResultSet.
Listing 17.4 JDBC 2.0-Only Scrollable ResultSet Example (DBDriver4.java)
// Create and use a scrollable ResultSet. import java.sql.*; //Import all the JDBC classes. // The following imports are just for InstantDB. import jdbc.idbDriver; import java.net.URL; import java.io.*; import java.util.*; class ScrollingReader { Connection con=null; Statement stmt; public boolean readTable() { boolean result = true; String dbURL = "jdbc:idb:c:\\purejava\\cdrom\\chap17\\purejava.prp"; // The format of this URL is dictated // in part by Java and in part by InstantDB. // Protocol: jdbc // Subprotcol: idb // Driver-specific part: location of properties file // used by Instantdb. try { // Print DriverManager info to the console. // This is deprecated but useful. Class.forName("jdbc.idbDriver"); // Load the JDBC driver class into // memory. The driver instantiates itself // and registers itself with DriverManager. con = DriverManager.getConnection (dbURL); // Get a database connection from the // appropriate driver. } catch (Exception e) { System.out.println ("Failed to load jdbc.idbDriver driver."); result = false; }
try { stmt = con.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); // Now that you have a Connection, create // a Statement object so you can send // SQL calls to the JDBC driver. // This Statement will create a ResultSet that is: // 1. Scrollable and sensitive to modifications to // the underlying table. This could also be // TYPE_SCROLL_INSENSITIVE in this particular case. // 2. Read-only ResultSet. } catch (SQLException e) { // Either the program failed to get // a connection, which is probably a // database URL or database setup problem, or // the creation of the Statement // object failed (possibly because the // database engine or driver does not support // scrollable ResultSets). System.err.println("problems connecting to " + dbURL); result = false; } try { // Retrieve all the rows from the table with // a ResultSet and print the contents of // the ResultSet. ResultSet rs = stmt.executeQuery( "SELECT * from BookCatalog"); // Now scroll around the ResultSet. rs.absolute(3); // Go to a specific row. int rowNumb = 0; rowNumb = rs.getRow(); // Get the row number, which should be 3. System.out.println ("The row number, which should be 3 is: + rowNumb); System.out.println("Title: " + rs.getString("Title")); // Print the title to show // that the row has actually changed. rs.relative(-2); // Go backwards two rows. rowNumb = rs.getRow(); // Get the row number which should be 1. System.out.println ("The row number, which should be 1 is: + rowNumb); System.out.println("Title: " + rs.getString("Title")); rs.next(); // Move forward to row 3. rs.next(); rs.previous(); // Move backward to row 2. rowNumb = rs.getRow(); // Get the row number, which should be 2.
"
"
System.out.println ("The row number, which should be 2 is: + rowNumb); System.out.println("Title: " + rs.getString("Title")); // Print the title to show // that the row has actually changed. // Now test to see if we are still // in the ResultSet bounds. rs.next(); // Move forward one row. rs.last(); // Go to the last row. rowNumb = rs.getRow(); // Find out how many rows are in the ResultSet. System.out.println("There are " + rowNumb + "rows in the ResultSet"); if (!rs.isAfterLast()) { // Have we walked off the // bottom of the ResultSet? // If not, show a row. System.out.print(rs.getString ("CallNumber")); System.out.print("\t" + rs.getString ("Author")); System.out.print("\t" + rs.getString ("Title")); System.out.print("\t" + rs.getString ("Notes")); System.out.println("\t" + rs.getString("Copies")); } // Now go past the end of the ResultSet and work // backwards, printing each row. System.out.println ("Here are the rows in reverse order"); rs.afterLast(); while(rs.previous() == true) { System.out.print(rs.getString ("CallNumber")); System.out.print("\t" + rs.getString ("Author")); System.out.print("\t" + rs.getString ("Title")); System.out.print("\t" + rs.getString ("Notes")); System.out.println("\t" + rs.getString("Copies")); } con.close(); // Closing the Connection also destroys the ResultSet and // Statement objects, at least as usable entities. } catch (SQLException e) { // First, show SQLState. System.err.println("SQLState: " + e.getSQLState()); System.err.println("problems with SQL sent to "+ dbURL+ ": "+e.getMessage()); result = false; } return result; } } // End class definition for ScrollingReader.
"
{ ScrollingReader scrRdr = new ScrollingReader(); scrRdr.readTable(); System.exit(0); } } Here's the output as it appears on my system:
InstantDB - Version 3.11 beta 1 Copyright (c) 1997-1999 Instant Computer Solutions Ltd. main SELECT * from BookCatalog The row number, which should be 3 is: 3 Title: Truth is Stranger than It Used to Be The row number, which should be 1 is: 1 Title: Till We Have Faces The row number, which should be 2 is: 2 Title: How Now Shall We Live? There are 4rows in the ResultSet Lew2 Lewis, C.S. Out of the Silent Planet null 2 Here are the rows in reverse order Lew2 Lewis, C.S. Out of the Silent Planet null 2 Mid1 MIddleton, J. Richard Truth is Stranger than It Used to Be Non-Fiction. PostModernism 4 Col1 Colson, Charles How Now Shall We Live? Non-Fiction. Contemporary Issues 4 Lew1 Lewis, C.S. Till We Have Faces Fiction 3 Batch Updates JDBC 2.0 allows for batch updates. In the code in DBDriver1.java, I called insert five separate times. Using a batch update, I could have made just one call that passed all the insert statements to the JDBC driver and on to the database engine. Making fewer calls improves your program's performance. Modifiable ResultSets JDBC 2.0 allows you to modify a ResultSet directly and have the changes reflected in the underlying table automatically. You can insert a row into a ResultSet, delete a row from a ResultSet, or change a value in a column in a ResultSet, and Java automatically changes the underlying table for you. This allows you to work with the data in a Java-centric way, after you've obtained the ResultSet. You are not required to make SQL calls for each thing. Note As of September 1999, InstantDB allows you to use updatable ResultSets. Although the changes to the ResultSet do affect the underlying table, the ResultSet itself will not show the changes until you repeat the query. So you will have to do a query to verify that your changes actually took effect.
Updatable ResultSet: Code Example Unlike the example in Listing 17.4, the ResultSet must be instantiated with CONCUR_UPDATABLE to allow the ResultSet to be updated. The createStatement() call in Listing 17.5 creates a Statement object that generates an updatable ResultSet. The ResultSet is also scrollable. This is not required for an updatable ResultSet, but if you are changing the ResultSet, you will probably find it helpful to be able to scroll around the ResultSet. As well as updating a table, you can insert rows into it or delete rows from it. You insert a row into an updatable ResultSet using insertRow(). You may think of this as a separate buffer for a row; it is not a part of the ResultSet returned from the query as such. To insert a row, you move to the insert row by calling moveToInsertRow(), use updatexxx() methods to set the column values, and then use insertRow() to insert the row into the ResultSet. The driver will insert the row into the underlying table.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=128 (5 of 11) [6/2/2002 10:44:31 PM]
Listing 17.5 gives an example of how to modify a ResultSet. Caution Simply asking for an updatable ResultSet does not guarantee that you will get one. The JDBC driver might not support this feature and might return a read-only ResultSet to you. You can help ensure that your ResultSet will be updatable by specifying a SQL query that includes the primary key column in the columns selected and does a select from only one table. You can test to see if you received an updatable ResultSet by coding the following:
int concurType = rs.getConcurrency(); If concurType equals 1007, the ResultSet is read-only. If it equals 1008, the ResultSet is updatable.
To change a given row, you must move to it. If you decide after updating an existing row that you want to cancel the updates, call cancelRowUpdates() before you call updateRow(), which must be called normally in order to permanently modify the row.
Listing 17.5 JDBC 2.0-Only Updatable ResultSet Example (DBDriver5.java)
// Create and use a scrollable ResultSet. import java.sql.*; //Import all the JDBC classes. // The following imports are just for InstantDB. import jdbc.idbDriver; import java.net.URL; import java.io.*; import java.util.*; class Updater { Connection con=null; Statement stmt; public boolean modifyResultSet() { boolean result = true; String dbURL = "jdbc:idb:c:\\purejava\\cdrom\\chap17\\purejava.prp"; // The format of this URL is dictated in part by // Java and in part by InstantDB. // Protocol: jdbc // Sub-protcol: idb // Driver-specific part: location of properties file // used by Instantdb. try { // Print DriverManager info to the console. // This is deprecated but useful. Class.forName("jdbc.idbDriver"); // Load the JDBC driver class into // memory. The driver instantiates itself // and registers itself with DriverManager. con = DriverManager.getConnection (dbURL); // Get a database connection from the // appropriate driver. } catch (Exception e) { System.out.println ("Failed to load jdbc.idbDriver driver."); result = false;
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=128 (6 of 11) [6/2/2002 10:44:31 PM]
} try { con.setAutoCommit(false); // Prevent auto-commit of work. Instead, do your // own transaction management of the resource // manager - in this case, the database. stmt = con.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); // Now that you have a Connection, create // a Statement object so you can send // SQL calls to the JDBC driver. // This Statement will create a ResultSet that is: // 1. Scrollable and sensitive to modifications to // the underlying table. This could also be // TYPE_SCROLL_INSENSITIVE in this particular case. // 2. Updatable ResultSet. } catch (SQLException e) { // Either the program failed to get a connection, // which is probably a database URL or database // setup problem, or the creation // of the Statement object failed (possibly // because the database engine or driver does not // support scrollable ResultSets). System.err.println("problems connecting to " + dbURL); result = false; } try { // Retrieve all the rows from the table with // a ResultSet and print the contents of // the ResultSet. ResultSet rs = stmt.executeQuery( "SELECT * from BookCatalog"); // Show the contents of the ResultSet. while(rs.next() == true) { System.out.print(rs.getString ("CallNumber")); System.out.print("\t" + rs.getString ("Author")); System.out.print("\t" + rs.getString ("Title")); System.out.print("\t" + rs.getString ("Notes")); System.out.println("\t" + rs.getString("Copies")); } // Now update a specific row. rs.absolute(3); // Go to a specific row. // Change the number of copies. rs.updateInt("Copies", 8); // Update the // ResultSet. // The updatexxx methods on a ResultSet // generally take two parameters: // a column name or number, and a value. // updateInt() is used here to change // the int value in the Copies column. rs.updateRow();
// // // // // // // // // //
You MUST call updateRow() before moving the cursor for this row to be updated. If you fail to call this, the updated values will be lost, and the row will return to its previous state. If you decide that you don't want to keep the changes to a row, you can call cancelRowUpdates(), to cancel all your updates to the row, as long as you have NOT called updateRow() yet.
// Now insert a row into the ResultSet. rs.moveToInsertRow(); // Move to the inset row. rs.updateString("CallNumber", "Pasc"); rs.updateString("Author","Pascal, Blaise" ); rs.updateString("Title", "Pensees"); rs.updateString("Notes", "Philosophy"); rs.updateInt("Copies", 2); rs.insertRow(); // Use an updatexxx method for every column. // Then call insertRow() to insert the row into // the ResultSet. The underlying table is not // changed until insertRow() is called. // The ResultSet keeps track of the row you were // on before calling moveToInsertRow(). While you // are on the insert row, you can call // moveToCurrentRow() to return to the current // row. rs.moveToCurrentRow(); System.out.println("The current row number is: " + rs.getRow()); // Display the current row number. // Move back to the current row after the insert. // You could also move to a different row using // any of the methods for a scrollable ResultSet, // like first(). rs.first(); // Move to the first row. rs.deleteRow(); // Delete a row from a ResultSet. // The visibility of changes you make depends on // the database driver and, to an extent, the // isolation level set. // In order to see the changes for sure, close the // ResultSet and repeat the query. con.commit(); // Commit your work. rs.close(); // Close the ResultSet and do a new query. // In general, you don't have to do this, because // doing a new query destroys the old ResultSet, // but this is cleaner. rs = stmt.executeQuery( "SELECT * from BookCatalog"); // Show the contents of the ResultSet. while(rs.next() == true) { System.out.print(rs.getString ("CallNumber"));
} con.close(); // Closing the Connection also destroys the ResultSet and // Statement objects, at least as usable entities. } catch (SQLException e) { // First show SQLState. System.err.println("SQLState: " + e.getSQLState()); System.err.println("problems with SQL sent to "+ dbURL+ ": "+e.getMessage()); result = false; } return result; } } // End class definition for Updater.
public class DBDriver5 { public static void main(String[] args) { Updater updtr = new Updater(); updtr.modifyResultSet(); System.exit(0); } } Here is the output on my system:
InstantDB - Version 3.11 beta 1 Copyright (c) 1997-1999 Instant Computer Solutions Ltd. main SELECT * from BookCatalog Lew1 Lewis, C.S. Till We Have Faces Fiction 3 Col1 Colson, Charles How Now Shall We Live? Non-Fiction. Contemporary Issues 4 Mid1 MIddleton, J. Richard Truth is Stranger than It Used to Be Non-Fiction. PostModernism 4 Lew2 Lewis, C.S. Out of the Silent Planet null 2 The current row number is: 3 main SELECT * from BookCatalog Col1 Colson, Charles How Now Shall We Live? Non-Fiction. Contemporary Issues 4 Lew2 Lewis, C.S. Out of the Silent Planet null 2 Mid1 Middleton, J. Richard Truth is Stranger than It Used to Be Non-Fiction. PostModernism 8 Pasc Pascal, Blaise Pensees Philosophy 2 You can see that the number of copies of Truth is Stranger than It Used to Be has been changed to 8, that the book Till We Have Faces is gone, and that Pascal's Pensees has been added. We have updated the table by updating the ResultSet using Java methods rather than SQL calls. Note The order of rows in a ResultSet has nothing to do with the order of rows in the underlying table. The driver keeps track of
which rows were selected and updates the rows in the underlying table. Because of this, if you insert a row into a ResultSet, there is no way to know where in the table the row will be inserted. The order of rows in the underlying table is indeterminate.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
code 2nd 3rd 4th 5th 6th 7th ResultSets updating in InstantDB scrolling ResultSet 2nd 3rd code 2nd 3rd 4th 5th 6th updatexxx() methods updating batches ResultSet code 2nd 3rd 4th 5th 6th 7th ResultSets in InstantDB
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 17. JDBC: Java Database Programming > Other Important Points to Remember < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229187008249131030101091018
integrity. What you gain is a lot more important than the tiny bit of coding you save by using autoCommit. Set the Isolation Level You should always set the isolation level of your program's connection to the database by calling
myConnection.setTransactionIsolation(isolation level); A transaction isolation level refers to how exclusive the control of the data needs to be. Five isolation levels are available in JDBC, although a given RDBMS may not support them all:
q q
TRANSACTION_NONENo transaction support. TRANSACTION_READ_COMMITTEDNo dirty reads are allowed, but phantom rows and nonrepeatable reads are permitted. A dirty read refers not to something salacious, but to reading data that is in an inconsistent state. For example, a bank might want to know the total of all funds in all accounts, even if the number is approximate because it is constantly changing. If a result that is pretty close to accurate is good enough, you can increase concurrency by permitting dirty reads. A phantom row means that a row has been deleted from the rows that represent your ResultSet. This means you are looking at a no-longer-existent row. A repeatable read means the same rows are read over and over without changes to the data between reads. This reduces concurrency. You need to strike the appropriate balance in your application between concurrency and exclusive control of the data. Of course, this is also influenced by how small a unit of a table your RDBMS can lock, ranging from a row to the whole table.
TRANSACTION_READ_UNCOMMITTEDDirty reads, nonrepeatable reads, and phantom rows are permitted. TRANSACTION_REPEATABLE_READNo nonrepeatable reads or dirty reads are allowed, but phantom rows are allowed. TRANSACTION_SERIALIZABLENo dirty reads, phantom rows, or nonrepeatable reads are allowed.
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229187007012016036040028170
These classes can step through text based on characters, words, sentences, or line breaks. This package is primarily designed for providing internationalization in your application. Specifically, you can write your application in a language- and locale-independent way, dynamically formatting data for a specific locale on-the-fly. Note This chapter does not examine other issues in internationalization, such as using foreign fonts, for which Swing provides support.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
All Books
Java > PURE Java 2 > 18. Text > Basic Internationalization in a Java Application < BACK
158122020158178182047242169203138024176246252243131023021084157229187006165144132135152124
public GUI() { Label nameLabel = new Label("Name"); TextField greeting = new TextField("Hello"); ... } The user interface this type of code generates works fine in the U.S., but it might not work well in other countries where English is either not the primary language, such as Japan or France, or in a country where English has different idioms than in the U.S. It is important to be able to modify this English text to a more appropriate language for a given locale. Note To support internationalization, you need to use ResourceBundle. Although this class is not in java.text, doing locale-specific translation is what much of java.text is for, so it seems reasonable to discuss it here. Just remember that you need java.util for a ResourceBundle.
To be able to provide for internationalization, you want translations of the words in your GUI into other languages you want to support. The mechanism to do this is a properties file. A properties file, one for each language you wish to support, provides language-specific equivalents for key names that remain the same. For the word "Hello" (used in the previous sample code): The English version would say greeting="Good Day"; The French version would say greeting="Bon Jour"; The German version would say greeting="Guten Tag"; When your program runs, it loads the correct properties file based on the locale, and then Java automatically uses the correct language-specific version. Say, for example, that you have a screen with an OK button. You don't want to use OK in every locale; you would rather use an appropriate equivalent. So you define locale-specific equivalents in every properties file for the application. To access the locale-specific value, you call the getString() method:
getString("OK") The correct version of "OK" to get depends upon the locale. You create a Locale object that specifies the language and country, and then you pass this Locale object to methods of other objects that use the locale information. In this first example, the most important locale-sensitive object is a ResourceBundle, which contains the language-specific values for a given locale. Locale and ResourceBundle Classes: Code Example You obtain a ResourceBundle with a getBundle() call that accepts two parameters, the name of the properties file
minus the specific language and country, and a Locale object that specifies the language and country. These are concatenated together by the JVM to construct the actual properties filename to be used. Listing 18.1 provides a simple example of internationalizing an application with ResourceBundle.
Listing 18.1 Internationalizing an Application with ResourceBundle and Properties Files (TextDriver1.java)
// Simple Internationalized Program. import java.util.*; class Bundler { Locale theLocale; String theCountry; String theLanguage; ResourceBundle theBundle; public Bundler(String c, String l) { theCountry = c; theLanguage = l; theLocale = new Locale(theLanguage, theCountry); // Set the locale to the user's input values. // In an applet you would have to derive the // locale through the system properties. theBundle = ResourceBundle.getBundle( "MessagesBundle", theLocale); // Create a ResourceBundle for the chosen // locale. The ResourceBundle contains // the locale-specific values. You could create // several ResourceBundles when your // program starts, but generally, you only // create them one at a time. } public boolean displayLocaleText() { // Now that you have the right ResourceBundle // for the specified locale, print out the values // from the ResourceBundle for a partially // locale-specific message. System.out.println("You can search for a " + theBundle.getString("book")); System.out.println("by specifying either the "+ theBundle.getString("author")); System.out.println("or by " + theBundle.getString("title")); System.out.println("The catalog system will reply " + "with a " + theBundle.getString("message")); System.out.println("to tell you if the " + theBundle.getString("book") + " was found"); return true; } } // End class definition for Bundler. public class TextDriver1 { // To execute, type: // TextDriver1 EN us
// or // TextDriver1 DE de. public static void main(String[] args) { Bundler bundler = new Bundler(args[0], args[1]); bundler.displayLocaleText(); } } Listings 18.2 and 18.3 show the Properties files used for the ResourceBundle.
Listing 18.2 MessageBundle_de_DE.properties
book = das Buch title = der Titel author = der Schopfer message = das Botschaft
Listing 18.3 MessageBundle_en_US.properties
book = Book title = Title: author = Author: message = Message This program accepts console input, the language, and the country. These are used to create a Locale object. The language is a pair of lowercase letters that conform to the choices specified in ISO-639. You can find a complete list of the ISO-639 codes at http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt. The country code consists of two uppercase letters and conforms to ISO-3166. A copy of ISO-3166 can be found at http://www.chemie.fuberlin.de/diverse/doc/ISO_3166.html. You can also specify a third parameter that specifies an arbitrary value specific to your program. This could be used to request a font that is specific to UNIX or Windows, for example. The Locale class also provides constants for some locale parameters, such as JAPANESE or JAPAN. That Locale object is used to select the correct properties file to build a ResourceBundle. The program then uses the ResourceBundle to display locale-specific text using the getString() method. Caution If the specified Properties file is not found, a default Properties file is used instead.
Tip You should be sure to provide a default .properties file for your application so that the user doesn't have to specify a locale and so that the program works even if the user doesn't specify a locale. The default Properties file alleviates the need to have a Properties file for every possible locale.
This approach of using Locales and ResourceBundle objects allows your program to easily adapt to new locales. There is much more to ResourceBundle, such as ListResourceBundle, for which you should consult the Java documentation.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229187006165144130156057113
Formatting Text
Besides the wording of elements, such as error messages, GUI component labels, log messages, and the like, there are many items in data that must be formatted for a specific locale. These include currency, dates, time, measurements, titles (like Mr. or Mrs.), numbers, phone numbers, and postal addresses. Tip Avoid long, multielement messages that require translation of several items. Because numerous languages have a distinctive word order, it can be difficult to translate these messages.
You can also check to see which locales are supported to a locale-sensitive class. You can do this with the following code:
Locale localeList[] = DateFormat.getAvailableLocales(); for (int i = 0; i < localeList.length; i++) { System.out.println(localeList[i].toString()) } This code will display a list of language and country codes, which will not be very user-friendly. If you want to produce output that is user-friendly, replace the toString() method in this preceding code snippet with the Locale.getDisplayName method, in this way:
Locale localeList[] = DateFormat.getAvailableLocales(); for (int i = 0; i < localeList.length; i++) { System.out.println(localeList[i].getDisplayName()) } You can also find the default locale by calling the getDefault() method.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 18. Text > Formatting Numeric Data < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229187006165147231047032052
Use the predefined formats available through the NumberFormat class. Use a pattern for formatting data, such as a String pattern with DecimalFormat. This allows you to format values in a locale-independent way, such as replacing commas with decimal points. You use a pattern String to accomplish this.
Numeric Formatting: Code Example There are two main ways to customize numeric values: NumberFormat and DecimalFormat. The former is for simple, straightforward formatting, and the latter is for custom formatting. Listing 18.4 shows how to use NumberFormat for predefined formats and DecimalFormat for customized formatting with patterns.
Listing 18.4 Formatting Values with NumberFormat, DecimalFormat, and DecimalFormatSymbols (TextDriver2.java)
// Program to format data with help from java.text. import java.text.*; import java.util.*; class Formatter { Locale usLocale; Locale deLocale; NumberFormat usNumberFormatter; NumberFormat deNumberFormatter; String formattedValue; public Formatter() { usLocale = new Locale("EN", "us"); deLocale = new Locale("DE", "de"); // Create two Locale objects to use in formatting // for specific locales. } public boolean formatForLocale() {
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=134 (1 of 5) [6/2/2002 10:46:31 PM]
// Use predefined formatting from the // java.text.NumberFormat class, which // requries a Locale object. System.out.println("Number formatting:"); Float amount = new Float(1253200.55); usNumberFormatter = NumberFormat.getNumberInstance(usLocale); deNumberFormatter = NumberFormat.getNumberInstance(deLocale); // Create two Locale objects to use in formatting // for specific locales. formattedValue = usNumberFormatter.format(amount); System.out.println(formattedValue + " " + usLocale.toString()); // Print the value according to US format. formattedValue = deNumberFormatter.format(amount); System.out.println(formattedValue + " " + deLocale.toString()); // Print the value according to the German format. System.out.println("Currency Formatting:"); Float price = new Float(265500.34); usNumberFormatter = NumberFormat.getCurrencyInstance(usLocale); deNumberFormatter = NumberFormat.getCurrencyInstance(deLocale); // Call getCurrencyInstance() instead of // getNumberInstance(). formattedValue = usNumberFormatter.format(price); System.out.println(formattedValue + " " + usLocale.toString()); // Print the value according to US format. formattedValue = deNumberFormatter.format(price); System.out.println(formattedValue + " " + deLocale.toString()); // Print the value according to the German format. // Note: NumberFormat knows how to format // numbers, but knows nothing about currency // exchange rates. You will have to add additional // logic somewhere for that in a real application. System.out.println("Percentage Formatting:"); // Convert a decimal to a percentage. Float fraction = new Float(0.83); usNumberFormatter = NumberFormat.getPercentInstance(usLocale); deNumberFormatter = NumberFormat.getPercentInstance(deLocale); // Call getPercentInstance() instead of // getNumberInstance(). formattedValue = usNumberFormatter.format(fraction); System.out.println(formattedValue + " " + usLocale.toString()); // Print the value according to US format.
formattedValue = deNumberFormatter. format(fraction); System.out.println(formattedValue + " " + deLocale.toString()); // Print the value according to German format. // Use custom formatting. // First, do locale-independent formatting. String dollarString = "$###,###.##"; double amt = 5234425.773; DecimalFormat dollarFormatter = new DecimalFormat(dollarString); String output = dollarFormatter.format(amt); System.out.println(amt + " is formated to " + output); // Note in the output that only two numbers follow the // decimal point in the formatted version. DecimalFormatSymbols dfSymbols = new DecimalFormatSymbols(usLocale); dfSymbols.setDecimalSeparator('*'); dfSymbols.setGroupingSeparator('-'); String formatString = "###,###.0##"; DecimalFormat dfFormatter = new DecimalFormat(formatString, dfSymbols); dfFormatter.setGroupingSize(3); double dbl = 7334455.6678; System.out.println("Before custom formatting" + " the number is: " + dbl); System.out.println("With custom formatting" + " the number is: " + dfFormatter.format(dbl)); return true; } } public class TextDriver2 { public static void main (String[] args) { Formatter form = new Formatter(); form.formatForLocale(); } } This program shows how to use the NumberFormat and DecimalFormat classes to format numbers and currency. Calling a method such as getCurrencyInstance() returns an object you can use to produce a locale-sensitive currency amount. Using methods from DecimalFormatSymbols, on the other hand, allows you to specify the separator character and the decimal point character to use when you format a number. Caution When you use a pattern, such as "##,###.##", you might cause truncation of the value to the right of the decimal point. That might be acceptable. If it is not, you either specify more fields or round the
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=134 (3 of 5) [6/2/2002 10:46:31 PM]
For formatting numbers and currency, see the DecimalFormat class in the Java API doc. The options for constructing a pattern are listed in Table 18.1. (This table comes from the Java API documentation from Sun for this class.)
Table 18.1. Pattern and Format Options for DecimalFormat Meaning A digit. A digit; zero shows as absent. Placeholder for decimal separator. Placeholder for grouping separator. Separates mantissa and exponent for exponential formats. Separates formats. Default negative prefix. Multiply by 100 and show as percentage. Multiply by 1000 and show as per mille. Currency sign. Replaced by currency symbol. If doubled, replaced by international currency symbol. If present in a pattern, the monetary decimal separator is used instead of the decimal separator. Any other characters can be used in the prefix or suffix. Used to quote special characters in a prefix or suffix.
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
formats DecimalFormat class options 2nd formatting numeric data code 2nd 3rd 4th internationalizing 2nd 3rd 4th internationalizingclasses;DecimalFormat;pattern options numeric values with DecimalFormat class, code 2nd 3rd with DecimalFormatSymbols class, code 2nd 3rd with NumberFormat class, code 2nd 3rd getCurrencyInstance() method i18n numeric data code to format 2nd 3rd 4th formatting 2nd 3rd 4th 5th methods getCurrencyInstance() NumberFormat class numeric values code to format 2nd 3rd numeric data i18n (internationalization) code to format 2nd 3rd 4th formatting 2nd 3rd 4th 5th numeric values formatting with DecimalFormat class, code 2nd 3rd with DecimalFormatSymbols class, code 2nd 3rd with NumberFormat class, code 2nd 3rd patterns DecimalFormat class options 2nd
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 18. Text > Formatting Dates and Times < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229187006165147237137128138
// Program to format data with help from java.text import java.text.*; import java.util.*; class DateShaper { Locale usLocale; Locale deLocale; DateFormat usDateFormatter; DateFormat deDateFormatter; String formattedDate; Date theDate; public DateShaper() { usLocale = new Locale("EN", "us"); deLocale = new Locale("DE", "de"); // Create two Locale objects to use in formatting // for specific locales. theDate = new Date(); // The java.text classes use Date // objects for showing dates, even though // most methods of the Date class are // deprecated. } public boolean formatDates() { // Use getDateInstance to use predefined // date formats. usDateFormatter = DateFormat.getDateInstance( DateFormat.DEFAULT, usLocale);
formattedDate = usDateFormatter.format(theDate); System.out.println(formattedDate + " " + usLocale.toString()); deDateFormatter = DateFormat.getDateInstance( DateFormat.DEFAULT, deLocale); formattedDate = deDateFormatter.format(theDate); System.out.println(formattedDate + " " + deLocale.toString()); // DateFormat.DEFAULT is one of five // choices. Here are the others: usDateFormatter = DateFormat. getDateInstance( DateFormat.SHORT, usLocale); formattedDate = usDateFormatter.format(theDate); System.out.println("Short form: " + formattedDate + " " + usLocale.toString()); deDateFormatter = DateFormat.getDateInstance( DateFormat.SHORT, deLocale); formattedDate = deDateFormatter.format(theDate); System.out.println("Short form: " + formattedDate + " " + deLocale.toString()); // Medium date: usDateFormatter = DateFormat.getDateInstance( DateFormat.MEDIUM, usLocale); formattedDate = usDateFormatter.format(theDate); System.out.println("Medium form: " + formattedDate + " " + usLocale.toString()); deDateFormatter = DateFormat.getDateInstance( DateFormat.MEDIUM, deLocale); formattedDate = deDateFormatter.format(theDate); System.out.println("Medium form: " + formattedDate + " " + deLocale.toString()); // Long form: usDateFormatter = DateFormat.getDateInstance( DateFormat.LONG, usLocale); formattedDate = usDateFormatter.format(theDate); System.out.println("Long form: " + formattedDate + " " + usLocale.toString()); deDateFormatter = DateFormat.getDateInstance( DateFormat.LONG, deLocale); formattedDate = deDateFormatter.format(theDate); System.out.println("Long form: " + formattedDate + " " + deLocale.toString()); // Full form: usDateFormatter = DateFormat.getDateInstance( DateFormat.FULL, usLocale); formattedDate = usDateFormatter.format(theDate); System.out.println("Full form: " + formattedDate + " " + usLocale.toString()); deDateFormatter = DateFormat.getDateInstance(
DateFormat.FULL, deLocale); formattedDate = deDateFormatter.format(theDate); System.out.println("Full form: " + formattedDate + " " + deLocale.toString()); // You can also get just the Time, // getTimeInstance(), in these // locale-specific formats or both the date // and time. Here is an example of the // API for both date and time. usDateFormatter = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.FULL, usLocale); formattedDate = usDateFormatter.format(theDate); System.out.println("Full form: " + formattedDate + " " + usLocale.toString()); deDateFormatter = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.FULL, deLocale); formattedDate = deDateFormatter.format(theDate); System.out.println("Full form: " + formattedDate + " " + deLocale.toString()); // Perform custom formatting of dates and time. String datePattern = "yyyy.MM.dd G 'at'hh:mm:ss z"; SimpleDateFormat sdf = new SimpleDateFormat(datePattern, usLocale); String formattedDate = sdf.format(theDate); System.out.println(datePattern + " " + formattedDate); datePattern = "EEE, MMM d, ''yy"; sdf = new SimpleDateFormat(datePattern, usLocale); formattedDate = sdf.format(theDate); System.out.println(datePattern + " " + formattedDate); // Show the date and time with two custom // formats. // You can also use DateFormatSymbols // to modify how dates and times are formatted. DateFormatSymbols dfSymbols; String[] defaultWeek; String[] modifiedWeek; dfSymbols = new DateFormatSymbols(usLocale); defaultWeek = dfSymbols.getShortWeekdays(); System.out.println("Default short weekday names:"); for (int i = 0; i < defaultWeek.length; i++) { System.out.println(defaultWeek[i]); } System.out.println("Modified weekday names:"); String[] shortDayNames = { "", "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};
dfSymbols.setShortWeekdays(shortDayNames); modifiedWeek = dfSymbols.getShortWeekdays(); for (int i = 0; i < modifiedWeek.length; i++) { System.out.println(modifiedWeek[i]); } sdf = new SimpleDateFormat("E", dfSymbols); String theDay = sdf.format(theDate); System.out.println(theDay); return true; } } public class TextDriver3 { public static void main (String[] args) { DateShaper ds9 = new DateShaper(); ds9.formatDates(); } } As you can see from this program, you can use predefined data and time formats using the DateFormat class and calling
q q q
For customizing date and time values, you can use SimpleDateFormat instead. There are lots of different ways to combine formatting options, such as
Sat, Aug 21, '99 The SimpleDateFormat class provides the information in Table 18.2 on formatting. (This table is taken from the Java API documentation for SimpleDateFormat from Sun Microsystems, Inc.)
Table 18.2. Information Provided by SimpleDateFormat. Meaning Presentation Era designator (Text) AD Year (Number) 1996 Month in year (Text & Number) July & 07 Day in month (Number) 10 Hour in am/pm (1~12) (Number) 12 Hour in day (0~23) (Number) 0 Minute in hour (Number) 30 Second in minute (Number) 55
Symbol G y M d h H m s
Example
S E D F w W a k K z ' ''
Millisecond Day in week Day in year Day of week in month Week in year Week in month am/pm marker Hour in day (1~24) Hour in am/pm (0~11) Time zone Escape for text Single quote
(Number) (Text) (Number) (Number) (Number) (Number) (Text) (Number) (Number) (Text) (Delimiter) (Literal)
978 Tuesday 189 2 (2nd Wednesday in July) 27 2 PM 24 0 Pacific Standard Time '
Note If you do not specify a locale when you instantiate a DateFormat object, the default locale will be used.
The DecimalFormatSymbols class provides for changing separator and decimal point characters, among other things. Similarly, the DateFormatSymbols class provides for creating custom date formats. For example, the setWeekdays() method allows you to define your own names for the days of the week. You have freedom to set the various properties any way you want. Tip Although the example programs show simple, single items, you can also use these formatting techniques with multivalue, complex messages. To do this, you create a "template" with parameters that accept the formatted value. Support for complex message formatting is provided with the MessageFormat class. Multivalue message formatting is needed when using languages besides English because of variation in word order. English puts the verb after the subject, but German puts it at the end of the sentence. Hebrew, on the other hand, can put the verb first in the sentence. Since word order may vary widely, you cannot simply put the values in the same place in a message across languages.
Although they are not covered here, there are also classes for supporting testing characters, parsing text, and comparing Strings. See the java.text package for exam ples, such as the Character or Collator classes in the Java API doc.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
formatting 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th formatting with DateFormat class 2nd 3rd 4th 5th 6th formatting with SimpleDateFormat class 2nd 3rd 4th 5th 6th times formatting 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th formatting with DateFormat class 2nd 3rd 4th 5th 6th formatting with SimpleDateFormat class 2nd 3rd 4th 5th 6th DateFormat class dates code to format 2nd 3rd 4th 5th 6th times code to format 2nd 3rd 4th 5th 6th DateFormatSymbols class dates formatting with DateFormat class, code 2nd 3rd 4th 5th 6th with SimpleDateFormat class, code 2nd 3rd 4th 5th 6th i18n (internationalization) code to format 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th formatting 2nd 3rd 4th 5th 6th 7th DecimalFormatSymbols class formatting dates code 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th internationalizing 2nd 3rd 4th 5th 6th 7th with DateFormat class, code 2nd 3rd 4th 5th 6th with SimpleDateFormat class, code 2nd 3rd 4th 5th 6th SimpleDateFormat class symbols 2nd times code 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th internationalizing 2nd 3rd 4th 5th 6th 7th with DateFormat class, code 2nd 3rd 4th 5th 6th with SimpleDateFormat class, code 2nd 3rd 4th 5th 6th i18n dates code to format 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th formatting 2nd 3rd 4th 5th 6th 7th times code to format 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th formatting 2nd 3rd 4th 5th 6th 7th methods setWeekdays() setWeekdays() method SimpleDateFormat class dates code to format 2nd 3rd 4th 5th 6th formatting symbols 2nd times code to format 2nd 3rd 4th 5th 6th times formatting with DateFormat class, code 2nd 3rd 4th 5th 6th with SimpleDateFormat class, code 2nd 3rd 4th 5th 6th i18n (Internationalization) code to format 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th formatting 2nd 3rd 4th 5th 6th 7th
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229187006165146104126087163
All Books
Search
Java > PURE Java 2 > 19. Utility Classes > The Collection Framework < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229187006165148061030027171
All Books
Search
Java > PURE Java 2 > 19. Utility Classes > Collection Interfaces < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229187006165148059160144106
Collection Interfaces
The interfaces provided in the Collection Framework allow you to access and manipulate elements in a collection in a manner that is independent of the implementation of the collection. There are two groups of interfaces in the Collection Framework, one that begins with the Collection interface and a second that begins with the Map inter face. These are shown in Figures 19.1 and 19.2, respectively.
Figure 19.1 Inheritance hierarchy for the Collection interface .
The reason there are two inheritance hierarchies is that while SortedSet is a special kind of Set, and Set is a special kind of Collection, the Map interface represents a different way of viewing data, even though a Map does represent a collection of items. Based on the interface definitions, a Map does not have an IsA relationship with a Collection. Note In order to provide for interfaces that cover many different implementations, several methods declared in these interfaces are optional, in the sense that a given implementation does not need to support this operation if it is inappropriate. If a method that is not supported is called on an object of a class that does not support that operation, an UnsupportedOperationException is thrown. The Collection Framework was designed to provide a complete set of powerful and flexible ways to implement collections. In order to allow for simple implementations, however, there are several optional methods. You can skip those in our implementation if you wish.
CollectionA Collection is a group of objects that are elements of the Collection. No subclasses directly implement the Collection interface, but most classes that implement sub-interfaces accept a Collection object in their constructors. A Collection may represent a group of objects that contains duplicates or a group of elements that are unique in the Collection. Set The Set interface provides methods for a Collection, which allows no duplicate elements. In the library example I've been using, a Set might be used to represent library patrons or card catalog entries. List A List provides methods for a Collection that contains an ordered sequence of elements. A List may contain duplicate elements. Vectors implement the List interface. Map A Map represents an object that contains key/value pairs, in which the pairs are unique. A hashtable illustrates this.
Note
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=139 (2 of 5) [6/2/2002 10:47:11 PM]
You can view a Map implementation as a Collection by using one of the Collection views, keySet, values, or entrySet.
Two other interfaces, Comparable and Comparator , can be used to compare the elements of one or more collections. Comparable provides methods for automatic sorting of elements in a natural order. The Comparator interface provides methods for you to have complete control over the ordering of elements in a Collection, in particular implementations of SortedSet and SortedMap. Both of these maintain elements in ascending order. These two interfaces provide the ability to sort generic objects as long as they implement the Comparable interface or as long as a suitable Comparator object is provided. Several core Java classes, such as String and the primitive wrapper classes like Integer, implement the Comparable interface, allowing them to be easily sorted. You do not have to write your own sorting mechanism from scratch. Here are the basic methods implemented by all Collection implementation classes :
int size(); boolean isEmpty(); boolean contains(Object element); boolean add(Object element); // Optional boolean remove(Object element); // Optional Iterator iterator();
There are optional methods as well (optional in the sense that an implementation class does not have to give them a meaningful implementation):
There are also two methods to create arrays from the Collection implementation:
These methods are fairly self-explanatory, especially if you've ever used a Vector. These are generalized methods. This means that the add() method does not require that an element cannot be a duplicated. That is an implementation detail of a specific class. As mentioned, the Iterator class , an instance of which is returned by the iterator() method, is similar to an Enumeration but improves upon the Enumeration class by providing a way to remove elements of the collection through which the program is iterating. An Iterator can be used on any type of Collection. That is, its methods, like
hasNext() or remove() , work on any kind of Collection, whether an unsorted Set with no duplicates or a SortedList with duplicates. Methods such as addAll() and removeAll() are "bulk methods"; that is, they work on an entire Collection at one time, allowing you to do mass adds, mass deletes, mass comparison tests, and so forth. Tip You can turn a Collection, coll, into a Set containing no duplicates by doing this: Collection uniqueElements = new HashSet(coll);
For a complete list of the methods in each Collection Framework interface, see Chapter 33, "java.util." There is more that could be said about the Collection interfaces, but it is time to move on to techniques for using their implementation classes.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
implementation, viewing inheritance hierarchy Set Iterator class iterator() method java.util Collection Framework interfaces 2nd 3rd 4th 5th List interface Map interfaces inheritance hierarchy Map interface implementation viewing methods addAll() Collection Framework 2nd hasNext() iterator() remove() removeAll() packages java.util Collecton Framework, interfaces 2nd 3rd 4th 5th remove() method removeAll() method Set interface utilities Collection Framework interfaces 2nd 3rd 4th 5th java.util Collection Framework, interfaces 2nd 3rd 4th 5th viewing Map interface implementation
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 19. Utility Classes > Collection Implementations < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229186234124149121118095117
Collection Implementations
This section covers techniques for using some of the implementation classes. It is not exhaustive, but it is illustrative. Note It is possible to create your own implementations of the interfaces using the abstract implementations, such as AbstractList. In fact, the abstract classes provide implementation for some of the methods. You just need to provide the implementations for some of the basic methods.
The class names for implementation classes are made of two parts: the implementation name and the interface name, such as HashMap, a Map implementation based on hashing. Table 19.1 shows the main implementation classes by interface.
Table 19.1. Implementation Classes for Each Main Collection Interface Implementing Classes HashSet TreeSet (implements SortedSet) ArrayList LinkedList HashMap TreeMap (implements SortedMap)
Here are some important features of all these Collection Framework implementation classes :
q q
Consistent namesAll are built on the implemented interface. Consistent behaviorAll these classes implement the optional methods in the interfaces. Every one of these classes permits null elements, keys, and values. All these classes use unsynchronized methods. All have fail-fast Iterators, which fail quickly if they detect concurrent modification. All are serializable. All these classes implement the clone() method.
q q q q
The combination of methods that do not use synchronization and fast-fail Iterators is critically important. A Vector's methods are potentially slower than a HashMap's methods because a Vector uses synchronized methods. This prevents two Threads from modifying a Vector element in an unpredictable manner. A HashMap's methods do not use synchronization. This means that a HashMap is vulnerable to undesirable modification. To deal with this, the Iterator for a Collection object will fail quickly in a safe manner if it detects some other object is updating the HashMap concurrently. If you have coded your application to use a HashMap, while avoiding incorrect concurrent updates to a HashMap, your program will run faster than an equivalent program that uses a Vector instead of a HashMap. Caution The fast-fail mechanism comes into play only when the underlying collection is changed such that the Iterator is no longer valid. This means that you could do concurrent updates that do not corrupt the state of the data as far as the
Vector continues to use synchronized methods in Java 2. The rationale for using unsynchronized methods is that synchronization is often of no benefit because only one Thread is using the Collection object, and therefore there is no danger of concurrent updates putting the data into an incorrect or inconsistent state. Thus, synchronization helps the minority of applications while penalizing the majority needlessly. Tip If you need to use a synchronized version of a Collection object's methods, you can wrap the object with a synchronization wrapper. The synchronization wrappers allow any Collection implementation to use synchronization.
Sun suggests that when dealing with a Collection implementation you assign its instance to an interface type, so that you can deal with the object in an implementation-independent way, with the specific implementation choice based on performance requirements. In keeping with this suggestion, the following code examples are built around the interface, not the specific class. So there are examples for using a Set, a List, and a Map. Set Implementations In this section I will show how to use a Set implementation. Sets allow for a Collection that contains only unique values. Note Attempting to add a duplicate element does not produce an Exception of any kind. The operation is simply not successful, and there is nothing you need to do about the JVM's action.
There are two implementation classes for the Set interface in java.util: HashSet and TreeSet. A HashSet uses a hashtable to store its elements. A TreeSet stores its elements in a balanced binary tree called a red-black tree. According to an article at the Java Developer's Connection (http://developer.javasoft.com), a HashSet performs better than a TreeSet. All the Collection classes support the Collection method iterator() . An Iterator provides a means for stepping through a Collection of any kind. It will be implemented differently for a Set than for a Map or List, but a developer does not have to be concerned with how an Iterator works for a given Collection implementation class. To use an Iterator, you call its hasNext() method , which returns true if there are more elements to traverse and false if there are no more elements in the Collection to traverse. HashSet Code Example Sets are easy to create, and the HashSet is a simple yet handy implementation of the Set interface. Listing 19.1 shows how to create and use a HashSet.
Listing 19.1 Using a HashSet (SetDriver.java)
{ // Make a HashSet to hold the names of // Graeco-Roman Historiographers. bookSet = new HashSet(); bookSet.add("Seutonius"); bookSet.add("Thucydides"); bookSet.add("Tacitus"); bookSet.add("Josephus"); bookSet.add("Xenophon"); bookSet.add("Luke"); // Here a HashSet is created and its object // reference is assigned to an object of type Set, // i.e., an object that implements the Set interface. // // // // // // } public boolean traverseSet() { Iterator iterator = bookSet.iterator(); // Create an Iterator object, again using // a generic Collection method, not a specific // HashSet method. while (iterator.hasNext()) // hasNext() returns true if there is another // element in the Collection to which to move. { System.out.println Next use the Collection method add() to add elements to this HashSet. This approach lets the developer call the same Collection methods regardless of how a given implementation class like HashSet, actually adds an element to the underlying set object.
("Graeco-Roman Historiographer: " + iterator.next()); // next() steps to the next element in the Collection. } System.out.println("The maximum value in the HashSet is " + Collections.max(bookSet)); // This uses a static method of the Collections class // Find the maximum value in the Set. bookSet.add("Luke"); // Attempt to add a duplicate element. This should have no effect. System.out.println("HashSet after attempting to add" + " a duplicate value "); // A new Iterator is needed to iterate again. Iterator anotherIterator = bookSet.iterator(); while (anotherIterator.hasNext()) {
System.out.println("Graeco-Roman Historiographer: anotherIterator.next()); } return true; } } // End class definition for MapMaker. public class SetDriver { public static void main(String args[]) { SetMaker sm = new SetMaker(); boolean result = sm.traverseSet(); System.exit(0); } }
" +
In keeping with the library theme, this program stores the names of important Graeco-Roman history writers in a HashSet. Then it prints them. Here is the output:
C:\Purejava\Source\Chap19>java SetDriver Graeco-Roman Historiographer: Xenophon Graeco-Roman Historiographer: Seutonius Graeco-Roman Historiographer: Tacitus Graeco-Roman Historiographer: Josephus Graeco-Roman Historiographer: Thucydides Graeco-Roman Historiographer: Luke The maximum value in the HashSet is Xenophon HashSet after attempting to add a duplicate value Graeco-Roman Historiographer: Xenophon Graeco-Roman Historiographer: Seutonius Graeco-Roman Historiographer: Tacitus Graeco-Roman Historiographer: Josephus Graeco-Roman Historiographer: Thucydides Graeco-Roman Historiographer: Luke
What you should notice about this list is that it is not sorted at all. You should also notice that the elements were printed out from the HashSet in the order they appear in it, which is different from the order in which you added them to the HashSet. This is a function of the value of each element's hash code. You can also find the minimum and maximum values of any Collection. It is illustrated here by looking for the element with the maximum value. In this case, that might not be meaningful, but it would be in many applications. To show that attempting to add a duplicate element has no effect on the HashSet, the program also attempts to add a duplicate entry. List Implementations This section shows how to use a List implementation. Lists permit duplicate values to be added. They also provide ordering. By default this is natural ordering, or ordering by ascending value. There are several methods that apply more readily to Lists than to Sets, as will be shown in the sample code. A List adds to the methods provided by a Collection the ability to retrieve a specific element based on an index, such as element 5 in the collection.
One of the differences between an array and a Collection is that an array is fixed in size once instantiated, while a Collection can grow dynamically. An ArrayList is essentially an array that can be enlarged. An ArrayList allows any kind of value, including null, and implements all the optional Collection methods. It uses an array internally but can grow. It is roughly equivalent to a Vector, except that an ArrayList's methods are unsynchronized. As the following code snippet shows, an ArrayList can be created much like a Set:
List subjectList = new ArrayList(); subjectList.add("History"); subjectList.add("Philosophy"); subjectList.add("Religion"); subjectList.add("Literature"); Iterator iterator = subjectList.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }
Not only can you easily make a List, but you can change between Set and List in your code with minimal effort, as your needs change. List Code Example Lists are easy to create. Because the various Collection subinterfaces all provide many of the same methods, this code is practically identical to the code for using a HashSetexcept that it uses an ArrayList instead. Lists can use several algorithms (methods for performing useful tasks, such as sorting a List) with the Collections.sort() method , which is most relevant to a List. Here are some other examples:
q q q
Collections.shuffle() Mixes up the order of elements. Collections.binarySearch() Performs a binary search for an element. Collections.reverse() Reverses the order of the elements in the List.
Caution Be careful not to confuse the Collection interface with the Collections class , which ends with an s. The latter provides implemented methods that can be defined and used independently of the Collection implementation.
Tip You can convert a simple array into a List using the method Arrays.asList(), like this:
There is also a LinkedList class, which implements a List as a linked structure. Although it provides all the expected operations for a stack, a queue, or a double-ended linked list (deque), a developer does not have to go
through the usual pain of maintaining previous and next references, as would be necessary when implementing a linked list in C. This is all done for you by the LinkedList class. The LinkedList class has specific methods for manipulating the first and last elements of the list. Note Since there is a separate Stack class, the need to use the LinkedList class is reduced, and in general it is preferable to use ArrayList.
While the preferred List implementation is ArrayList, the sample program in Listing 19.2 shows how to create and use a LinkedList, including the constructor that takes a Collectionin this case, an ArrayList.
Listing 19.2 Using a LinkedList (ListDriver.java)
import java.util.*; class ListMaker { ArrayList bookList; LinkedList authorList; public ListMaker() { // Make an ArrayList to hold the names of // Graeco-Roman Historiographers. bookList = new ArrayList(); bookList.add("Seutonius"); bookList.add("Thucydides"); bookList.add("Tacitus"); bookList.add("Josephus"); bookList.add("Xenophon"); bookList.add("Luke"); // Here an ArrayList is
created. Then // this ArrayList is used in the constructor of a LinkedList. // This must be a LinkedList in order to access // LinkedList-specific methods. authorList = new LinkedList(bookList); } public boolean traverseList() { Iterator iterator = authorList.iterator(); // Get an Iterator for the LinkedList // then step through the LinkedList. while (iterator.hasNext()) // hasNext() returns true if there is another
// element in the Collection to which to move. { System.out.println("Graeco-Roman Historiographer: " + iterator.next()); // next() steps to the next element in the Collection. } // Now use the sort algorithm to produce a sorted list. Collections.sort(authorList); Iterator anotherIterator = authorList.iterator(); // Create an Iterator object, again using // a generic Collection method, not a specific LinkedList method. System.out.println(" "); System.out.println("LinkedList presented in" + " sorted order "); while (anotherIterator.hasNext()) { System.out.println("Graeco-Roman Historiographer: " + anotherIterator.next()); } // Now add an element to the front of the LinkedList. authorList.addFirst("Lucian"); // Now add an element to a specific location. authorList.add(4, "Polybius"); // Now show that a duplicate can be added to a list. authorList.add("Luke"); // Now iterate a final time through the LinkedList to see // the results of the additions. Iterator thirdIterator = authorList.iterator(); System.out.println(" "); System.out.println("LinkedList after" + " adding more elements "); while (thirdIterator.hasNext()) { System.out.println("Graeco-Roman Historiographer: " + thirdIterator.next()); } return true; } } // End class definition
for MapMaker. public class ListDriver { public static void main(String args[]) { ListMaker lm = new ListMaker();
boolean result = lm.traverseList(); System.exit(0); } } This code departs from the statement made earlier that it is best to use the interfaces and their methods rather than coding something like
The reason this code is different is that since the List interface does not support all the methods available to a LinkedList, it was necessary to work specifically with an object of type LinkedList and an object reference of type LinkedList. This code also illustrates that, unlike with a Set, you can add a duplicate to a List. Notice also, as in the earlier example, that you need to get a new Iterator object if you want to traverse a Collection multiple times using next(). Here is the output from the program:
java ListDriver Graeco-Roman Historiographer: Graeco-Roman Historiographer: Graeco-Roman Historiographer: Graeco-Roman Historiographer: Graeco-Roman Historiographer: Graeco-Roman Historiographer: LinkedList presented in sorted Graeco-Roman Historiographer: Graeco-Roman Historiographer: Graeco-Roman Historiographer: Graeco-Roman Historiographer: Graeco-Roman Historiographer: Graeco-Roman Historiographer:
Seutonius Thucydides Tacitus Josephus Xenophon Luke order Josephus Luke Seutonius Tacitus Thucydides Xenophon
LinkedList after adding more elements Graeco-Roman Historiographer: Lucian Graeco-Roman Historiographer: Josephus Graeco-Roman Historiographer: Luke Graeco-Roman Historiographer: Seutonius Graeco-Roman Historiographer: Polybius Graeco-Roman Historiographer: Tacitus Graeco-Roman Historiographer: Thucydides Graeco-Roman Historiographer: Xenophon Graeco-Roman Historiographer: Luke
First, the contents of LinkedList are printed using an Iterator. Then the Collections.sort() method is used to sort the contents. This does not merely display the elements in sorted order but modifies the order of the elements as they exist in the linked structure. Then, to show additional LinkedList features, an element is added to the start of the List with addFirst(), an element is added to a specific location (element 4, with elements beginning at element 0), and then a duplicate element is added to the end of the list. Note An Iterator does not support adding elements while iterating through a List. If you attempt this, a ConcurrentModificationException is thrown, followed by a fast-fail. There is a subinterface, called
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=140 (8 of 14) [6/2/2002 10:47:31 PM]
ListIterator, that permits add, set, and remove while iterating through a List. It's instantiated like this:
Map Implementations As noted, a Map contains name/value pairs. Each entry in the Map contains two values: one for the key and the other for the value. A Map cannot contain any duplicates, and no ordering is mandated. There are two general purpose Map implementations: HashMap and TreeMap. You would use TreeMap if the order of items in the Map was important. Otherwise, you would simply use a HashMap. Map Code Example A Map's methods are not synchronized. If multiple Threads attempt to update the Map at once, an exception may be thrown, and the data will likely end up in an unpredictable state. If you want to allow concurrent updates but also want Thread-safety, you can wrap the Map (or any other type of Collection) in a synchronized wrapper. In this case, I am using Collections.synchronizedMap. This provides synchronization without a developer having to create a special subclass of Map just to get synchronization, or use the older, less desirable Hashtable. Listing 19.3 shows an example of using a TreeMap.
Listing 19.3 Using a TreeMap to Provide an Ordered Map Object (MapDriver.java)
import java.util.*; class MapMaker { Map bookMap; public MapMaker() { bookMap = Collections.synchronizedMap(new TreeMap()); // Make a TreeMap and wrap it with a synchronizedMap // wrapper to make the TreeMap methods synchronized // which they are not by default. bookMap.put("Lewis, C.S.", "The Lion, the Witch and the Wardrobe"); bookMap.put("Schaeffer, Francis", "How Shall We then Live?"); bookMap.put("Pascal, Blaise", "Pensees"); bookMap.put("Authors unknown", "Septuagint"); bookMap.put("Philo", "Life of Moses"); // Use put() to add name-value pairs into a TreeMap. } public boolean traverseMap() { Iterator iterator = bookMap.entrySet().iterator(); // Obtain an Iterator based on the Collection view "entrySet". while (iterator.hasNext()) { Map.Entry e = (Map.Entry)iterator.next(); System.out.println(e.getKey() + ", "
+ e.getValue()); // The entrySet view contains both keys and values. } // Search the TreeMap for a particular value. System.out.println(""); if(bookMap.containsKey("Lewis, C.S.")) System.out.println("Author C.S. Lewis found"); else System.out.println("Author
not found"); // Next, return just the keys. Iterator keyIterator = bookMap.keySet().iterator(); // Obtain an Iterator based on the Collection view "keySet". System.out.println(""); System.out.println("Print just the keys " + "for the TreeMap"); while (keyIterator.hasNext()) { System.out.println(keyIterator.next()); // The keySet view contains only keys. } return true; } } // End class definition for MapMaker. public class MapDriver { public static void main(String args[]) { MapMaker mm = new MapMaker(); mm.traverseMap(); System.exit(0); }
} Notice that the input was ordered in the TreeMap based on the natural order of the key. That is, the Map was created in ascending order based on the key, in this case author names. You can use Collection views to iterate through a Map's contents. In fact, using Collection views is the only way to iterate through a Map's contents. You can print the keys, the values, or both from a Map. Here's the basic code to iterate a Map's keys:
iterator.hasNext(); )
System.out.println(iterator.next()); )
for (Iterator iterator= theMap.entrySet().iterator(); iterator.hasNext();) { Map.Entry e = (Map.Entry) iterator.next(); System.out.println(e.getKey() + ": " + e.getValue()); }
C:\Purejava\Source\Chap19>java MapDriver Authors unknown, Septuagint Lewis, C.S., The Lion, the Witch and the Wardrobe Pascal, Blaise, Pensees Philo, Life of Moses Schaeffer, Francis, How Shall We then Live? Author C.S. Lewis found Print just the keys for the TreeMap Authors unknown Lewis, C.S. Pascal, Blaise Philo Schaeffer, Francis
You can also search a Map for a specific key with containsKey() or a specific value with containsValue(). You can also change the value of a given value for a specific key during iteration over the Map using setValue() or remove an element using remove(), provided the specific Map implementation supports these methods. There is one other handy thing you can use a Map for: a multimap. There is no class or interface for a multimap because it is not used very often. To make a multimap, you can create a Map that uses a Set or a List for a value. In this way, you might create a foreign language dictionary, in which an English word serves as the key and a Set containing another language's potential equivalents is listed. Or, you might use a List to provide a multielement description for an inventory item named in the key.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
methods HashSet implementation class hasNext() method implementation classes class names Collection Framework features 2nd Collection Framwork interfaces HashSet TreeSet implementations Collection Framework 2nd 3rd HashSet code 2nd 3rd 4th List 2nd List code 2nd 3rd 4th 5th 6th Map Map code 2nd 3rd 4th Set 2nd List 2nd Map Set 2nd HashSet code 2nd 3rd 4th List code 2nd 3rd 4th 5th 6th Map code 2nd 3rd 4th interfaces Collection Collections class, comparing Collection Framwork interfaces;implementation classes Iterator 2nd elements adding hasNext() method iterator() method Iterators Java Developer s Connection Web site java.util Collection Framework implementations 2nd 3rd List implementations 2nd Map implementations Set implementations 2nd Set implementations, HashSet code 2nd 3rd 4th Set implementations, List code 2nd 3rd 4th 5th 6th Set implementations, Map code 2nd 3rd 4th LinkedList code 2nd 3rd 4th lists ArrayList LinkedList code 2nd 3rd 4th methods ArrayList Collection Framework synchronization wrappers Collections.binarySearch() Collections.reverse() Collections.shuffle() Collections.sort() HashMap
hasNext() iterator() Vector packages java.util Collecton Framework, HashSet code 2nd 3rd 4th Collecton Framework, implementations 2nd 3rd Collecton Framework, List code 2nd 3rd 4th 5th 6th Collecton Framework, List implementations 2nd Collecton Framework, Map code 2nd 3rd 4th Collecton Framework, Map implementations Collecton Framework, Set implementations 2nd Set interface HashSet implementation class TreeSet implementation class synchronization wrappers Collection Framework methods TreeSet implementation class utilities Collection Framework HashSet code 2nd 3rd 4th implementations 2nd 3rd List code 2nd 3rd 4th 5th 6th List implementations 2nd Map code 2nd 3rd 4th Map implementations Set implementations 2nd java.util Collection Framework, HashSet code 2nd 3rd 4th Collection Framework, implementations 2nd 3rd Collection Framework, List code 2nd 3rd 4th 5th 6th Collection Framework, List implementations 2nd Collection Framework, Map code 2nd 3rd 4th Collection Framework, Map implementations Collection Framework, Set implementations 2nd Vector methods 2nd Web sites Java Developers Connection wrappers synchronizing Collection Framework methods
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 19. Utility Classes > GregorianCalendar < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229186234124144250157033083
GregorianCalendar
In JDK 1.0, the Date class, which is a measure of the number of milliseconds that have passed since the start of the current era, was used for calendar-type dates. You would create a Date object and set its day, month, year, and time as needed. The Date class, however, lacked several important properties, such as support for daylight savings time and time zones. In response to these gaps, Javasoft introduced the Calendar and GregorianCalendar classes in JDK 1.1. Beginning with JDK 1.1 and now in Java 2, the java.util.Date class is intended to be used for a "moment in time." That is, if you need to compare two time values or subtract an earlier time from a later time, to find out how long something took to happen, the Date class is one appropriate way to do this. The Calendar class and its subclasses support internationalization as well. These classes also provide for making a time stamp independent of a particular calendar. Tip A preferable method for getting moments in time in milliseconds, which you can then compare and manipulate, is through calling
System.currentTimeInMillis()
Since this does not require objects to be instantiated by you, it will likely perform better than creating and manipulating two Date objects. As noted elsewhere in this book, object allocation and deallocation are the most expensive activities you can perform in Java.
The "current era" is from UNIX and equals midnight on January 1, 1970. So a Date object represents the number of milliseconds that have passed since then or, in the case of a date value prior to the start of the era, the milliseconds from the start of the era back to the date. In JDK 1.0, the Date class supported this function as well as serving as the class to use for calendar-type dates. As of JDK 1.1, all the methods that relate to calendar-type dates have been deprecated. You should not use this class for calendar-type dates. The Calendar and GregorianCalendar classes were added to JDK 1.1 to use for calendar-type dates. The Calendar class is the base class, but the only concrete subclass in Java 2 is GregorianCalendar, which allows you to manipulate Gregorian and Julian dates. Note To create a class for a calendar system other than Gregorian or Julian, I recommend that you subclass Calendar if possible, in order to get some of the functions such as time zones. You should make your own separate class that does not subclass Calendar only if you need to support a calendar system that is totally foreign to the month, day, and year system of Calendar. This might be the case for, say, a Chinese or Jewish calendar. The Calendar class does not lend itself well to lunar-oriented calendars, so using the Calendar as a base class for a calendar that needs to track the 14th of Nisan would be rather awkward.
You should use the GregorianCalendar class to create and manipulate calendar-type dates. This class includes support for year, month, day, day of the week, week of the month, daylight saving time, and time zones. Note The GregorianCalendar class, developed by Taligent, has some quirks. In particular, there are methods that should probably be public that are protected. This makes them much more difficult to use, because you have to subclass Calendar to use them. There is also no handy way to format and print a GregorianCalendar object. You have to convert it to a Date object first and then use SimpleDateFormat's methods! Still, the Calendar and GregorianCalendar classes are a big improvement over Date for tracking calendar-type dates.
GregorianCalendar Code Example Besides telling you that you should use GregorianCalendar instead of Date, there is nothing else conceptually to know. Listing 19.4 shows how to create, interrogate, and modify a GregorianCalendar object. There are a number of ways to create a GregorianCalendar object, ranging from accepting a default time zone and the current time in the default locale to specifying the time zone and the precise year, month, day, and time you want.
Listing 19.4 Using the GregorianCalendar Class (CalendarDriver.java)
// Basic GregorianCalendar Program. import java.util.*; import java.text.*; class DateInfo { SimpleTimeZone pdt; // A GregorianCalendar needs a time zone. public boolean displayCalenderInfo() { // Get time zone ids. String[] tzIds = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000); // If no ids were returned, something is wrong, so quit. if (tzIds.length == 0) return false; else; // Create a Pacific Standard Time zone. pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, tzIds[0]); // Set up rules for daylight savings time pdt.setStartRule (Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); pdt.setEndRule (Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); // Create a GregorianCalendar with the Pacific Daylight time zone // and the current date and time. // NOTE: You can set a superclass equal to a subclass instance. Calendar calendar = new GregorianCalendar(pdt); // Create a GregorianCalendar object using // Pacific Daylight Time, accounting // for Daylight savings time. This does not // actually set the date in this object. // That is done, oddly enough, with a Date // object and the setTime() method as shown below.
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=141 (2 of 7) [6/2/2002 10:48:01 PM]
Date trialTime = new Date(); // Gets the current moment. calendar.setTime(trialTime); // Set the GregorianCalendar object's date and time // to the current moment. // Print a bunch of interesting things. System.out.println("Calendar data members and methods"); System.out.println("ERA: " + calendar.get(Calendar.ERA)); System.out.println("YEAR: " + calendar.get(Calendar.YEAR)); // Print the era and year. This era is BC or AD. // Note: Months begin with 0. System.out.println("MONTH: " + calendar.get(Calendar.MONTH)); System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR)); System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH)); // Print month and week information. // These values are all numeric. You would need to convert them // to make them user-friendly. The Calendar class has static // final values to match all the possibilities, so you could // test the value in a switch statement to come up // with the correct String to display or print. // Caldendar.Date = day of month System.out.println("DATE: " + calendar.get(Calendar.DATE)); System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH)); System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR)); System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK)); System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); // Prints information about the time of day next. // AM =0, PM =1 System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM)); System.out.println("HOUR: " + calendar.get(Calendar.HOUR)); System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY)); System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE)); System.out.println("SECOND: " + calendar.get(Calendar.SECOND)); System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND)); System.out.println("ZONE_OFFSET: " + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // Print the time. System.out.println("DST_OFFSET: " + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); // Now print a nicely formatted date. System.out.println(DateFormat.getDateInstance().
format
(calendar.getTime())); System.out.println("Current Time, with hour reset to 3"); // Now change the date and display the data again. calendar.clear(Calendar.HOUR_OF_DAY); calendar.clear(Calendar.MONTH); // Clear the values you want to change. calendar.set(Calendar.HOUR, 3); calendar.set(Calendar.MONTH, 11); System.out.println("ERA: " + calendar.get(Calendar.ERA)); System.out.println("YEAR: " + calendar.get(Calendar.YEAR)); System.out.println("MONTH: " + calendar.get(Calendar.MONTH)); System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR)); System.out.println("WEEK_OF_MONTH: " + calendar.get (Calendar.WEEK_OF_MONTH)); System.out.println("DATE: " + calendar.get(Calendar.DATE)); System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH)); System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR)); System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK)); System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM)); System.out.println("HOUR: " + calendar.get(Calendar.HOUR)); System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY)); System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE)); System.out.println("SECOND: " + calendar.get(Calendar.SECOND)); System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND)); System.out.println("ZONE_OFFSET: " + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // Time zone offset in hours. System.out.println("DST_OFFSET: " + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours // Now print a nicely formatted date. System.out.println(DateFormat.getDateInstance().format (calendar.getTime())); return true; } } public class CalendarDriver { public static void main
(String[] args) { DateInfo di = new DateInfo(); boolean result = di.displayCalenderInfo(); if (result == true) System.exit(0); else System.exit(-1); } } Here is the output from running the program on September 27, 1999:
Calendar data members and methods ERA: 1 YEAR: 1999 MONTH: 8 WEEK_OF_YEAR: 40 WEEK_OF_MONTH: 5 DATE: 27 DAY_OF_MONTH: 27 DAY_OF_YEAR: 270 DAY_OF_WEEK: 2 DAY_OF_WEEK_IN_MONTH: 4 AM_PM: 0 HOUR: 11 HOUR_OF_DAY: 11 MINUTE: 54 SECOND: 57 MILLISECOND: 680 ZONE_OFFSET: -8 DST_OFFSET: 1 Sep 27, 1999 Current Time, with hour reset to 3 and month reset to December ERA: 1 YEAR: 1999 MONTH: 11 WEEK_OF_YEAR: 1 WEEK_OF_MONTH: 5 DATE: 27 DAY_OF_MONTH: 27 DAY_OF_YEAR: 361 DAY_OF_WEEK: 2 DAY_OF_WEEK_IN_MONTH: 4 AM_PM: 0 HOUR: 3 HOUR_OF_DAY: 3 MINUTE: 54 SECOND: 57 MILLISECOND: 680 ZONE_OFFSET: -8 DST_OFFSET: 0 Dec 27, 1999
You can see here that, except for providing a formatted date (which you can modify), all the values are numeric. You would need to convert these to String representations after you get them. There is not much conceptually to master here. It is simply a matter of constructing a GregorianCalendar object, setting its values, and then getting them back out. Note that when you want to modify a value in a GregorianCalendar object , you should call the clear() method of the Calendar class with the specific data member you want to change, such as DAY_OF_WEEK or MONTH. Then you set the new value in the data member. Also, notice that you should use DateFormat or SimpleDateFormat class from the java.text package to print a nicely formatted date. For more on formatting dates, see Chapter 18, "Text." Without formatting, the data values from a GregorianCalendar object can be easily manipulated, but they are not very user friendly.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
GregorianCalendar class 2nd 3rd 4th 5th GregorianCalendar class, code 2nd 3rd 4th 5th
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 19. Utility Classes > Using Properties < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229186235212195119050108118
Using Properties
Anyone who has worked with Microsoft Windows for very long has encountered .ini files or the Registry. Both .ini files and the Registry contain configuration information. Similar information is often placed in a .login or .cshrc file in UNIX. Java also has a facility for storing application-specific information: a Properties object, which can be stored in a file if necessary. Many programs need configuration information, such as the appropriate directory to look in for a specific file, or the name of a host to use to find a database or the port to use for starting the JavaIDL orb. The Java way to deal with the need for such configuration values is to create a file using a Properties object, which consists of name/value pairs. When you run a Java program, be it an application, an applet, or a servlet, there are system properties available to interrogate. You can examine a specific property, such as the file separator for a system, like / or \, by calling the getProperty() method of the System class like this:
This returns the platform-dependent value of the file separator on a given system. You could use this to dynamically build a platform-specific file path from platform-independent Java code. You could get all the available system properties at once by coding
You can also change the value of a property by using the put() method. Caution Be cautious when modifying a system property as opposed to a user-defined property. The setting affects all subsequent work within the JVM.
You can also create your own properties, like rmi.host, to specify the hostname for an RMI remote object and the host that the corresponding RMIRegistry is running on. Or, you might want to keep a running total or base value, such as lastAccountNumber. You can store anything you want, as long as it can be stored as a String. Both the property name and the property value must be Strings. You have already seen several system properties being used or described, such as java.security.policy and jdbc.drivers. Using Properties greatly enhances the flexibility of your program. You can change a property without having to change your code. You can get Properties from the command line, from applet parameter tags, or from a flat file, among other choices. It is a lot less bug-prone an approach than actually going into your code and hard-coding a value that may end up making your code platform-dependent.
Properties Code Example To use a Properties object, you work with key-value pairs, either getting a value using a key or setting a value for a key. Listing 19.5 shows how to create a Properties object; get the system properties; add a new, user-defined property; and then save the new property by writing the Properties object to a file.
Listing 19.5 Using the Properties Class (PropDriver.java)
// Create and Modify a Properties object. import java.io.*; import java.util.*; class SystemInfo { private Properties userProperties; private FileInputStream fis; private File userPropFile; private String userFileName; private String userName; public SystemInfo(String userID) { userFileName = userID + "Props" + ".txt"; userName = userID; } public void getSystemInfo() { Properties sysProps = new Properties(System.getProperties()); // Get the system properties. sysProps.list(System.out); // List the system properties to standard out. String userDir = System.getProperty("user.dir"); System.out.println("User working directory is " + userDir); String separator = System.getProperty("file.separator"); setUserInfo(userDir, separator); } private void setUserInfo(String dir, String fs) { FileOutputStream fos = null; String wholeFileName = dir + fs + userFileName; userProperties = new Properties(); // Create a new file using the File(String) constructor. userPropFile = new File
(wholeFileName); if(userPropFile.exists() == false) { // Property file doesn't exist, so create it. try
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=142 (2 of 5) [6/2/2002 10:48:15 PM]
{ userPropFile = new File(wholeFileName); fos = new FileOutputStream(userPropFile); } catch(IOException e) { System.out.println("Error creating user Properties file"); } // It may be that the first time you need to use a // configuration file made with a Properties object that // the file doesn't exist, so be prepared for that. // This would most likely happen the first time the program // runs on a given system. // "Put" some new property values in. try { userProperties.put("USER", userName); userProperties.put("WORKING.DIR", dir); userProperties.store(fos, "User Properties"); fos.flush(); fos.close(); } catch(IOException e) { System.out.println ("Error creating user Properties file because " + e.toString()); } } // End if block else; try { fis = new FileInputStream(userPropFile); userProperties.load(fis); } catch(IOException e) { System.out.println("Error opening user Properties file" + e.toString()); } try { fis.close(); } catch(IOException e) { System.out.println
("Error closing user Properties file " + e.toString()); } // Now we've saved our own custom Properties file to use later // and read the file back in using load() to put the file's
// contents into a Properties object. Now display the file's // contents. System.out.println("Here is the retrieved contents " + "of the user properties file"); userProperties.list(System.out); // Now, modify the system properties. Properties sysProps = System.getProperties(); // Get all the system properties. System.out.println("Current System Properties"); sysProps.list(System.out); // The list() method returns all the properties in the // Properties object. sysProps.setProperty("great.java.book", "Pure Java 2"); // Use setProperty() to add or change a property. System.out.println("Modified system properties"); sysProps.list(System.out); } } // End class definition for SystemInfo. public class PropDriver { public static void main(String[] args) { SystemInfo info = new SystemInfo("Buzz"); info.getSystemInfo(); System.exit(0); } } This program does several things. First, it lists the current system properties using the list() method against the Properties object returned from System.getProperties(), which contains all the current system properties. Next, the program creates a new Properties object, adds two properties to it, including user.name and user.dir, and then stores it. Note The old way to save a Properties object to a file was to use the save() method . This method was deprecated in Java 2, because save() does not throw an IOException if there is an IO error. In Java 2, you should use the store() method , which does throw an IOException if an IO error occurs. So store() is safer to use, because you are notified if a problem occurs.
Then the program reinitializes the contents of the userProperties Properties object from the file using the load() method. Finally, the program gets the system properties and then adds to those properties an additional system property, great.java.book. It then lists the modified system properties. Notice that when you added the new property, it did not necessarily show up at the end of the list of properties. This is because a Properties object is not merely a list of properties, but is a subclass of Hashtable. So the value of the key for the new property was hashed, and that hash was used to determine where the new property would go. You can use getProperty() to get a specific property, setProperty to add or modify a property, and load() and store() to read a Properties object from and write a Properties object to a file. Observe that all properties are strings, and the key for a property is generally a period-delimited name. Properties allow your program to save or read its own configuration values, making your code much more flexible. You often simply need to know a given system property. All the system properties are properties in a Properties object created at runtime, which describes the runtime environment, including platform-dependent information.
Delivered for Maurice ling Swap Option Available: 7/15/2002 Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229186235212194108120035084
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229186235212194102009227120
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark 2002, O'Reilly & Associates, Inc.
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 20. java.applet > Package Name: java.applet < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229186235212193134233164187
Interfaces
Appet
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
class interfaces
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 20. java.applet > Interface and Class Details < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229186235212192177030049153
public abstract interface AppletContext The AppletContext interface provides methods to obtain information about an applet's enclosing Web page, other applets, and the applet's environment. As shown here, your applet can obtain an AppletContext and then get information:
AppletContext ap = getAppletContext(); Sring appletName = ap.getApplet(); // Get the name of the current applet. ap.showDocument("http://www.GradLibraray.edu/index.html"); // Replace the current web page with the one at this URL. ap.setStatus("Check out complete"); // Update the text in the browser's status window. Alternatively, you can use similar methods that are implemented in the java.applet.Applet class itself for some of these methods.
Methods
getApplet(String appletName) getApplets() getAudioClip(URL aUrl) getImage(URL imageUrl) showDocument(URL aUrl) showDocument(URL aUrl, String targetString) showStatus(String aStatus)
public abstract interface AppletStub The AppletStub interface declares the methods used by an AppletStub, which is attached to an
applet at runtime. This stub provides an interface between the applet and the program running it, such as a browser. It is not normally used by an applet developer.
Methods
isActive() getDocumentBase() getCodeBase() getParameter(String aName) getAppletContext() appletResize(int theWidth, int theHeight)
Interface AudioClip
public abstract interface AudioClip The public abstract interface AudioClip provides methods that can be implemented to allow the playing of sound clip objects by an applet. Here is an example of part of an applet that can play sound continuously while the applet's web page is displayed:
public class SoundApplet extends Applet { public void init() // Called when the applet is instantiated. { AudioClip ac = getAudioClip ("http://www.soundlib.com/JazzSampler.au"); } public void start() // Called when you go to the web page. { ac.loop(); } public void stop() // Called when you leave the web page. { ac.stop(); } } // End SoundApplet class definition. Tip To be a good web citizen, always be sure that you stop() an applet playing sound in the applet's stop() method. Otherwise, even when you go to a totally different web page at a different web site, the music will still be playing. Imagine how annoying it would be to hear the same song repeatedly. Even if you liked it when it first played, you'd hate it after 20 minutes, and you would probably have to shut down the browser to stop it.
The Applet class is the class that all applets must extend. It contains methods for obtaining applet resources, such as getAudioClip() for AudioClips and getImage() for Images. Your code never calls the constructor for an Applet. It is called by a Java program running inside the browser, outside of your control. The applet instance is instantiated by this program, which then calls the applet's init() method. Your applet GUI component is then added to an invisible Frame created by this application. You can tell the application is starting because the browser's status line says that it is starting Java. This application is required to run your applet, and the application is hosted by the browser. Thus, your applet is not a stand-alone program. The browser's JVM starts the whole process by calling the main method of the application.
Constructor
public
Methods
Applet()
public public public public public public public public public public public public public public public public public public public public public public
final void boolean URL URL String AppletContext void void void Image static final AudioClip AudioClip AudioClip String Locale String[][] void void void void void void
setStub(AppletStub aStub) isActive() getDocumentBase() getCodeBase() getParameter(String aName) getAppletContext() resize(int theWidth, int theHeight) resize(Dimension dim) showStatus(String message) getImage(URL aUrl, String aName) newAudioClip(URL url) getAudioClip(URL aUrl) getAudioClip(URL aUrl, String aName) getAppletInfo() getLocale() getParameterInfo() play(URL aUrl) play(URL url, String name) init() start() stop() destroy()
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
sount on Web pages objects sound clip AudioClip interface packages java.applet Applet class 2nd 3rd Applet class, constructor Applet class, methods 2nd AppletContext interface 2nd 3rd AppletContext interface, methods 2nd AppletStub interface 2nd AppletStub interface, methods 2nd AudioClip interface 2nd 3rd sound Web pages AudioClip interface 2nd 3rd stop() method sound clip objects AudioClip interface stop() method sound on Web pages Web pages sound AudioClip interface 2nd 3rd stop() method
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084157229186235212199232024125233
All Books
Search
Java > PURE Java 2 > 21. java.awt > Package Name: java.awt < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229186232000077067102010042
ActiveEvent Adjustable Composite CompositeContext ItemSelectable LayoutManager LayoutManager2 MenuContainer Paint PaintContext PrintGraphics Shape Stroke Transparency Classes
AlphaComposite AWTEvent AWTEventMulticaster AWTPermission BasicStroke BorderLayout Button Canvas CardLayout Checkbox CheckboxGroup CheckboxMenuItem Choice Color Component ComponentOrientation
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=151 (1 of 3) [6/2/2002 10:49:44 PM]
Container Cursor Dialog Dimension Event EventQueue FileDialog FlowLayout Font FontMetrics Frame GradientPaint Graphics Graphics2D GraphicsConfigTemplate GraphicsConfiguration GraphicsDevice GraphicsEnvironment GridBagConstraints GridBagLayout GridLayout Image Insets Label List MediaTracker Menu MenuBar MenuComponent MenuItem MenuShortcut Panel Point Polygon PopupMenu PrintJob Rectangle RenderingHints RenderingHints.Key Scrollbar ScrollPane SystemColor TextArea TextComponent TextField TexturePaint Toolkit Window Exceptions
AWTError
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 21. java.awt > Package Name: java.awt.datatransfer < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229186233234140099067048165
UnsupportedFlavorException
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 21. java.awt > Package Name: java.awt.dnd < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229186233234139243016168091
DnDConstants DragGestureEvent DragGestureRecognizer DragSource DragSourceContext DragSourceDragEvent DragSourceDropEvent DragSourceEvent DropTarget DropTarget.DropTargetAutoScroller DropTargetContext DropTargetDragEvent DropTargetDropEvent DropTargetEvent MouseDragGestureRecognizer Exceptions
InvalidDnDOperationException
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 21. java.awt > Package Name: java.awt.event < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229186233234138159111129036
ActionListener AdjustmentListener AWTEventListener ComponentListener ContainerListener FocusListener InputMethodListener ItemListener KeyListener MouseListener MouseMotionListener TextListener WindowListener Classes
ActionEvent AdjustmentEvent ComponentAdapter ComponentEvent ContainerAdapter ContainerEvent FocusAdapter FocusEvent InputEvent InputMethodEvent InvocationEvent ItemEvent KeyAdapter KeyEvent MouseAdapter MouseEvent
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=154 (1 of 2) [6/2/2002 10:51:00 PM]
All Books
Search
Java > PURE Java 2 > 21. java.awt > Package Name: java.awt.font < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229186238200115044206015215
FontRenderContext GlyphJustificationInfo GlyphMetrics GlyphVector GraphicAttribute ImageGraphicAttribute LineBreakMeasurer LineMetrics ShapeGraphicAttribute TextAttribute TextHitInfo TextLayout TextLayout.CaretPolicy TextLine.TextLineMetrics TransformAttribute
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 21. java.awt > Package Name: java.awt.geom < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229186238200117169243054196
PathIterator Classes
AffineTransform Arc2D Arc2D.Double Arc2D.Float Area CubicCurve2D CubicCurve2D.Double CubicCurve2D.Float Dimension2D Ellipse2D Ellipse2D.Double Ellipse2D.Float FlatteningPathIterator GeneralPath Line2D Line2D.Double Line2D.Float Point2D Point2D.Double Point2D.Float QuadCurve2D QuadCurve2D.Double QuadCurve2D.Float Rectangle2D Rectangle2D.Double Rectangle2D.Float RectangularShape RoundRectangle2D RoundRectangle2D.Double
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=156 (1 of 2) [6/2/2002 10:52:01 PM]
RoundRectangle2D.Float Exceptions
IllegalPathStateException NoninvertibleTransformException
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 21. java.awt > Package Name: java.awt.print < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084157229186239038060079163125255
All Books
Java > PURE Java 2 > 21. java.awt > java.awt Interface and Class Details < BACK
158122020158178182047242169203138024176246252243131023021084157229186239038058105010113008
public abstract interface ActiveEvent This interface provides the API for an event to dispatch itself.
Methods
public void
dispatch()
Interface Adjustable
public abstract interface Adjustable This interface specifies fields and methods for an item that contains an adjustable numeric value within a set range of values. It is used by the Scrollbar and JScrollBar classes.
Fields
HORIZONTAL VERTICAL
public public public public public public public public public public public public public public public
int void int void int void int void int void int void int void void
getOrientation() setMinimum(int minumum) getMinimum() setMaximum(int max) getMaximum() setUnitIncrement(int unit) getUnitIncrement() setBlockIncrement(int block) getBlockIncrement() setVisibleAmount(int visibleAmt) getVisibleAmount() setValue(int v) getValue() addAdjustmentListener(AdjustmentListener l) removeAdjustmentListener (AdjustmentListener l)
Interface Composite
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=158 (1 of 92) [6/2/2002 10:53:47 PM]
public abstract interface Composite This interface provides methods for drawing a graphics primitive in a graphics context and combining the primitive with colors for rendering.
Methods
public CompositeContext
Interface CompositeContext
public abstract interface CompositeContext This interface declares the methods used for a CompositeContext to describe the environment for operations on Composite objects.
Methods
Interface ItemSelectable
public abstract interface ItemSelectable This interface declares methods for objects that contain items of which zero or more can be selected.
Methods
Interface LayoutManager This interface declares the methods for implementing a LayoutManager object, such as an instance of FlowLayout.
Interface LayoutManager2
public abstract interface LayoutManager2 extends LayoutManager This interface is an extension to the LayoutManager interface to provide for constraint-based layouts, primarily from tool providers.
Methods
public void public public public public Dimension float float void
addLayoutComponent(Component component, Object constraintObject) maximumLayoutSize(Container targetContainer) getLayoutAlignmentX(Container targetContainer) getLayoutAlignmentY(Container targetContainer) invalidateLayout(Container targetContainer)
Interface MenuContainer
public abstract interface MenuContainer This interface declares methods for a MenuContainer class.
Methods
public abstract interface Paint extends Transparency This interface provides methods for generating color patterns by Graphics2D-related objects.
Methods
public PaintContext
createContext(ColorModel colorModel, Rectangle devBounds, Rectangle2D rectBounds, AffineTransform xForm, RenderingHints renderHints)
Interface PaintContext
public abstract interface PaintContext This interface declares methods for defining an environment in which color patterns are generated.
Methods
public void dispose() public ColorModel getColorModel() public Raster getRaster(int x, int y, int w, int h) Interface PrintGraphics
public abstract interface PrintGraphics This interface declares methods for a print graphics object for a page.
Method
getPrintJob()\
public abstract interface Shape This interface declares methods for defining geometric shapes.
Methods
getBounds() getBounds2D() contains(double x, double y) contains(Point2D pt2D) intersects(double x, double y, double width, double height) public boolean intersects(Rectangle2D rect2D) public boolean contains(double x, double y, double width, double height) public boolean contains(Rectangle2D rect2D) public PathIterator getPathIterator(AffineTransform affTran) public PathIterator getPathIterator(AffineTransform affTran, double flat) Interface Transparency
public abstract interface Transparency This interface provides fields and methods for common transparency modes.
Fields
public static final int OPAQUE public static final int TRANSLUCENT
Methods
AWTEvent is the base class for all AWT event classes, such as ActionEvent.
Fields
protected int protected boolean public static final public static final public static final public static final public static final public static final public static final public static final public static final public static final public static final
id long long long long long long long long long long long consumed COMPONENT_EVENT_MASK CONTAINER_EVENT_MASK FOCUS_EVENT_MASK KEY_EVENT_MASK MOUSE_EVENT_MASK MOUSE_MOTION_EVENT_MASK WINDOW_EVENT_MASK ACTION_EVENT_MASK ADJUSTMENT_EVENT_MASK ITEM_EVENT_MASK TEXT_EVENT_MASK
INPUT_METHOD_EVENT_MASK RESERVED_ID_MAX
public public
Methods
public int public String public String protected void protected boolean protected void
Class AWTEventMulticaster
public class AWTEventMulticaster extends Object implements ComponentListener, ContainerListener, FocusListener, KeyListener, MouseListener, MouseMotionListener, WindowListener, ActionListener,
graphics/ccc.gif ItemListener, AdjustmentListener, TextListener,
InputMethodListener
This class handles multicast event dispatching, serving as the glue coupling a Component, events produced by it, and listeners that process those events. Rather than letting the JVM do its event propagation, it is implemented by an application to do its own event handling. Here is code showing the basics of using this class:
public ToggleSwitch extends Component { ActionListener listener = null; // In addition to methods to define the ToggleSwitch's // appearance and other properties, you use
// AWTEventMulticaster to handle events on the // component. public void addActionListener(ActionListener l) { listener = AWTEventMulticaster.add(listener, l); } public void removeActionListener(ActionListener l) { listener = AWTEventMulticaster.remove(listener, l); } public void processEvent(AWTEvent e) { // An event which generates an "action" event. if(listener != null) { listener.actionPerformed(new ActionEvent()); } } }
Fields
a b
protected AWTEventMulticaster
Methods
(EventListener a, EventListener b)
Protected eventListener public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void
remove(EventListener listener) componentResized(ComponentEvent compEvt) componentMoved(ComponentEvent compEvt) componentShown(ComponentEvent compEvt) componentHidden(ComponentEvent compEvt) componentAdded(ContainerEvent compEvt) componentRemoved(ContainerEvent contEvt) focusGained(FocusEvent focusEvt) focusLost(FocusEvent focusEvt) keyTyped(KeyEvent keyEvt) keyPressed(KeyEvent keyEvt) keyReleased(KeyEvent keyEvt) mouseClicked(MouseEvent mouseEvt) mousePressed(MouseEvent mouseEvt) mouseEntered(MouseEvent mouseEvt) mouseExited(MouseEvent mouseEvt) mouseDragged(MouseEvent mouseEvt) mouseMoved(MouseEvent mouseEvt) windowOpened(WindowEvent windowEvt)
public public public public public public public public public public public public public
void void void void void void void void void void void void static
public static
public static FocusListener listenerB) public static public static MouseListener listenerB) public static public static public static public static public static
windowClosing(WindowEvent windowEvt) windowClosed(WindowEvent windowEvt) windowIconified(WindowEvent windowEvt) windowDeiconified(WindowEvent windowEvt) windowActivated(WindowEvent windowEvt) windowDeactivated(WindowEvent windowEvt) actionPerformed(ActionEvent actionEvt) itemStateChanged(ItemEvent itemEvt) adjustmentValueChanged(AdjustmentEvent adjEvt) textValueChanged(TextEvent textEvt) inputMethodTextChanged(InputMethodEvent imEvt) caretPositionChanged(InputMethodEvent imEvt) add(ComponentListenerComponentListenerlistenerA, ComponentListener listenerB) add(ContainerListener ContainerListenerlistenerA, ContainerListener listenerB) add(FocusListener listenerA, add(KeyListenerA, KeyListener, KeyListener listenerB) add(MouseListener listenerA, add(MouseMotionListener listenerA, MouseMotionListener listener) add(WindowListener listenerA, WindowListener listenerB) add(ActionListener listenerA, ActionListener listenerB) add(ItemListener listenerA, ItemListener listenerB) add(AdjustmentListener AdjustmentListener listenerA, AdjustmentListener listenerB) add(TextListener listenerA, TextListener listenerB) add(InputMethodListener listenerA, InputMethodListener listenerB) remove(ComponentListener listenerA, ComponentListener listenerB) remove(ContainerListener listenerA, ContainerListener listenerB) remove(FocusListener listenerA, FocusListener listenerB) remove(KeyListener listenerA, KeyListener listenerB) remove(MouseListener listenerA, MouseListener listenerB) remove(MouseMotionListener listenerA, MouseMotionListener listenerB) remove(WindowListener listenerA, WindowListener listenerB) remove(ActionListener listenerA, ActionListener listenerB) remove(ItemListener listenerA, ItemListener listenerB) remove(AdjustmentListener listenerA, AdjustmentListener listenerB) remove(TextListener listenerA, TextListener listenerB)
public static public static public static public static public static public static public static public static public static public static public static public static public static
protected static
remove(InputMethodListener listenerA, InputMethodListener listenerB) addInternal(EventListener listenerA, EventListener listenerB) removeInternal(EventListener listenerA, EventListener listenerB) saveInternal(ObjectOutputStream stream, String internalString) throws IOException save(ObjectOutputStream stream, String aString, EventListener listener) throws IOException
Class BorderLayout
This class defines a layout manager for arranging and sizing components in a container. It divides the container into five regions: North, South, East, West, and Center. By default, the Center is the largest area. This is the default layout manager for Frames and Dialogs. Here's a code snippet that shows the use of this layout manager:
Frame fr = new Frame("Welcome to the Grad Library"); // A Frame has a BorderLayout by default. Button searchButton = new Button("Search"); TextField searchValue = new TextField(" "); Label searchlabel = new Label("Enter search Value below"); fr.add("North", searchLabel); fr.add("Center", searchValue); fr.add("South", searchButton);
Fields
Constructors
public public
Methods
public public public public public public public public public public public public public public
int int void void void void Dimension Dimension Dimension float float void void String
getHgap() getVgap() setVgap(int vGap) addLayoutComponent(Component c, Object constraintsObj) addLayoutComponent(String compName, Component c) removeLayoutComponent(Component c) minimumLayoutSize(Container cont) preferredLayoutSize(Container cont) maximumLayoutSize(Container cont) getLayoutAlignmentX(Container cont) getLayoutAlignmentY(Container cont) invalidateLayout(Container cont) layoutContainer(Container cont) toString()
Class Button
This class implements a clickable button GUI component. It has two appearances: pressed and nonpressed.
Panel buttonPanel = new Panel(); buttonPanel.setLayout(new GridLayout(3,1); Button searchButton = new Button("Search"); Button clearButton = new Button("Clear"); Button helpButton("Help"); buttonPanel.add(searchButton); buttonPanel.add(clearButton); buttonPanel.add(helpButton); // This creates three Buttons and puts them into one Panel. // By using the GridLayout, the Buttons are aligned // vertically and are all the same size.
Constructors
public public
Methods
public void public String public void public void public void public void protected void protected void
addNotify() getLabel() setLabel(String aLabel) setActionCommand(String aCommand) addActionListener(ActionListener listener) removeActionListener(ActionListener listener) processEvent(AWTEvent evt) processActionEvent(ActionEvent evt)
A Canvas represents a blank rectangular area that normally does not generate events, but on which an application can draw or receive user input. This can be done only after the Canvas receives explicit focus from the application.
Constructors
public public
Methods
public class
CardLayout is a layout manager for a container that provides a means to implement a GUI that supports a Rolodex-like appearance. When you add components or containers to the CardLayout, it logically treats them as cards, only one of which can be displayed at one time.
Constructors
public public
Methods
public public public public public public public public public public public public public public public public public public public
int void int void void void void Dimension Dimension float float void void void void void void void String
getHgap() setHgap(int hGap) getVgap() setVgap(int vGap) addLayoutComponent(Component c, Object constraintsObj) addLayoutComponent(String compName, Component com) removeLayoutComponent(Component com) minimumLayoutSize(Container aContainer) maximumLayoutSize(Container aContainer) getLayoutAlignmentX(Container aContainer) getLayoutAlignmentY(Container aContainer) invalidateLayout(Container aContainer) layoutContainer(Container aContainer) first(Container aContainer) next(Container aContainer) previous(Container aContainer) last(Container aContainer) show(Container aContainer, String card) toString()
Class Checkbox
A Checkbox component is a GUI element that can be set to an on or off state by clicking on it. The appearance of a Checkbox is determined in part by whether it is functioning as a standalone check box or as a radio button (by being added to a CheckboxGroup). Here is a code snippet showing how to instantiate Checkboxes and give them radio button behavior:
CheckboxGroup cbg = new CheckboxGroup(); Checkbox cb1 = new Checkbox("English", true, cbg); // Create a Checkbox that is checked when the // application first starts. Make the Checkbox // behave like a radio button by putting it in a // CheckboxGroup (a non-visual object). Checkbox ch2 = new Checkbox("French", cbg, false); Checkbox cb3 = new Checkbox("German", false, cbg); Note Be aware that Checkbox is spelled with a lowercase b, but the Swing counterpart, JCheckBox, uses an uppercase Bas it should, following conventional Java naming practices.
Constructors
label) label, boolean state) label, boolean state, CheckboxGroup group) label, CheckboxGroup group, boolean state)
public void public String public void public boolean public void public Object[] public CheckboxGroup public void public void public void protected void protected void protected String Class CheckboxGroup
addNotify() getLabel() setLabel(String label) getState() setState(boolean state) getSelectedObjects() getCheckboxGroup() setCheckboxGroup(CheckboxGroup g) addItemListener(ItemListener l) removeItemListener(ItemListener l) processEvent(AWTEvent e) processItemEvent(ItemEvent e) paramString()
A CheckboxGroup is a non-visual object whose sole purpose is to make the on/off state of multiple Checkbox objects mutually exclusive. That is, if three Checkbox items are added to a CheckboxGroup, only one can be on at one time. CheckboxGroup is neither a visual component nor an AWT component.
Constructors
public
Methods
CheckboxGroup()
Class Choice
A Choice component provides a noneditable, nonscrollable, drop-down list of selectable items. You can select only one item at a time. The following snippet shows how to create and populate a Choice component:
public
Methods
Choice()
public public public public public public public public public public public
void int int String void void void void void void String
addNotify() getItemCount() countItems() getItem(int index) add(String item) addItem(String item) insert(String item, int index) remove(String item) remove(int position) removeAll() getSelectedItem()
public Object[] public int public void public void public void public void protected void protected void protected String Class Color
getSelectedObjects() getSelectedIndex() select(int pos) select(String str) addItemListener(ItemListener l) removeItemListener(ItemListener l) processEvent(AWTEvent e) processItemEvent(ItemEvent e) paramString()
The class Color provides fields and methods for setting colors in components, including 16 default colors. Generally, you simply use a color as a parameter to a method, such as setBackground():
theFrame.setBackground(Color.blue); theFrame.setForeground(Color.yellow); Tip You can also use a Color object you have constructed by providing values for red, green, and blue. All the colors you see on a television, for example, are made up of only red, green, and blue pixels. If the color you want is not provided, you can use these three colors to create any color needed in an application.
Fields
Constructors
Color(int red, int green, int blue) Color(int red, int green, int blue, int Alpha) Color(int rgbValue) Color(int rgbaValue, boolean hasAnAlpha) Color(float red, float green , float blue) Color(float red, float green, float blue, float alpha) Color(ColorSpace colorSpace, float[] componentArray, float theAlpha)
public public public public public public public public public public public public public public public public public public public public public public public
int int int int int Color Color int boolean String static Color static Color static Color static Color static int static float[] static Color float[] float[] float[] float[] float[] float[]
public PaintContext
getRed() getGreen() getBlue() getAlpha() getRGB() brighter() darker() hashCode() equals(Object obj) toString() decode(String name) throws NumberFormatException getColor(String name) getColor(String name, Color value) getColor(String prop, Int val) HSBtoRGB(float aHue, float sat, float brightVal) RGBtoHSB(int red, int green, Int blue, float[] hsbvalues) getHSBColor(float hue, float sat, float brightVal) getRGBComponents(float[] compArray) getRGBColorComponents(float[] compArray) getComponents(float[] compArray) getColorComponents(float[] compArray) getComponents(ColorSpace cspace, float[] comps) getColorComponents(ColorSpace colorSpace, float[] comps) createContext(ColorModel colorModel, Rectangle rect, Rectangle2D rect2d, AffineTransform affineTxform, RenderingHints rendHints) getTransparency()
Serializable
The Component class is the superclass of all nonmenu-related AWT GUI component classes. Extending it directly results in a lightweight class with no native peer. Along with several abstract methods, the Component class provides several useful concrete methods. An application generally does not use this class directly.
Fields
TOP_ALIGNMENT LEFT_ALIGNMENT
protected
Methods
Component()
public public public public public public public public public public public public public public public public public public public public public public public
String void Container java.awt.peer.ComponentPeer void DropTarget final Object Toolkit boolean boolean boolean boolean boolean void void void void boolean void void void void void
getName() setName(String name) getParent() getPeer() setDropTarget(DropTarget target) getDropTarget() getTreeLock() getToolkit() isValid() isDisplayable() isVisible() isShowing() isEnabled() setEnabled(boolean b) enable() enable(boolean b) disable() isDoubleBuffered() enableInputMethods(boolean enableFlag) setVisible(boolean flag) show() show(boolean b) hide()
public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public
Color void Color void Font void Locale void ColorModel Point Point Point void void void Dimension Dimension void void void void Rectangle Rectangle void void void int int int int Rectangle Dimension Point boolean boolean Dimension Dimension Dimension Dimension Dimension float float void void void void Graphics FontMetrics void Cursor void void void void void void void
getForeground() setForeground(Color c) getBackground() setBackground(Color c) getFont() setFont(Font font) getLocale() setLocale(Locale locale) getColorModel() getLocation() getLocationOnScreen() location() setLocation(int x, int y) move(int x, int y) setLocation(Point pt) getSize() size() setSize(int w, int h) resize(int w, int h) setSize(Dimension dim) resize(Dimension dim) getBounds() bounds() setBounds(int x, int y, int w, int h) reshape(int x, int y, int w, int h) setBounds(Rectangle rect) getX() getY() getWidth() getHeight() getBounds(Rectangle rv) getSize(Dimension dim) getLocation(Point pt) isOpaque() isLightweight() getPreferredSize() preferredSize() getMinimumSize() minimumSize() getMaximumSize() getAlignmentX() getAlignmentY() doLayout() layout() validate() invalidate() getGraphics() getFontMetrics(Font font) setCursor(Cursor cur) getCursor() paint(Graphics gr) update(Graphics gr) paintAll(Graphics gr) repaint() repaint(long t) repaint(int x, int y, int w, int h) repaint(long t, int x, int y, int w, int h) print(Graphics gr) printAll(Graphics gr)
public boolean public Image public boolean public boolean public int public int public public public public public public public public public public boolean boolean boolean Component Component Component void final void boolean void
public void public void public void public void public void public void public void public void public void public void public void public InputMethodRequests public InputContext protected final void protected final void protected AWTEvent protected void protected void protected protected protected protected void void void void
imageUpdate (Image image, int flags, int x, int y, int w, int h) createImage(int w, int h) prepareImage(Image theImage, ImageObserver obsrvr) prepareImage(Image theImage, Int w, int h, ImageObserver obsrvr) checkImage(Image theImage, ImageObserver obsrvr) checkImage(Image theImage, int w, int h, ImageObserver observr) contains(int x, int y) inside(int x, int y) contains(Point pt) getComponentAt(int x, int y) locate(int x, int y) getComponentAt(Point pt) deliverEvent(Event event) dispatchEvent(AWTEvent event) postEvent(Event event) addComponentListener (ComponentListener listener) removeComponentListener (ComponentListener listener) addFocusListener(FocusListener listener) removeFocusListener(FocusListener listener) addKeyListener(KeyListener listener) removeKeyListener(KeyListener listener) addMouseListener(MouseListener listener) removeMouseListener(MouseListener listener) addMouseMotionListener (MouseMotionListener listener) removeMouseMotionListener (MouseMotionListener listener) addInputMethodListener (InputMethodListener listener) removeInputMethodListener (InputMethodListener listener) getInputMethodRequests() getInputContext() enableEvents(long events) disableEvents(long events) coalesceEvents(AWTEvent existingEvent, AWTEvent nextEvent) processEvent(AWTEvent event) processComponentEvent(ComponentEvent event) processFocusEvent(FocusEvent event) processKeyEvent(KeyEvent event) processMouseEvent(MouseEvent event) processMouseMotionEvent(MouseEvent event) processInputMethodEvent (InputMethodEvent event) handleEvent(Event event) mouseDown(Event event, int x, int y)
public public public public public public public public public public public
boolean boolean boolean boolean boolean boolean boolean boolean void void boolean
public boolean public boolean public void public void public void public boolean public void public void protected String public String public void public void public void public void public void public void public void public void addPropertyChange public void removePropertyChange protected void public void public ComponentOrientation Class Container
mouseDrag(Event event, int x, int y) mouseUp(Event event, int x, int y) mouseMove(Event event, int x, int y) mouseEnter(Event event, int x, int y) mouseExit(Event event, int x, int y) keyDown(Event event, Int theKey) keyUp(Event event, int theKey) action(Event event, Object whichObject) addNotify() removeNotify() gotFocus(Event event, Object whichObject) lostFocus(Event event, Object whichObject) isFocusTraversable() requestFocus() transferFocus() nextFocus() hasFocus() add(PopupMenu popup) remove(MenuComponent popup) paramString() toString() list() list(PrintStream output) list(PrintStream output, int indentation) list(PrintWriter output) list(PrintWriter output, int indentation) addPropertyChangeListener (PropertyChangeListener listener) removePropertyChangeListener (PropertyChangeListener listener) Listener(String propertyName, PropertyChangeListener listener) Listener(String propertyName, PropertyChangeListener listener) firePropertyChange(String propertyName, Object oldValue, Object newValue) setComponentOrientation (ComponentOrientation o) getComponentOrientation()
public class Container extends Component The Container class is the superclass for all containers, such as Window, Frame, Dialog, Panel, and all Swing components. Generally, an application does not use this class directly, unless it is an IDE that builds GUIs.
Constructors
public
Container()
Methods
public int public int public Component public Component[] public Insets public Insets public Component public Component public Component public void public void protected void public void public void public void public LayoutManager public void public void public void public void public void protected void public void public Dimension public Dimension public Dimension public Dimension public Dimension
getComponentCount() countComponents() getComponent(int n) getComponents() getInsets() insets() add(Component comp) add(String name, Component comp) add(Component comp, int index) add(Component comp, Object constraints) add(Component comp, Object constraints, int index) addImpl(Component comp, Object constraints, int index) remove(int index) remove(Component comp) removeAll() getLayout() setLayout(LayoutManager mgr) doLayout() layout() invalidate() validate() validateTree() setFont(Font f) getPreferredSize() preferredSize() getMinimumSize() minimumSize() getMaximumSize()
public float public float public void public void public void public void public void public void public void protected void protected void public void public Component public Component public Component public Component public Component public void public void public boolean protected String public void public void Class Cursor
getAlignmentX() getAlignmentY() paint(Graphics g) update(Graphics g) print(Graphics g) paintComponents(Graphics g) printComponents(Graphics g) addContainerListener(ContainerListener l) removeContainerListener(ContainerListener l) processEvent(AWTEvent e) processContainerEvent(ContainerEvent e) deliverEvent(Event e) getComponentAt(int x, int y) locate(int x, int y) getComponentAt(Point p) findComponentAt(int x, int y) findComponentAt(Point p) addNotify() removeNotify() isAncestorOf(Component c) paramString() list(PrintStream out, int indent) list(PrintWriter out, int indent)
This class provides for the bitmapped representation of the mouse cursor. Note that you can control the shape of the cursor within the borders of your Java application, but not the shape of the cursor elsewhere on the desktop or in other programs.
Fields
public static final int public static final int public static final int public static final int public static final int public static final int public static final int protected static Cursor[] public static final int protected String
Constructors
NE_RESIZE_CURSOR N_RESIZE_CURSOR S_RESIZE_CURSOR W_RESIZE_CURSOR E_RESIZE_CURSOR HAND_CURSOR MOVE_CURSOR predefined CUSTOM_CURSOR name
public protected
Methods
public static public static (String name) public static public int public String public String Class Dialog
A Dialog is a subclass of Window that has a title and a border. It is generally used for user input and has two states: modal and modeless. A modal Dialog blocks until it is dismissed. A modeless Dialog does not lock the application. A Dialog must always be owned by a Frame or Dialog instance. Tip For the sake of good class design and reusability, you should always subclass the Dialog class when defining a dialog. Otherwise, you must repeatedly write code for things such as login dialogs and pop-up message box dialogs.
Here's a code snippet that shows calling and constructing a Dialog object. In the GUI program:
Dialog loginDialog = new LoginDialog(theFrame, "Login Screen", true); loginDialog.setSize(500,400); loginDialog.setVisible(true); In the LoginDialog class:
public class LoginDialog extends Dialog { Frame frameRef; public LoginDialog(Frame fr, String title, boolean modal) { super(fr, title, modal); frameRef = fr; // Save for later use to // do callbacks to GUI. // finish constructing the dialog. } } Call dispose() to destroy the dialog. Note This class does not have a zero-argument constructor. You must call the Dialog constructor with parameters, and you need to call super() with at least a reference to a Frame. Because you need a Frame, and because accessing and using the Frame in which an applet exists is difficult at best, it is generally not practical to use a dialog in an applet. Generally, you will need to use some other mechanism to change screens, such as using a CardLayout with Buttons or Labels along one side to simulate a tabbed dialogunless, of course, you use Swing. Then, you can use the much better JTabbedPane.
Constructors
Dialog(Frame owner) Dialog(Frame owner, boolean modal) Dialog(Frame owner, String title) Dialog(Frame owner, String title, boolean modal) Dialog(Dialog owner) Dialog(Dialog owner, String title) Dialog(Dialog owner, String title, boolean modal)
public void public boolean public void public String public void public void public boolean public void protected String Class FileDialog
addNotify() isModal() setModal(boolean b) getTitle() setTitle(String title) show() isResizable() setResizable(boolean resizable) paramString()
public class FileDialog extends Dialog The FileDialog class represents a predefined dialog that matches the look and feel of the platform that the dialog is running on. You can use a FileDialog to get the directory and filename to use in opening or saving a file. By default, the dialog displays as an Open dialog, although you can specify that it should display as a Save As dialog. You do not have control over its appearance, although you do need to call setVisible(true) on it. Caution It is critically important to remember that a FileDialog only gets directory and filenames for you. It does not actually access any files, nor does it actually save a file to persistent storage.
Fields
LOAD SAVE
FileDialog(Frame parent) FileDialog(Frame parent, String title) FileDialog(Frame parent, String title, int mode)
Methods
public void public int public void public String public void public String public void public FilenameFilter public void protected String Class FlowLayout
addNotify() getMode() setMode(int mode) getDirectory() setDirectory(String dir) getFile() setFile(String file) getFilenameFilter() setFilenameFilter(FilenameFilter filter) paramString()
The FlowLayout layout manager uses two basic principles to lay out and size the components in a Container. It arranges components from left to right, top to bottom. Also, it sizes a component according to the minimum size required to display it properly. This means that when you use FlowLayout to position and size two buttons, the two buttons will be of different widths if their text labels are of different lengths. For this reason, and because of its simple layout strategy, this layout manager is not used much compared to the other choices.
Fields
Constructors
public public public public public public public public public public public public
int void int void int void void void Dimension Dimension void String
getAlignment() setAlignment(int align) getHgap() setHgap(int hgap) getVgap() setVgap(int vgap) addLayoutComponent(String name, Component comp) removeLayoutComponent(Component comp) preferredLayoutSize(Container target) minimumLayoutSize(Container target) layoutContainer(Container target) toString()
Class Font
The Font class represents the font for drawing text on a component. You can specify the style, type, and size of a font like this:
Font aFont = new Font("Serif", Font.BOLD, 14); TheFrame.setFont(aFont); You can make the appearance of your GUI more consistent between platforms if you always specify the font by doing a setFont() on the container.
Fields
public static final public static final public static final public static final public static final public static final protected String protected int protected int protected float
Constructors
PLAIN BOLD ITALIC ROMAN_BASELINE CENTER_BASELINE HANGING_BASELINE name style size pointSize
public public
Methods
public public public public public public public public public public public public public public public public public public public public public public
String String String String int int float boolean boolean boolean static Font static Font static Font int boolean String int int byte Map AttributedCharacterIterator.Attribute[] Font
public Font
public Font public Font public boolean public int public int
public int
public LineMetrics
public LineMetrics
getPSName() getName() getFontName() getFontName(Locale l) getStyle() getSize() getSize2D() isPlain() isBold() isItalic() getFont(String nm) decode(String str) getFont(String nm, Font font) hashCode() equals(Object obj) toString() getNumGlyphs() getMissingGlyphCode() getBaselineFor(char c) getAttributes() getAvailableAttributes() deriveFont(int style, float size) deriveFont(int style, AffineTransform trans) deriveFont(float size) deriveFont (AffineTransform trans) deriveFont(int style) deriveFont(Map attributes) canDisplay(char c) canDisplayUpTo(String str) canDisplayUpTo(char[] text, Int start, int limit) canDisplayUpTo (CharacterIterator iter, int start, int limit) getItalicAngle() hasUniformLineMetrics() getLineMetrics(String str, FontRenderContext frc) getLineMetrics(char[] chars, int beginIndex, int limit, FontRenderContext frc) getLineMetrics (CharacterIterator ci, int beginIndex, int limit,
public Rectangle2D
public Rectangle2D
public Rectangle2D
public Rectangle2D
public Rectangle2D
public GlyphVector
public GlyphVector
public GlyphVector
public GlyphVector
FontRenderContext frc) getStringBounds(String str, FontRenderContext frc) getStringBounds(String str, Int beginIndex, int limit, FontRenderContext frc) getStringBounds(char[] chars, int beginIndex, Int limit, FontRenderContext frc) getStringBounds (CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc) getMaxCharBounds (FontRenderContext frc) createGlyphVector (FontRenderContext frc, String str) createGlyphVector (FontRenderContext frc, char[] chars) createGlyphVector (FontRenderContext frc, CharacterIterator ci) createGlyphVector (FontRenderContext frc, int[] glyphCodes) finalize() throws Throwable
A Frame is a top-level container that has a title and a border and serves as the main window for a standalone Java GUI application. The following code snippet shows typical things to do with a Frame. In this case, the class subclasses Frame and serves as the main GUI class. This is a lot more convenient than creating a separate, cumbersome Frame object. The arguments for containing a Frame rather than extending it are inadequate because, if the definition of Frame changes significantly, your code is broken.
setFont("Serif", Font.PLAIN, 15); setSize(500,400); setVisible(true); ) } Caution It is critical that you call both setSize() and setVisible(true). Otherwise, your GUI will either have dimensions of 0,0 or will not be visible.
Fields
public public public public public public public public public public public public public public public public
static static static static static static static static static static static static static static static static
final final final final final final final final final final final final final final final final
int int int int int int int int int int int int int int int int
DEFAULT_CURSOR CROSSHAIR_CURSOR TEXT_CURSOR WAIT_CURSOR SW_RESIZE_CURSOR SE_RESIZE_CURSOR NW_RESIZE_CURSOR NE_RESIZE_CURSOR N_RESIZE_CURSOR S_RESIZE_CURSOR W_RESIZE_CURSOR E_RESIZE_CURSOR HAND_CURSOR MOVE_CURSOR NORMAL ICONIFIED
Constructors
public public
Methods
protected void public void public String public void public Image public void public MenuBar public void public boolean public void public void public int public void public void protected String public void
finalize() throws Throwable addNotify() getTitle() setTitle(String title) getIconImage() setIconImage(Image image) getMenuBar() setMenuBar(MenuBar mb) isResizable() setResizable(boolean resizable) setState(int state) getState() remove(MenuComponent m) removeNotify() paramString() setCursor(int cursorType)
getCursorType() getFrames()
The Graphics class is an abstract class that provides methods for drawing onto components or containers on various devices. It is used for all drawing operations, including painting the screen, drawing an image off-screen, and drawing the screen to the printer, which is a graphics-related operation.
Constructors
protected Methods
Graphics()
public public public public public public public public public public public public public public public public public public public public public
abstract Graphics Graphics abstract void abstract Color abstract void abstract void abstract void abstract Font abstract void FontMetrics abstract FontMetrics abstract Rectangle abstract void abstract void abstract Shape abstract void abstract void abstract void abstract void void abstract void
create() create(int x, int y, int width, int height) translate(int x, int y) getColor() setColor(Color c) setPaintMode() setXORMode(Color c1) getFont() setFont(Font font) getFontMetrics() getFontMetrics(Font f) getClipBounds() clipRect(int x, int y, int width, int height) setClip(int x, int y, Int width, int height) getClip() setClip(Shape clip) copyArea(int x, int y, int width, int height, int dx, int dy) drawLine(int x1, Int y1, int x2, int y2) fillRect(int x, int y, int width, int height) drawRect(int x, int y, int width, int height) clearRect(int x, int y, int width, int height)
public abstract void public abstract void public void public void public abstract void public abstract void public abstract void public abstract void public abstract void public abstract void public void public abstract void public void public abstract void public abstract void public void public void public abstract boolean public abstract boolean public abstract boolean public abstract boolean
drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) draw3DRect(int x, int y, int width, int height, boolean raised) fill3DRect(int x, int y, int width, int height, boolean raised) drawOval(int x, int y, int width, int height) fillOval(int x, int y, int width, int height) drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) drawPolyline(int[] xPoints, int[] yPoints, int nPoints) drawPolygon(int[] xPoints, Int[] yPoints, int nPoints) drawPolygon(Polygon p) fillPolygon(int[] xPoints, int[] yPoints, int nPoints) fillPolygon(Polygon p) drawString(String str, int x, int y) drawString(AttributedCharacterIterator iterator, int x, int y) drawChars(char[] data, int offset, int length, int x, int y) drawBytes(byte[] data, int offset, int length, int x, int y) drawImage(Image img, int x, int y, ImageObserver observer) drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) drawImage(Image img, Int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) dispose() finalize() toString() getClipRect() hitClip(int x, int y, int width, int height) getClipBounds(Rectangle r)
Class Graphics2D
The Graphics2D class, a subclass of Graphics, provides more sophisticated support for drawing, including 2D drawing facilities.
Constructors
protected 1
Methods
Graphics2D()
public void
public void
draw3DRect(int x, int y, int width, int height, boolean raised) fill3DRect(int x, int y, int width, int height, boolean raised) draw(Shape s) drawImage(Image img, AffineTransform xform, ImageObserver obs) drawImage(BufferedImage img, BufferedImageOp op, int x, int y) drawRenderedImage(RenderedImage img, AffineTransform xform) drawRenderableImage (RenderableImage img, AffineTransform xform) drawString(String str, int x, int y) drawString(String s, float x,
public abstract void public abstract void public abstract boolean public public public public public abstract abstract abstract abstract abstract GraphicsConfiguration void void void void
public abstract Object public public public public public public public public public public public public public public public public public public public abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract void void RenderingHints void void void void void void void void AffineTransform Paint Composite void Color Stroke void FontRenderContext
drawString (AttributedCharacterIterator iterator, float x, float y) drawGlyphVector(GlyphVector g, float x, float y) fill(Shape s) hit(Rectangle rect, Shape s, boolean onStroke) getDeviceConfiguration() setComposite(Composite comp) setPaint(Paint paint) setStroke(Stroke s) setRenderingHint (RenderingHints.Key hintKey, Object hintValue) getRenderingHint (RenderingHints.Key hintKey) setRenderingHints(Map hints) addRenderingHints(Map hints) getRenderingHints() translate(int x, int y) translate(double tx, double ty) rotate(double theta) rotate(double theta, double x, double y) scale(double sx, double sy) shear(double shx, double shy) transform(AffineTransform Tx) setTransform(AffineTransform Tx) getTransform() getPaint() getComposite() setBackground(Color color) getBackground() getStroke() clip(Shape s) getFontRenderContext()
Class GridBagConstraints
The GridBagConstraints class defines constraints that are used for sizing and positioning components with a container that has a GridBagLayout layout manager. The following code snippet shows an example of setting up the constraints for a component:
Panel bookPanel = new Panel(); BookPanel.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); Button checkOutButton = new Button("Check out"); gbc.gridx = 3; gbc.gridy = 1; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.fill = GridBagConstraints.HORIZONTAL; bookPanel.add(checkOutButton, gbc);
Fields
public public public public public public public public public public public public public public public public public public public public public public public
static static static static static static static static static static static static static static static int int int int double double int int
final final final final final final final final final final final final final final final
int int int int int int int int int int int int int int int
RELATIVE REMAINDER NONE BOTH HORIZONTAL VERTICAL CENTER NORTH NORTHEAST EAST SOUTHEAST SOUTH SOUTHWEST WEST NORTHWEST gridx gridy gridwidth gridheight weightx weighty anchor fill
public public
GridBagConstraints() GridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, Insets insets, int ipadx, int ipady)
Methods
clone()
Fields
protected static final int protected static final int protected static final int protected Hashtable protected GridBagConstraints protected java.awt.GridBagLayoutInfo public int[] public int[] public double[] public double[]
Constructors
MAXGRIDSIZE MINSIZE PREFERREDSIZE comptable defaultConstraints layoutInfo columnWidths rowHeights columnWeights rowWeights
public
Methods
GridBagLayout()
public void public GridBagConstraints protected lookupConstraints public Point public int[][] public double[][] public Point public void public void public void public Dimension public Dimension public Dimension public float public float public void public void public String protected java.awtGridBagLayoutInfo protected void protected Dimension
setConstraints(Component comp, GridBagConstraints constraints) getConstraints(Component comp) GridBagConstraints(Component comp) getLayoutOrigin() getLayoutDimensions() getLayoutWeights() location(int x, int y) addLayoutComponent(String name, Component comp) addLayoutComponent(Component comp, Object constraints) removeLayoutComponent(Component comp) preferredLayoutSize(Container parent) minimumLayoutSize(Container parent) maximumLayoutSize(Container target) getLayoutAlignmentX(Container parent) getLayoutAlignmentY(Container parent) InvalidateLayout(Container target) layoutContainer(Container parent) toString() GetLayoutInfo(Container parent, int sizeflag) AdjustForGravity(GridBagConstraints constraints, Rectangle r) GetMinSize(Container parent, java.awt.GridBagLayoutInfo info) ArrangeGrid(Container parent)
The GridLayout layout manager sizes and positions components in a grid consisting of rows and columns. Each grid cell, and hence every component, is equal in size, unlike components in a GridBagLayout, which may differ in size. Tip You can use a GridLayout in a Panel to get Buttons or Labels aligned vertically or horizontally and to make each component the same size. This contrasts with how FlowLayout sizes components. You do something like this:
Panel buttonPanel = new Panel(new GridLayout(5,1)); Button searchBtn = new Button("Search"); Button resetBtn = new Button("Reset"); Button helpBtn = new Button("Help"); buttonPanel.add(searchBtn); buttonPanel.add(new Panel()); buttonPanel.add(resetBtn); buttonPanel.add(new Panel()); buttonPanel.add(helpBtn); This code also shows another trick. By adding empty Panels, you make the Buttons shorter and/or space them as you wish.
Constructors
GridLayout() GridLayout(int rows, int cols) GridLayout(int rows, int cols, int hgap, int vgap)
public public public public public public public public public public
int void int void int void int void void void
getRows() setRows(int rows) getColumns() setColumns(int cols) getHgap() setHgap(int hgap) getVgap() setVgap(int vgap) addLayoutComponent(String name, Component comp) removeLayoutComponent(Component comp)
Class Image
This class lets you create an image from a .gif or .jpeg file or from an image created in memory.
Fields
finalObject final int final int final int final int final int
Constructors
public
Methods
Image()
getWidth(ImageObserver observer) getHeight(ImageObserver observer) getSource() getGraphics() getProperty(String name, ImageObserver observer) getScaledInstance(int width, int height, int hints) flush()
Insets represent the width of the border on each side of a container. The position within a container is relative to the insets.
Fields
Constructors
public
Methods
A Label is a GUI component for displaying read-only text. Normally Labels do not accept user interaction nor generate events. Labels can only display text, with left, right, or centered alignment. A Label is one of the few components that provides for alignment. Here's an example of making an aligned Label:
public static final int public static final int public static final int
Constructors
public void public int public void public String public void protected String Class List
A List is a GUI component that contains a scrollable list of items. A List can support multiple or single selection modes. If there are more items than can be seen without scrolling, a vertical scrollbar is automatically provided. The items in the List are text-only items. The following code snippet shows how to create and populate a List:
Panel listPanel = new Panel(new BroderLayout()); List authorList = new List(4, true); // Make a List that // shows four rows to begin with and allows // multiple selection. authorList.add("Thucidedes"); authorList.add("Luke"); authorList.add("Seutonius"); authorList.add("Tacitus"); authorList.add("Josephus"): authorList.add("Xenophon"); authorList.select(3); // Select an item in the List System.out.println("The item is: " + AuthorList.getSelectedItem()); ListPanel.add("Center", authorList); Note A List component has its own scrollbars. This is unlike a Jlist, which must be placed in a JScrollPane in order to have scrollbars.
Constructors
public void public void public int public int public String public String[] public void public void public void public void public void public void public void public void public void public void public int public int[] public String public String[] public Object[] public void public void public boolean public boolean public int public boolean public boolean public void public void public int public void public Dimension public Dimension public Dimension public Dimension public Dimension public Dimension public Dimension public Dimension public void public void public void public void protected void protected void protected void protected String public void
addNotify() removeNotify() getItemCount() countItems() getItem(int index) getItems() add(String item) addItem(String item) add(String item, int index) addItem(String item, int index) replaceItem(String newValue, int index) removeAll() clear() remove(String item) remove(int position) delItem(int position) getSelectedIndex() getSelectedIndexes() getSelectedItem() getSelectedItems() getSelectedObjects() select(int index) deselect(int index) isIndexSelected(int index) isSelected(int index) getRows() isMultipleMode() allowsMultipleSelections() setMultipleMode(boolean b) setMultipleSelections(boolean b) getVisibleIndex() makeVisible(int index) getPreferredSize(int rows) preferredSize(int rows) getPreferredSize() preferredSize() getMinimumSize(int rows) minimumSize(int rows) getMinimumSize() minimumSize() addItemListener(ItemListener l) removeItemListener(ItemListener l) addActionListener(ActionListener l) removeActionListener(ActionListener l) processEvent(AWTEvent e) processItemEvent(ItemEvent e) processActionEvent(ActionEvent e) paramString() delItems(int start, int end)
Class MediaTracker
The MediaTracker class defines an object that can be used to track the loading/downloading of images to an application or applet. It can check on the status of the image(s) being loaded or wait for the loading to complete and then continue executing other code. It is customarily run in a separate thread dedicated to loading images. Note The use of this class can improve performance of an application or applet significantly. It allows the GUI to execute on the AWT thread, even though the images are still loading. It can also tell the main GUI or execute some GUI method when the images are completely downloaded. This lets your GUI be available much faster than it would be if you had to wait for all the images to load. It is also much more satisfying for the user, who does not have to look at an hourglass cursor for a long period of time.
Fields
Constructors
public
Methods
MediaTracker(Component comp)
addImage(Image image, int id) addImage(Image image, int id, int w, int h) checkAll() checkAll(boolean load) isErrorAny()
public Object[] public void public boolean public public public public public public public public public public public int boolean boolean boolean Object[] void boolean int void void void
getErrorsAny() waitForAll() throws InterruptedException waitForAll(long ms) throws InterruptedException statusAll(boolean load) checkID(int id) checkID(int id, boolean load) isErrorID(int id) getErrorsID(int id) waitForID(int id) throws InterruptedException waitForID(int id, long ms) throws InterruptedException statusID(int id, boolean load) removeImage(Image image) removeImage(Image image, int id) removeImage(Image image, int id, int width, int height)
Class Menu
A Menu object placed on a MenuBar is a pull-down menu. Examples might be File, Edit, and Help. Here is a code snippet to illustrate the creation of a small Menu for an application:
MenuItem newMI = new MenuItem("New"); MenuItem openMI = new MenuItem("Open"); MenuItem copyMI = new MenuItem("Copy"); MenuItem pasteMI = new MenuItem("Paste"); Menu fileMenu = new Menu("File"); Menu editMenu = new Menu("Edit"); fileMenu.add(newMI); fileMenu.add(openMI); editMenu.add(copyMI); editMenu.add(pasteMI); MenuItem aboutMI = new MenuItem("About"); Menu helpMenu = new Menu("Help"); HelpMenu.add(aboutMI); MenuBar mb = new MenuBar(); mb.add(fileMenu); mb.add(editMenu); mb.setHelpMenu(helpMenu); theFrame.setMenuBar(mb); Tip To get cascading menus, in which selecting one menu choice displays multiple menu choices to the right, you add a Menu m1 to a Menu m2 and then add MenuItems to m1.
Caution You cannot readily add a MenuBar to any container but a Frame. Therefore, it is extremely difficult to add a menu to an applet. The easiest alternative is to use a CardLayout that displays multiple Panels (cards) in the applet. To select a specific card, you put a row of Buttons above the Panels and click the Button of the desired screen. This simulates a tabbed dialog.
Constructors
public public public public public public public public public public public
void void boolean int int MenuItem MenuItem void void void void
addNotify() removeNotify() isTearOff() getItemCount() countItems() getItem(int index) add(MenuItem mi) add(String label) insert(MenuItem menuitem, int index) insert(String label, int index) addSeparator()
Class MenuBar
A MenuBar component provides a menu bar for the outermost window of an application, a Frame. Unlike most components, it is not added to the Frame, but placed in the Frame with the setMenuBar method. A MenuBar cannot be placed in any other container. You cannot directly add a MenuBar to an applet. See the description of java.awt.Menu for an example of using a MenuBar. You can only put a MenuBar in a Frame, but this restriction does not apply to a JmenuBar. This makes building menus in Swing easier and much more flexible.
Constructors
public
Methods
MenuBar()
Class MenuItem
A MenuItem object represents one menu item in a menu, such as Open on a File menu. MenuItems must be placed in a menu. They cannot stand by themselves. See the description of java.awt.Menu for an example of using MenuItems.
Constructors
public void public void public void public void public MenuShortcut public void public void protected final void protected final void public void public String public void public void protected void protected void public String Class MenuShortcut
setEnabled(boolean b) enable() enable(boolean b) disable() getShortcut() setShortcut(MenuShortcut s) deleteShortcut() enableEvents(long eventsToEnable) disableEvents(long eventsToDisable) setActionCommand(String command) getActionCommand() addActionListener(ActionListener l) removeActionListener(ActionListener l) processEvent(AWTEvent e) processActionEvent(ActionEvent e) paramString()
A MenuShortcut represents a keyboard shortcut to perform a specific MenuItem choice. Just as when you click on a MenuItem, the shortcut generates an ActionEvent for the MenuItem. Here's a code snippet showing how to create a shortcut:
Menu fileMenu = new Menu("File"); MenuItem openMI = new MenuItem("Open", new MenuShortcut(KeyEvent.VK_N)); openMI.setActionCommand("Open"); // Required for shortcut fileMenu.add(openMI);
Constructors
public public
Methods
public int public boolean public boolean public boolean public int public String protected String Class Panel
public class Panel extends Container A Panel is a basic container. It has no border or title bar. It is best to put Components into Panels and then put Panels into Frames in order to increase the flexibility of a GUI. Doing this is also essential for creating complex screens using the AWT. You cannot do much with a Panel by itself. It is only a rectangle with no borders. In fact, the Panel class is often subclassed in order to create a bordered panel. Here is simple code for creating a Panel:
Constructors
public public
Methods
addNotify()
public class Point extends Point2D implements Serializable A Point represents a visual point based on x and y coordinates in the coordinate space.
Fields
x y
Constructors
public public public public public public public public public public
double double Point void void void void void boolean String
getX() getY() getLocation() setLocation(Point p) setLocation(int x, int y) setLocation(double x, double y) move(int x, int y) translate(int x, int y) equals(Object obj) toString()
Class PopupMenu
public class PopupMenu extends Menu The PopupMenu class provides for a pop-up menu that appears at given coordinates within a Component. Because some platforms do not support a right mouse button, the PopupMenu class does not directly support activation through the right mouse button.
Constructors
public public
Methods
Class PrintJob
A PrintJob object provides for printing a Graphics object to a printing device. You request a PrintJob object from the system and return it when done. It provides for the print dialog automatically. The PrintJob object has been superseded by the PrinterJob class in Java 2.
Constructors
public
Methods
PrintJob()
Class Rectangle
The Rectangle class represents a rectangular area on the screen. Because all JDK AWT components are rectangular in shape, a Rectangle object can be used to specify the bounds of another component. Making a Rectangle is fairly simple. You can use it in a number of ways. One way is to define the rectangular area that a component, sized with absolute positioning, should fill. Here's a code snippet to illustrate this:
Panel pan = new Panel(); pan.setLayout(null); Label bookLabel = new Label("Book Info"); bookLabel.setBounds(new Rectangle(50,50,120,20)); // Using absolute positioning is not recommended because this // component will only look good on one platform. pan.add(bookLabel);
Fields
x y width height
Constructors
Rectangle() Rectangle(Rectangle r) Rectangle(int x, int y, int width, int height) Rectangle(int width, int height) Rectangle(Point p, Dimension d) Rectangle(Point p) Rectangle(Dimension d)
public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public
double double double double Rectangle Rectangle2D void void void void Point void void void void Dimension void void void boolean boolean boolean boolean boolean boolean Rectangle Rectangle void void void void boolean int Rectangle2D Rectangle2D boolean String
getX() getY() getWidth() getHeight() getBounds() getBounds2D() setBounds(Rectangle r) setBounds(int x, int y, int width, int height) setRect(double x, double y, double width, double height) reshape(int x, int y, int width, Int height) getLocation() setLocation(Point p) setLocation(int x, int y) move(int x, int y) translate(int x, int y) getSize() setSize(Dimension d) setSize(int width, int height) resize(int width, int height) contains(Point p) contains(int x, int y) contains(Rectangle r) contains(int X, int Y, int W, int H) inside(int x, int y) intersects(Rectangle r) intersection(Rectangle r) union(Rectangle r) add(int newx, int newy) add(Point pt) add(Rectangle r) grow(int h, int v) isEmpty() outcode(double x, double y) createIntersection(Rectangle2D r) createUnion(Rectangle2D r) equals(Object obj) toString()
Class ScrollPane
A ScrollPane is a special container that provides horizontal and vertical scrollbars and manages user scrolling automatically. It is far superior to the Scrollbar class and provides great flexibility in scrollbar usage. Here's an example of how to make one:
ScrollPane sp = new ScrollPane(SCROLLBARS_ALWAYS); // Create a ScrollPane that always shows scrollbars. Panel pan = new Panel(); // ScrollPanes must be put in a container. pan.add(sp); sp.add(bigImage);
Fields
public static final int public static final int public static final int
Constructors
public public
Methods
protected final void addImpl(Component comp, Object constraints, int index) public int getScrollbarDisplayPolicy() public Dimension getViewportSize() public int getHScrollbarHeight() public int getVScrollbarWidth() public Adjustable getVAdjustable() public Adjustable getHAdjustable() public void setScrollPosition(int x, int y) public void setScrollPosition(Point p) public Point getScrollPosition() public final void setLayout(LayoutManager mgr) public void doLayout() public void layout() public void printComponents(Graphics g) public void addNotify() public String paramString() Class TextArea
A TextArea provides an area for typing or viewing multiple lines of text, although the entire contents are accessed programmatically as one String object. A TextArea provides its own scrollbars. By setting scrolling to SCROLLBARS_VERTICAL_ONLY, the TextArea object will do automatic word-wrap. Other than this feature, a TextArea is a fairly simple text editing area, in contrast to a JEditorpane, for example.
Fields
Constructors
TextArea() TextArea(String text) TextArea(int rows, int columns) TextArea(String text, int rows, int columns) TextArea(String text, int rows, int columns, int scrollbars)
public void public void public void public void public void public void public void public int public void public int public void public int public Dimension public Dimension public Dimension public Dimension public Dimension public Dimension public Dimension public Dimension protected String Class TextComponent
addNotify() insert(String str, int pos) insertText(String str, int pos) append(String str) appendText(String str) replaceRange(String str, int start, int end) replaceText(String str, int start, Int end) getRows() setRows(int rows) getColumns() setColumns(int columns) getScrollbarVisibility() getPreferredSize(int rows, int columns) preferredSize(int rows, int columns) getPreferredSize() preferredSize() getMinimumSize(int rows, int columns) minimumSize(int rows, int columns) getMinimumSize() minimumSize() paramString()
extends Component The TextComponent class is the superclass of TextField and TextArea. Normally, a TextComponent is not instantiated directly, but because TextComponent has many methods that are useful for both TextField and TextArea, familiarity with TextComponent is quite helpful.
Fields
textListener
Methods
public void public void public String public String public boolean public void public int public void public int public void public void public void public void public int public void public void protected void protected void protected String
removeNotify() setText(String t) getText() getSelectedText() isEditable() setEditable(boolean b) getSelectionStart() setSelectionStart(int selectionStart) getSelectionEnd() setSelectionEnd(int selectionEnd) select(int selectionStart, int selectionEnd) selectAll() setCaretPosition(int position) getCaretPosition() addTextListener(TextListener l) removeTextListener(TextListener l) processEvent(AWTEvent e) processTextEvent(TextEvent e) paramString()
Class TextField
public class TextField extends TextComponent A TextField is a GUI component that allows the entering of a single line of text.
Constructors
Methods
public void public void public boolean public int public void public Dimension public Dimension public Dimension public Dimension public Dimension public Dimension public Dimension public Dimension public void public void protected void protected void protected String Class Toolkit
setEchoCharacter(char c) setText(String t) echoCharIsSet() getColumns() setColumns(int columns) getPreferredSize(int columns) preferredSize(int columns) getPreferredSize() preferredSize() getMinimumSize(int columns) minimumSize(int columns) getMinimumSize() minimumSize() addActionListener(ActionListener l) removeActionListener(ActionListener l) processEvent(AWTEvent e) processActionEvent(ActionEvent e) paramString()
This is the abstract superclass of all Toolkit implementations and has several useful methods, such as getImage(). The Toolkit subclasses bind components to their native peer implementations. Here's one of the many handy things you can do with a Toolkit object:
public
Methods
Toolkit()
createButton(Buttontarget) createTextField (TextField target) protected abstract java.awt.peer.LabelPeer createLabel(Label target) protected abstract java.awt.peer.ListPeer createList(List target) protected abstract java.awt.peer.CheckboxPeer createCheckbox(Checkbox target) protected abstract java.awt.peer.ScrollbarPeer createScrollbar (Scrollbar target) protected abstract java.awt.peer.ScrollPanePeer createScrollPane (ScrollPane target) protected abstract java.awt.peer.TextAreaPeer createTextArea(TextArea target) protected abstract java.awt.peer.ChoicePeer createChoice(Choice target) protected abstract java.awt.peer.FramePeer createFrame(Frame target) protected abstract java.awt.peer.CanvasPeer createCanvas(Canvas target) protected abstract java.awt.peer.PanelPeer createPanel(Panel target) protected abstract java.awt.peer.WindowPeer createWindow(Window target) protected abstract java.awt.peer.DialogPeer createDialog(Dialog target) protected abstract java.awt.peer.MenuBarPeer createMenuBar(MenuBar target) protected abstract java.awt.peer.MenuPeer createMenu(Menu target) protected abstract java.awt.peer.PopupMenuPeer createPopupMenu (PopupMenu target) protected abstract java.awt.peer.MenuItemPeer createMenuItem(MenuItem target) protected abstract java.awt.peer.FileDialogPeer createFileDialog (FileDialog target) protected abstract java.awt.peer. createCheckboxMenuItem CheckboxMenuItemPeer (CheckboxMenuItem target) protected java.awt.peer createComponentLightweightPeer (Component target) protected abstract java.awt.peer.FontPeer getFontPeer(String name, int style) protected void loadSystemColors (int[] systemColors) public abstract Dimension getScreenSize() public abstract int getScreenResolution() public abstract ColorModel getColorModel() public abstract String[] getFontList() public abstract FontMetrics getFontMetrics(Font font) public abstract void sync() public static Toolkit getDefaultToolkit() public abstract Image getImage(String filename) public abstract Image getImage(URL url) public abstract Image createImage(String filename) public abstract Image createImage(URL url) public abstract boolean prepareImage(Image image, int width, int height,
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=158 (68 of 92) [6/2/2002 10:53:49 PM]
public abstract void public abstract Clipboard public int protected static Container public Cursor
public Dimension
public int public static String public final EventQueue protected abstract EventQueue public abstract java.awt.dnd.peer
public DragGestureRecognizer
ImageObserver observer) checkImage(Image image, int width, int height, ImageObserver observer) createImage (ImageProducer producer) createImage(byte[] imagedata) createImage(byte[] imagedata, int imageoffset, int imagelength) getPrintJob(Frame frame, String jobtitle, Properties props) beep() getSystemClipboard() getMenuShortcutKeyMask() getNativeContainer (Component c) createCustomCursor (Image cursor, Point hotSpot, String name) throws IndexOutOfBoundsException getBestCursorSize (int preferredWidth, int preferredHeight) getMaximumCursorColors() getProperty(String key, String defaultValue) getSystemEventQueue() getSystemEventQueueImpl() createDragSourceContextPeer (DragGestureEvent DragSourceContextPeer dge) throws InvalidDnDOperationException createDragGestureRecognizer (Class abstractRecognizerClass, DragSource ds, Component c, int srcActions, DragGestureListener dgl) getDesktopProperty (String propertyName) setDesktopProperty (String name, Object newValue) Property(String name) Properties() Listener(String name, PropertyChangeListener pcl)
public void
public void
Listener(String name, PropertyChangeListener pcl) addAWTEventListener (AWTEventListener listener, long eventMask) removeAWTEventListener (AWTEventListener listener)
Class Window
A Window is a top-level container. It is the parent class for Frame and Dialog. It has no border and no title bar. Generally, one does not directly instantiate a Window object.
Constructors
public public
Methods
protected void public void public void public void public void public void public void public Toolkit public final String public Locale public InputContext public Window public Window[] public void public void protected void protected void public Component public boolean public boolean public void public void
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
finalize() throws Throwable addNotify() pack() show() dispose() toFront() toBack() getToolkit() getWarningString() getLocale() getInputContext() getOwner() getOwnedWindows() addWindowListener(WindowListener l) removeWindowListener(WindowListener l) processEvent(AWTEvent e) processWindowEvent(WindowEvent e) getFocusOwner() postEvent(Event e) isShowing() applyResourceBundle(ResourceBundle rb) applyResourceBundle(String rbName)
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
containers Button class constructors methods 2nd buttons clickable Canvas class constructors methods CardLayout class constructors methods 2nd Checkbox class code constructors methods CheckboxGroup class constructors methods Choice class code constructors methods 2nd classes AWTEvent constructors fields 2nd methods AWTEventMulticaster code 2nd constructors fields methods 2nd 3rd BorderLayout code constructors fields 2nd methods 2nd Button constructors methods 2nd Canvas constructors methods CardLayout constructors methods 2nd Checkbox code constructors methods CheckboxGroup constructors methods Choice code constructors methods 2nd Color constructors fields methods 2nd Component constructors
fields methods 2nd 3rd 4th Container constructors methods 2nd Cursor constructors fields 2nd methods Dialog code constructors methods FileDialog constructors fields methods 2nd FlowLayout constructors fields methods 2nd Font constructors fields methods 2nd 3rd Frame code constructors fields 2nd methods 2nd GidBagConstraints Graphics constructors methods 2nd 3rd Graphics2D constructors methods 2nd GridBagConstraints code constructors fields 2nd methods GridBagLayout constructors fields methods 2nd GridLayout code constructors methods 2nd Image constructors fields methods 2nd Insets constructors fields methods Label constructors fields methods List code
constructors methods 2nd MediaTracker constructors fields methods 2nd Menu code constructors methods 2nd MenuBar constructors methods MenuItem constructors methods 2nd MenuShortcut code constructors methods Panel code constructors methods Point constructors fields methods 2nd PopupMenu constructors methods PrintJob constructors methods Rectangle code constructors fields methods 2nd ScrollPane code constructors fields methods 2nd TextArea constructors fields methods 2nd TextComponent fields methods 2nd TextField constructors methods 2nd Toolkit constructors fields methods 2nd 3rd Transparency Window constructors methods 2nd clickable buttons code
AWTEventMulticaster class 2nd BorderLayout class Checkbox class Choice class Dialog class Frame class GridBagConstraints class GridLayout class List class Menu class MenuShortcut class Panel class Rectangle class ScrollPane class Color class constructors fields methods 2nd Component class constructors fields methods 2nd 3rd 4th components arranging and sizing constraints defining positioning sizing Composite interface methods CompositeContext interface methods constraints defining constructors AWTEvent class AWTEventMulticaster class BorderLayout class Button class Canvas class CardLayout class Checkbox class CheckboxGroup class Choice class Color class Component class Container class Cursor class Dialog class FileDialog class FlowLayout class Font class Frame class Graphics class Graphics2D class GridBagConstraints class GridBagLayout class GridLayout class Image class Insets class Label class List class MediaTracker class Menu class MenuBar class
MenuItem class MenuShortcut class Panel class Point class PopupMenu class PrintJob class Rectangle class ScrollPane class TextArea class TextField class Toolkit class Window class Container class constructors methods 2nd containers borders components arranging and sizing coordinates x and y Cursor class constructors fields 2nd methods cursors mouse Dialog class code constructors methods event dispatching multicast fields Adjustable interface AWTEvent class 2nd AWTEventMulticaster class BorderLayout class 2nd Color class Component class Cursor class 2nd FileDialog class FlowLayout class Font class Frame class 2nd GridBagConstraints class 2nd GridBagLayout class Image class Insets class Label class MediaTracker class Point class Rectangle class ScrollPane class TextArea class TextComponent class Toolkit class Transparency interface FileDialog class constructors fields methods 2nd files .gif images, creating
.jpeg images, creating FlowLayout class constructors fields methods 2nd Font class constructors fields methods 2nd 3rd Frame class code constructors fields 2nd methods 2nd GidBagConstraints class Graphics class constructors methods 2nd 3rd Graphics2D class constructors methods 2nd GridBagConstraints class code constructors fields 2nd methods GridBagLayout class constructors fields methods 2nd GridLayout class code constructors methods 2nd grids components positioning sizing Image class constructors fields methods 2nd images creating from .gif and .jpeg files Insets class constructors fields methods interfaces ActiveEvent methods Adjustable fields methods 2nd Composite methods CompositeContext methods ItemSelectable methods LayoutManager methods LayoutManager2
methods MenuContainer methods Paint methods PaintContext methods PrintGraphics methods Shape methods 2nd Transparency fields methods ItemSelectable interface methods java.awt ActiveEvent interface methods Adjustable interface fields methods 2nd AWTEvent class constructors fields 2nd methods AWTEventMulticaster class code 2nd constructors fields methods 2nd 3rd BorderLayout class code constructors fields 2nd methods 2nd Button class constructors methods 2nd Canvas class constructors methods CardLayout class constructors methods 2nd Checkbox class code constructors methods CheckboxGroup class constructors methods Choice class code constructors methods 2nd classes AWTEvent AWTEvent, constructors AWTEvent, fields 2nd AWTEvent, methods AWTEventMulticaster AWTEventMulticaster, constructors AWTEventMulticaster, fields AWTEventMulticaster, methods 2nd 3rd
BorderLayout BorderLayout, constructors BorderLayout, fields 2nd BorderLayout, methods 2nd Button Button, constructors Button, methods 2nd Canvas Canvas, constructors Canvas, methods CardLayout CardLayout, constructors CardLayout, methods 2nd Checkbox Checkbox, constructors Checkbox, methods CheckboxGroup CheckboxGroup, constructors CheckboxGroup, methods Choice Choice, constructors Choice, methods 2nd Color Color, constructors Color, fields Color, methods 2nd Component Component, constructors Component, fields Component, methods 2nd 3rd 4th Container Container, constructors Container, methods 2nd Cursor Cursor, constructors Cursor, fields 2nd Cursor, methods Dialog Dialog, constructors Dialog, methods FileDialog FileDialog, constructors FileDialog, fields FileDialog, methods 2nd FlowLayout FlowLayout, constructors FlowLayout, fields FlowLayout, methods 2nd Font Font, constructors Font, fields Font, methods 2nd 3rd Frame Frame, constructors Frame, fields 2nd Frame, methods 2nd GidBagConstraints Graphics Graphics, constructors Graphics, methods 2nd 3rd Graphics2D Graphics2D, constructors Graphics2D, methods 2nd GridBagConstraints, constructors
GridBagConstraints, fields 2nd GridBagConstraints, methods GridBagLayout GridBagLayout, constructors GridBagLayout, fields GridBagLayout, methods 2nd GridLayout GridLayout, constructors GridLayout, methods 2nd Image Image, constructors Image, fields Image, methods 2nd Insets Insets, constructors Insets, fields Insets, methods Label Label, constructors Label, fields Label, methods List List, constructors List, methods 2nd MediaTracker MediaTracker, constructors MediaTracker, fields MediaTracker, methods 2nd Menu Menu, constructors Menu, methods 2nd MenuBar MenuBar, constructors MenuBar, methods MenuItem MenuItem, constructors MenuItem, methods 2nd MenuShortcut MenuShortcut, constructors MenuShortcut, methods Panel Panel, constructors Panel, methods Point Point, constructors Point, fields Point, methods 2nd PopupMenu PopupMenu, constructors PopupMenu, methods PrintJob PrintJob, constructors PrintJob, methods Rectangle Rectangle, constructors Rectangle, fields Rectangle, methods 2nd ScrollPane ScrollPane, constructors ScrollPane, fields ScrollPane, methods 2nd TextArea TextArea, constructors TextArea, fields
TextArea, methods 2nd TextComponent TextComponent, fields TextComponent, methods 2nd TextField TextField, constructors TextField, methods 2nd Toolkit Toolkit, constructors Toolkit, fields Toolkit, methods 2nd 3rd Window Window, constructors Window, methods 2nd Color class constructors fields methods 2nd Component class constructors fields methods 2nd 3rd 4th Composite interface methods CompositeContext interface methods Container class constructors methods 2nd Cursor class constructors fields 2nd methods Dialog class code constructors methods FileDialog class constructors fields methods 2nd FlowLayout class constructors fields methods 2nd Font class constructors fields methods 2nd 3rd Frame class code constructors fields 2nd methods 2nd GidBagConstraints class Graphics class constructors methods 2nd 3rd Graphics2D class constructors methods 2nd GridBagConstraints class code constructors
fields 2nd methods GridBagLayout class constructors fields methods 2nd GridLayout class code constructors methods 2nd Image class constructors fields methods 2nd Insets class constructors fields methods interfaces ActiveEvent ActiveEvent, methods Adjustable Adjustable, fields Adjustable, methods 2nd Composite Composite, methods CompositeContext CompositeContext, methods ItemSelectable ItemSelectable, methods LayoutManager LayoutManager, methods LayoutManager2 LayoutManager2, methods MenuContainer MenuContainer, methods Paint Paint, methods PaintContext PaintContext, methods PrintGraphics PrintGraphics, methods Shape Shape, methods 2nd Transparency Transparency, fields Transparency, methods ItemSelectable interface methods Label class constructors fields methods LayoutManager interface methods LayoutManager2 interface methods List class code constructors methods 2nd MediaTracker class constructors fields
methods 2nd Menu class code constructors methods 2nd MenuBar class constructors methods MenuContainer interface methods MenuItem class constructors methods 2nd MenuShortcut class code constructors methods Paint interface methods PaintContext interface methods Panel class code constructors methods Point class constructors fields methods 2nd PopupMenu class constructors methods PrintGraphics interface methods PrintJob class constructors methods Rectangle class code constructors fields methods 2nd ScrollPane class code constructors fields methods 2nd Shape interface methods 2nd TextArea class constructors fields methods 2nd TextComponent class fields methods 2nd TextField class constructors methods 2nd Toolkit class constructors fields methods 2nd 3rd Transparency interface
fields methods Window class constructors methods 2nd Label class constructors fields methods layout managers components arranging and sizing LayoutManager interface methods LayoutManager2 interface methods List class code constructors methods 2nd lists scrollable MediaTracker class constructors fields methods 2nd Menu class code constructors methods 2nd MenuBar class constructors methods MenuContainer interface methods MenuItem class constructors methods 2nd MenuShortcut class code constructors methods methods ActiveEvent interface Adjustable interface 2nd AWTEvent class AWTEventMulticaster class 2nd 3rd BorderLayout class 2nd Button class 2nd Canvas class CardLayout class 2nd Checkbox class CheckboxGroup class Choice class 2nd Color class 2nd Component class 2nd 3rd 4th Composite interface CompositeContext interface Container class 2nd Cursor class Dialog class FileDialog class 2nd FlowLayout class 2nd Font class 2nd 3rd Frame class 2nd
Graphics class 2nd 3rd Graphics2D class 2nd GridBagConstraints class GridBagLayout class 2nd GridLayout class 2nd Image class 2nd Insets class ItemSelectable interface Label class LayoutManager interface LayoutManager2 interface List class 2nd MediaTracker class 2nd Menu class 2nd MenuBar class MenuContainer interface MenuItem class 2nd MenuShortcut class Paint interface PaintContext interface Panel class Point class 2nd PopupMenu class PrintGraphics interface PrintJob class Rectangle class 2nd ScrollPane class 2nd Shape interface 2nd TextArea class 2nd TextComponent class 2nd TextField class 2nd Toolkit class 2nd 3rd Transparency interface Window class 2nd mouse cursors multicast event dispatching packages java.awt ActiveEvent interface ActiveEvent interface, methods Adjustable interface Adjustable interface, fields Adjustable interface, methods 2nd AWTEvent class AWTEvent class, constructors AWTEvent class, fields 2nd AWTEvent class, methods AWTEventMulticaster class AWTEventMulticaster class, constructors AWTEventMulticaster class, fields AWTEventMulticaster class, methods 2nd 3rd BorderLayout class BorderLayout class, constructors BorderLayout class, fields 2nd BorderLayout class, methods 2nd Button class Button class, constructors Button class, methods 2nd Canvas class Canvas class, constructors Canvas class, methods CardLayout class CardLayout class, constructors CardLayout class, methods 2nd
Checkbox class Checkbox class, constructors Checkbox class, methods CheckboxGroup class CheckboxGroup class, constructors CheckboxGroup class, methods Choice class Choice class, constructors Choice class, methods 2nd classes, AWTEvent classes, AWTEvent, constructors classes, AWTEvent, fields 2nd classes, AWTEvent, methods classes, AWTEventMulticaster classes, AWTEventMulticaster, constructors classes, AWTEventMulticaster, fields classes, AWTEventMulticaster, methods 2nd 3rd classes, BorderLayout classes, BorderLayout, constructors classes, BorderLayout, fields 2nd classes, BorderLayout, methods 2nd classes, Button classes, Button, constructors classes, Button, methods 2nd classes, Canvas classes, Canvas, constructors classes, Canvas, methods classes, CardLayout classes, CardLayout, constructors classes, CardLayout, methods 2nd classes, Checkbox classes, Checkbox, constructors classes, Checkbox, methods classes, CheckboxGroup classes, CheckboxGroup, constructors classes, CheckboxGroup, methods classes, Choice classes, Choice, constructors classes, Choice, methods 2nd classes, Color classes, Color, constructors classes, Color, fields classes, Color, methods 2nd classes, Component classes, Component, constructors classes, Component, fields classes, Component, methods 2nd 3rd 4th classes, Container classes, Container, constructors classes, Container, methods 2nd classes, Cursor classes, Cursor, constructors classes, Cursor, fields 2nd classes, Cursor, methods classes, Dialog classes, Dialog, constructors classes, Dialog, methods classes, FileDialog classes, FileDialog, constructors classes, FileDialog, fields classes, FileDialog, methods 2nd classes, FlowLayout classes, FlowLayout, constructors classes, FlowLayout, fields
classes, FlowLayout, methods 2nd classes, Font classes, Font, constructors classes, Font, fields classes, Font, methods 2nd 3rd classes, Frame classes, Frame, constructors classes, Frame, fields 2nd classes, Frame, methods 2nd classes, GidBagConstraints classes, Graphics classes, Graphics, constructors classes, Graphics, methods 2nd 3rd classes, Graphics2D classes, Graphics2D, constructors classes, Graphics2D, methods 2nd classes, GridBagConstraints, constructors classes, GridBagConstraints, fields 2nd classes, GridBagConstraints, methods classes, GridBagLayout classes, GridBagLayout, constructors classes, GridBagLayout, fields classes, GridBagLayout, methods 2nd classes, GridLayout classes, GridLayout, constructors classes, GridLayout, methods 2nd classes, Image classes, Image, constructors classes, Image, fields classes, Image, methods 2nd classes, Insets classes, Insets, constructors classes, Insets, fields classes, Insets, methods classes, Label classes, Label, constructors classes, Label, fields classes, Label, methods classes, List classes, List, constructors classes, List, methods 2nd classes, MediaTracker classes, MediaTracker, constructors classes, MediaTracker, fields classes, MediaTracker, methods 2nd classes, Menu classes, Menu, constructors classes, Menu, methods 2nd classes, MenuBar classes, MenuBar, constructors classes, MenuBar, methods classes, MenuItem classes, MenuItem, constructors classes, MenuItem, methods 2nd classes, MenuShortcut classes, MenuShortcut, constructors classes, MenuShortcut, methods classes, Panel classes, Panel, constructors classes, Panel, methods classes, Point classes, Point, constructors classes, Point, fields classes, Point, methods 2nd
classes, PopupMenu classes, PopupMenu, constructors classes, PopupMenu, methods classes, PrintJob classes, PrintJob, constructors classes, PrintJob, methods classes, Rectangle classes, Rectangle, constructors classes, Rectangle, fields classes, Rectangle, methods 2nd classes, ScrollPane classes, ScrollPane, constructors classes, ScrollPane, fields classes, ScrollPane, methods 2nd classes, TextArea classes, TextArea, constructors classes, TextArea, fields classes, TextArea, methods 2nd classes, TextComponent classes, TextComponent, fields classes, TextComponent, methods 2nd classes, TextField classes, TextField, constructors classes, TextField, methods 2nd classes, Toolkit classes, Toolkit, constructors classes, Toolkit, fields classes, Toolkit, methods 2nd 3rd classes, Window classes, Window, constructors classes, Window, methods 2nd Color class Color class, constructors Color class, fields Color class, methods 2nd Component class Component class, constructors Component class, fields Component class, methods 2nd 3rd 4th Composite interface Composite interface, methods CompositeContext interface CompositeContext interface, methods Container class Container class, constructors Container class, methods 2nd Cursor class Cursor class, constructors Cursor class, fields 2nd Cursor class, methods Dialog class Dialog class, constructors Dialog class, methods FileDialog class FileDialog class, constructors FileDialog class, fields FileDialog class, methods 2nd FlowLayout class FlowLayout class, constructors FlowLayout class, fields FlowLayout class, methods 2nd Font class Font class, constructors Font class, fields
Font class, methods 2nd 3rd Frame class Frame class, constructors Frame class, fields 2nd Frame class, methods 2nd GidBagConstraints class Graphics class Graphics class, constructors Graphics class, methods 2nd 3rd Graphics2D class Graphics2D class, constructors Graphics2D class, methods 2nd GridBagConstraints class, constructors GridBagConstraints class, fields 2nd GridBagConstraints class, methods GridBagLayout class GridBagLayout class, constructors GridBagLayout class, fields GridBagLayout class, methods 2nd GridLayout class GridLayout class, constructors GridLayout class, methods 2nd Image class Image class, constructors Image class, fields Image class, methods 2nd Insets class Insets class, constructors Insets class, fields Insets class, methods interfaces, ActiveEvent interfaces, ActiveEvent, methods interfaces, Adjustable interfaces, Adjustable, fields interfaces, Adjustable, methods 2nd interfaces, Composite interfaces, Composite, methods interfaces, CompositeContext interfaces, CompositeContext, methods interfaces, ItemSelectable interfaces, ItemSelectable, methods interfaces, LayoutManager interfaces, LayoutManager, methods interfaces, LayoutManager2 interfaces, LayoutManager2, methods interfaces, MenuContainer interfaces, MenuContainer, methods interfaces, Paint interfaces, Paint, methods interfaces, PaintContext interfaces, PaintContext, methods interfaces, PrintGraphics interfaces, PrintGraphics, methods interfaces, Shape interfaces, Shape, methods 2nd interfaces, Transparency interfaces, Transparency, fields interfaces, Transparency, methods ItemSelectable interface ItemSelectable interface, methods Label class Label class, constructors Label class, fields Label class, methods
LayoutManager interface LayoutManager interface, methods LayoutManager2 interface LayoutManager2 interface, methods List class List class, constructors List class, methods 2nd MediaTracker class MediaTracker class, constructors MediaTracker class, fields MediaTracker class, methods 2nd Menu class Menu class, constructors Menu class, methods 2nd MenuBar class MenuBar class, constructors MenuBar class, methods MenuContainer interface MenuContainer interface, methods MenuItem class MenuItem class, constructors MenuItem class, methods 2nd MenuShortcut class MenuShortcut class, constructors MenuShortcut class, methods Paint interface Paint interface, methods PaintContext interface PaintContext interface, methods Panel class Panel class, constructors Panel class, methods Point class Point class, constructors Point class, fields Point class, methods 2nd PopupMenu class PopupMenu class, constructors PopupMenu class, methods PrintGraphics interface PrintGraphics interface, methods PrintJob class PrintJob class, constructors PrintJob class, methods Rectangle class Rectangle class, constructors Rectangle class, fields Rectangle class, methods 2nd ScrollPane class ScrollPane class, constructors ScrollPane class, fields ScrollPane class, methods 2nd Shape interface Shape interface, methods 2nd TextArea class TextArea class, constructors TextArea class, fields TextArea class, methods 2nd TextComponent class TextComponent class, fields TextComponent class, methods 2nd TextField class TextField class, constructors TextField class, methods 2nd
Toolkit class Toolkit class, constructors Toolkit class, fields Toolkit class, methods 2nd 3rd Transparency interface Transparency interface, fields Transparency interface, methods Window class Window class, constructors Window class, methods 2nd Paint interface methods PaintContext interface methods Panel class code constructors methods Point class constructors fields methods 2nd PopupMenu class constructors methods PrintGraphics interface methods PrintJob class constructors methods Rectangle class code constructors fields methods 2nd scrollable lists ScrollPane class code constructors fields methods 2nd Shape interface methods 2nd TextArea class constructors fields methods 2nd TextComponent class fields methods 2nd TextField class constructors methods 2nd Toolkit class constructors fields methods 2nd 3rd Transparency interface fields methods Window class constructors methods 2nd x and y coordinates
All Books
Search
Java > PURE Java 2 > 21. java.awt > java.awt.datatransfer Interface and Class Details < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019245187063220121094006122
public abstract interface ClipboardOwner This interface is used by classes that provide input to the system clipboard.
Methods
public void
Interface FlavorMap
public abstract interface FlavorMap This is used by implementing classes to map Native types to Mime types and associated DataFlavors.
Methods
Interface Transferable
public abstract interface Transferable This interface declares methods for use by classes that can transfer data.
Methods
This class provides methods to copy, cut, and paste data. Listing 21.1 contains sample code you can use to provide the functionality of copy, cut, and paste in your program. This program is intended for you to copy, cut, and paste between the two TextAreas in one Frame. However, you can paste to the right-most window from other textual sources, such as a word processing program that is not in Java.
Listing 21.1 Copy, Cut, and Paste Using the Clipboard Class (EditableDriver.java)
// Program to demonstrate copy, cut, and paste // using java.awt.datatransfer. import java.awt.*; import java.awt.event.*; import java.awt.datatransfer.*; import java.io.*; class EditableGUI extends Frame { public TextArea sourceTA; public TextArea targetTA; Menu editMenu; MenuItem copyMI; MenuItem cutMI; MenuItem pasteMI; MenuBar mb;
public EditableGUI() { sourceTA = new TextArea(60,10); targetTA = new TextArea(60,10); // Create two TextAreas to demonstrate // copy, cut and paste. Panel sourcePanel = new Panel(); sourcePanel.setLayout(new BorderLayout()); // Create a Panel with a BorderLayout. sourcePanel.add("North", new Label("Source")); sourcePanel.add("Center", sourceTA); Panel targetPanel = new Panel(); targetPanel.setLayout(new BorderLayout()); targetPanel.add("North", new Label("Target")); targetPanel.add("Center", targetTA); // The above code creates two Panels to use for // copying and pasting between. // Now build the menus for copying, // cutting, and pasting. editMenu = new Menu("Edit"); copyMI = new MenuItem("Copy"); cutMI = new MenuItem("Cut"); pasteMI = new MenuItem("Paste"); mb = new MenuBar(); editMenu.add(copyMI); editMenu.add(cutMI); editMenu.addSeparator(); // You can easily add a separator line // in a Java menu. editMenu.add (pasteMI); mb.add(editMenu); setMenuBar(mb); // Set up to listen to menu clicks. MenuProcessor mp = new MenuProcessor(this); copyMI.addActionListener(mp); cutMI.addActionListener(mp); pasteMI.addActionListener(mp); // Add Panels to Frame. setLayout(new GridLayout(1,2)); add(sourceTA); add(targetTA); // This makes two equal-sized Panels. setSize(600,400); setVisible(true); // You will need to implement a WindowListener // to be able to easily close the application. } } // End Class definition for EditableGUI.
// you could copy, cut, and paste between a Java GUI and // another program. class MenuProcessor implements ActionListener, ClipboardOwner // Implementing ClipboardOwner is needed for using the Clipboard. { // This class is for ActionEvents on MenuItems. EditableGUI gui; // Enable callbacks to GUI object. public MenuProcessor(EditableGUI g) { gui = g; } public void actionPerformed(ActionEvent e) { if ("Paste".equals(e.getActionCommand())) { Transferable tf = Toolkit.getDefaultToolkit().getSystemClipboard() .getContents(this); // Get the contents of the clipboard known to this // AWT object. try { if (tf != null && tf.isDataFlavorSupported (DataFlavor.stringFlavor)) { gui.targetTA.setBackground(Color.white); gui.targetTA.setForeground(Color.black); gui.targetTA. replaceRange((String)tf.getTransferData (DataFlavor.stringFlavor), gui.targetTA.getSelectionStart(), gui.targetTA.getSelectionEnd()); } } catch (UnsupportedFlavorException ufe) { System.out.println(ufe.getMessage()); } catch (IOException ioe) { ioe.printStackTrace(); } } if ("Copy".equals(e.getActionCommand())) { setClipContents(); // User-defined method }
if ("Cut".equals(e.getActionCommand())) { setClipContents(); gui.sourceTA.replaceRange("", gui.sourceTA.getSelectionStart(), gui.sourceTA.getSelectionEnd()); } } boolean setClipContents() { String selectedText = gui.sourceTA.getSelectedText(); StringSelection sourceContents = new StringSelection(selectedText); Toolkit.getDefaultToolkit().getSystemClipboard(). setContents(sourceContents, this); return true; } public void lostOwnership(Clipboard clip, Transferable transf) { System.out.println("Lost the clipboard ownership"); } } public class EditableDriver { public static void main(String args[]) { EditableGUI gui = new EditableGUI(); } }
Fields
owner contents
public
Methods
Clipboard(String name)
getContents(Object requestor)
A DataFlavor represents the data format of data in the clipboard, during drag and drop, or in a file system. The main flavor currently available is String. See the code in Listing 21.1 for an example of using the DataFlavor class.
Fields
Constructors
public public
DataFlavor(Class representationClass, String humanPresentableName) DataFlavor(String mimeType, String humanPresentableName) DataFlavor(String mimeType, String humanPresentableName, ClassLoader classLoader) throws ClassNotFoundException DataFlavor(String mimeType) throws ClassNotFoundException DataFlavor()
Methods
protected static final Class tryToLoadClass(String className, ClassLoader fallback) throws ClassNotFoundException public String getMimeType() public Class getRepresentationClass() public String getHumanPresentableName() public String getPrimaryType() public String getSubType() public String getParameter(String paramName) public void setHumanPresentableName (String humanPresentableName) public boolean equals(Object o) public boolean equals(DataFlavor dataFlavor) public boolean equals(String s) public boolean isMimeTypeEqual(String mimeType) public final boolean isMimeTypeEqual(DataFlavor dataFlavor) public boolean isMimeTypeSerializedObject() public boolean isRepresentationClassInputStream() public boolean isRepresentationClassSerializable() public boolean isRepresentationClassRemote() public boolean isFlavorSerializedObjectType() public boolean isFlavorRemoteObjectType() public boolean isFlavorJavaFileListType() public void writeExternal(ObjectOutput os) throws IOException public void readExternal(ObjectInput is) throws IOException, ClassNotFoundException public Object clone() throws CloneNotSupportedException protected String normalizeMimeTypeParameter(String parameterName, String parameterValue) protected String normalizeMimeType(String mimeType) Class StringSelection
This class provides methods to transfer a String in plain text format. See Listing 21.1, the Clipboard class, for an example of using the StringSelection class.
Constructors
public
Methods
StringSelection(String data)
getTransferDataFlavors() isDataFlavorSupported(DataFlavor flavor) throws UnsupportedFlavorException, IOException lostOwnership(Clipboard clipboard, Transferable contents)
This class maps native platform data formats to Mime types and their associated Java types.
Methods
FlavorMap
getDefaultFlavorMap() getNativesForFlavors(DataFlavor[] flavors) getFlavorsForNatives(String[] natives) encodeJavaMIMEType(String mimeType) encodeDataFlavor(DataFlavor df) isJavaMIMEType(String atom) decodeJavaMIMEType(String atom) decodeDataFlavor(String atom) throws ClassNotFoundException
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
code copying, cutting, and pasting 2nd 3rd 4th 5th 6th constructors fields methods ClipboardOwner interface methods clipboards data code Clipboard class copying, cutting, pasting 2nd 3rd 4th 5th 6th constructors Clipboard class DataFlavor class StringSelection class copying Clipboard class code 2nd 3rd 4th 5th 6th cutting Clipboard class code 2nd 3rd 4th 5th 6th data clipboards DataFlavor class constructors fields methods 2nd fields Clipboard class DataFlavor class FlavorMap interface methods interfaces ClipboardOwner methods FlavorMap methods Transferable methods java.awt.datatransfer classes Clipboard Clipboard, constructors Clipboard, fields Clipboard, methods DataFlavor DataFlavor, constructors DataFlavor, fields DataFlavor, methods 2nd StringSelection StringSelection, constructors StringSelection, methods 2nd SystemFlavorMap Clipboard class code to copy, cut, and paste 2nd 3rd 4th 5th 6th constructors fields
methods ClipboardOwner interface methods DataFlavor class constructors fields methods 2nd FlavorMap interface methods interfaces ClipboardOwner ClipboardOwner, methods FlavorMap FlavorMap, methods Transferable Transferable, methods StringSelection class constructors methods 2nd SystemFlavorMap class Transferable interface methods methods Clipboard class ClipboardOwner interface DataFlavor class 2nd FlavorMap interface StringSelection class 2nd Transferable interface packages java.awt.datatransfer classes, Clipboard classes, Clipboard, constructors classes, Clipboard, fields classes, Clipboard, methods classes, DataFlavor classes, DataFlavor, constructors classes, DataFlavor, fields classes, DataFlavor, methods 2nd classes, StringSelection classes, StringSelection, constructors classes, StringSelection, methods 2nd classes, SystemFlavorMap Clipboard class Clipboard class, constructors Clipboard class, fields Clipboard class, methods ClipboardOwner interface ClipboardOwner interface, methods DataFlavor class DataFlavor class, constructors DataFlavor class, fields DataFlavor class, methods 2nd FlavorMap interface FlavorMap interface, methods interfaces, ClipboardOwner
interfaces, ClipboardOwner, methods interfaces, FlavorMap interfaces, FlavorMap, methods interfaces, Transferable interfaces, Transferable, methods StringSelection class StringSelection class, constructors StringSelection class, methods 2nd SystemFlavorMap class Transferable interface Transferable interface, methods pasting Clipboard class code 2nd 3rd 4th 5th 6th StringSelection class constructors methods 2nd SystemFlavorMap class Transferable interface methods
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 21. java.awt > java.awt.dnd Interface and Class Details < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019245187063221108143045003
public abstract interface Autoscroll The Autoscroll interface is used by classes that might be drop targets and have scrollable regions that might not be visible.
Methods
Interface DragGestureListener
public abstract interface DragGestureListener extends EventListener This interface should be implemented by any class that wants to receive notification of a drag-and-drop operation being initiated. Chapter 8, "GUI Programming I: Applications," discusses using drag and drop and gives a complete example of using the major interfaces and classes. Note This package has been undergoing significant work during 1999, so if you need to use this facility, you should be sure you are using the latest Java 2 SDK. As of November 1999, this is the Java 2 SDK Standard Edition Version 1.3 beta.
Methods
publicvoid
dragGestureRecognized(DragGestureEvent dge)
Interface DragSourceListener
public abstract interface DragSourceListener extends EventListener This interface is implemented by classes that want to provide feedback to users on the state of a drag-and-drop gesture.
Methods
dragEnter(DragSourceDragEvent dsde) dragOver(DragSourceDragEvent dsde) dropActionChanged(DragSourceDragEvent dsde) dragExit(DragSourceEvent dse) dragDropEnd(DragSourceDropEvent dsde)
Interface DropTargetListener
public abstract interface DropTargetListener extends EventListener This interface is used by classes to provide notification that drop targets have been moved or dropped.
Methods
dragEnter(DropTargetDragEvent dtde) dragOver(DropTargetDragEvent dtde) dropActionChanged(DropTargetDragEvent dtde) dragExit(DropTargetEvent dte) drop(DropTargetDropEvent dtde)
Class DnDConstants
Class DragGestureEvent
A DragGestureEvent is generated by a drag-and-drop gesture and is passed to an object that implements the DragGestureListener interface.
Constructors
public public public public public public public public public public
DragGestureRecognizer Component DragSource Point Iterator Object[] Object[] int InputEvent void
public void
getSourceAsDragRecognizer() getComponent() getDragSource() getDragOrigin() iterator() toArray() toArray(Object[] array) getDragAction() getTriggerEvent() startDrag(Cursor dragCursor, Transferable transferable, DragSourceListener dsl) throws InvalidDnDOperationException startDrag(Cursor dragCursor, Image dragImage, Point imageOffset, Transferable transferable, DragSourceListener dsl) throws InvalidDnD OperationException
Class DragGestureRecognizer
The DragGestureRecognizer class is an abstract class that is subclassed by listener classes that listen for a drag-and-drop gesture's initial moves.
Fields
Constructors
DragGestureRecognizer(DragSource ds, Component c, int sa, DragGestureListener dgl) DragGestureRecognizer(DragSource ds, Component c, int sa) DragGestureRecognizer(DragSource ds, Component c) DragGestureRecognizer(DragSource ds)
protected abstract void protected abstract void public DragSource public Component public void public int public void public InputEvent public void public void
registerListeners() unregisterListeners() getDragSource() getComponent() setComponent(Component c) getSourceActions() setSourceActions(int actions) getTriggerEvent() resetRecognizer() addDragGestureListener (DragGestureListener dgl) throws TooManyListenersException public void removeDragGestureListener (DragGesture Listener dgl) protected void fireDragGesture Recognized(int dragAction, Point p) protected void appendEvent(InputEvent awtie) Class DragSource
Constructors
public
Methods
DragSource()
getDefaultDragSource() isDragImageSupported() startDrag(DragGestureEvent trigger, Cursor dragCursor, Image dragImage, Point imageOffset, Transferable transferable, DragSourceListener dsl, FlavorMap flavorMap) throws InvalidDnDOperationException public void startDrag(DragGestureEvent trigger, Cursor dragCursor, Transferable transferable, DragSourceListener dsl, FlavorMap flavorMap) throws InvalidDnDOperationException public void startDrag(DragGestureEvent trigger, Cursor dragCursor, Image dragImage, Point dragOffset, Transferable transferable, DragSourceListener dsl) throws InvalidDnDOperationException public void startDrag(DragGestureEvent trigger, Cursor dragCursor, Transferable transferable, DragSourceListener dsl) throws InvalidDnDOperationException protected DragSourceContext createDragSourceContext (java.awt.dnd.peer.DragSourceContextPeer dscp, DragGestureEvent dgl, Cursor dragCursor, Image dragImage, Point imageOffset, Transferable t, DragSourceListener dsl) public FlavorMap getFlavorMap() public DragGestureRecognizer createDragGestureRecognizer (Class recognizerAbstractClass, Component c, int actions, DragGestureListener dgl) public DragGestureRecognizer createDefaultDragGestureRecognizer
public
Methods
DragSourceEvent(DragSourceContext dsc)
A DropTarget is a component that has something dropped on it through the completion of a DragGesture.
Constructors
DropTarget(Component c, int ops, DropTargetListener dtl, boolean act, FlavorMap fm) DropTarget(Component c, int ops, DropTargetListener dtl, boolean act) DropTarget() DropTarget(Component c, DropTargetListener dtl) DropTarget(Component c, int ops, DropTargetListener dtl)
public void
public void
public void
setComponent(Component c) getComponent() setDefaultActions(int ops) getDefaultActions() setActive(boolean isActive) isActive() addDropTargetListener (DropTargetListener dtl) throws TooManyListenersException removeDropTargetListener (DropTargetListener dtl) dragEnter (DropTarget DragEvent dtde) dragOver (DropTarget DragEvent dtde) dropActionChanged (DropTargetDragEvent dtde) dragExit(DropTarget Event
public void
public DropTargetContext protected DropTarget Context protected DropTarget.DropTargetAutoScroller protected void protected void protected void Class DropTargetEvent
dte) drop(DropTarget DropEvent dtde) getFlavorMap() setFlavorMap(FlavorMap fm) addNotify (java.awt.peer.ComponentPeer peer) removeNotify (java.awt.peer.ComponentPeer peer) getDropTargetContext() createDropTargetContext() createDropTargetAutoScroller (Component c, Point p) initializeAutoscrolling (Point p) updateAutoscroll (Point dragCursorLocn) clearAutoscroll()
DropTargetEvent is the base class for all DropTargetEvent classes. It encapsulates a drag-and-drop operation, including DropTargetContext.
Fields
public
Methods
DropTargetEvent(DropTargetContext dtc)
public DropTargetContext
getDropTargetContext()
Class MouseDragGestureRecognizer
This class is for use in dealing with mouse-based drag-and-drop gestures through a DragGestureRecognizer.
Constructors
MouseDragGestureRecognizer(DragSource int act, DragGestureListener dgl) MouseDragGestureRecognizer(DragSource int act) MouseDragGestureRecognizer(DragSource MouseDragGestureRecognizer(DragSource
protected void protected void public void public void public void public void public void public void public void
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
DragSource class DragSourceEvent class DropTarget class DropTargetEvent class MouseDragGestureRecognizer class DnDConstants class methods DragGestureEvent class constructors methods 2nd DragGestureListener interface methods DragGestureRecognizer class constructors fields methods 2nd DragSource class constructors fields methods 2nd DragSourceEvent class constructors methods DragSourceListener interface methods DropTarget class constructors methods 2nd DropTargetEvent class constructors fields methods DropTargetListener interface methods fields DragGestureRecognizer class DragSource class DropTargetEvent class interfaces Autoscroll methods DragGestureListener methods DragSourceListener methods DropTargetListener methods java.awt.dnd Autoscroll interface methods classes DnDConstants DnDConstants, methods DragGestureEvent DragGestureEvent, constructors DragGestureEvent, methods 2nd DragGestureRecognizer DragGestureRecognizer, constructors DragGestureRecognizer, fields DragGestureRecognizer, methods 2nd DragSource
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=160 (12 of 15) [6/2/2002 11:00:48 PM]
DragSource, constructors DragSource, fields DragSource, methods 2nd DragSourceEvent DragSourceEvent, constructors DragSourceEvent, methods DropTarget DropTarget, constructors DropTarget, methods 2nd DropTargetEvent DropTargetEvent, constructors DropTargetEvent, fields DropTargetEvent, methods MouseDragGestureRecognizer MouseDragGestureRecognizer, constructors MouseDragGestureRecognizer, methods DnDConstants class methods DragGestureEvent class constructors methods 2nd DragGestureListener interface methods DragGestureRecognizer class constructors fields methods 2nd DragSource class constructors fields methods 2nd DragSourceEvent class constructors methods DragSourceListener interface methods DropTarget class constructors methods 2nd DropTargetEvent class constructors fields methods DropTargetListener interface methods interfaces Autoscroll Autoscroll, methods DragGestureListener DragGestureListener, methods DragSourceListener DragSourceListener, methods DropTargetListener DropTargetListener, methods MouseDragGestureRecognizer class constructors methods methods Autoscroll interface DnDConstants class
DragGestureEvent class 2nd DragGestureListener interface DragGestureRecognizer class 2nd DragSource class 2nd DragSourceEvent class DragSourceListener interface DropTarget class 2nd DropTargetEvent class DropTargetListener interface MouseDragGestureRecognizer class MouseDragGestureRecognizer class constructors methods packages java.awt.dnd Autoscroll interface Autoscroll interface, methods classes, DnDConstants classes, DnDConstants, methods classes, DragGestureEvent classes, DragGestureEvent, constructors classes, DragGestureEvent, methods 2nd classes, DragGestureRecognizer classes, DragGestureRecognizer, constructors classes, DragGestureRecognizer, fields classes, DragGestureRecognizer, methods 2nd classes, DragSource classes, DragSource, constructors classes, DragSource, fields classes, DragSource, methods 2nd classes, DragSourceEvent classes, DragSourceEvent, constructors classes, DragSourceEvent, methods classes, DropTarget classes, DropTarget, constructors classes, DropTarget, methods 2nd classes, DropTargetEvent classes, DropTargetEvent, constructors classes, DropTargetEvent, fields classes, DropTargetEvent, methods classes, MouseDragGestureRecognizer classes, MouseDragGestureRecognizer, constructors classes, MouseDragGestureRecognizer, methods DnDConstants class DnDConstants class, methods DragGestureEvent class DragGestureEvent class, constructors DragGestureEvent class, methods 2nd DragGestureListener interface DragGestureListener interface, methods DragGestureRecognizer class DragGestureRecognizer class, constructors DragGestureRecognizer class, fields DragGestureRecognizer class, methods 2nd DragSource class DragSource class, constructors DragSource class, fields DragSource class, methods 2nd DragSourceEvent class DragSourceEvent class, constructors
DragSourceEvent class, methods DragSourceListener interface DragSourceListener interface, methods DropTarget class DropTarget class, constructors DropTarget class, methods 2nd DropTargetEvent class DropTargetEvent class, constructors DropTargetEvent class, fields DropTargetEvent class, methods DropTargetListener interface DropTargetListener interface, methods interfaces, Autoscroll interfaces, Autoscroll, methods interfaces, DragGestureListener interfaces, DragGestureListener, methods interfaces, DragSourceListener interfaces, DragSourceListener, methods interfaces, DropTargetListener interfaces, DropTargetListener, methods MouseDragGestureRecognizer class MouseDragGestureRecognizer class, constructors MouseDragGestureRecognizer class, methods
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 21. java.awt > java.awt.event Interface and Class Details < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019245188211020005199148239
public abstract interface ActionListener extends EventListener Classes interested in processing ActionEvents must implement this interface. Such listeners are required to detect mouse clicks on Buttons, Menus, and several other objects.
Methods
public void
actionPerformed(ActionEvent e)
The Adjustment interface must be implemented by listeners interested in AdjustmentEvents, which are generated by a user interacting with a scrollbar type of object. Many components, such as List, provide automatic scrolling. The ScrollPane class does automatic scrolling, making the use of the ScrollBar class largely unnecessary. If a program does use a ScrollBar, however, a listener implementing this interface is necessary. Fortunately, there is only one method to implement. Caution In previous versions of the Win32 JDK, the ScrollBar class had many problems. This is another reason to avoid it.
Methods
public void
adjustmentValueChanged(AdjustmentEvent e)
Interface FocusListener
public abstract interface FocusListener extends EventListener This interface should be implemented by listeners interested in keyboard-based focus events. To use this interface, you might do something like what is shown in the following code snippet. In the GUI class:
Canvas can = new Canvas(); Can.addFocusListener(new CanvasFocusListener()); // A Canvas must get the focus before it can be modified by a user. . . . In the event-handling class:
class CanvasFocusListener implements FocusListener { public void focusGained(FocusEvent e) { // Let the user know she can work with the canvas now. } public void focusLost(FocusEvent e) { // Let the user know that she cannot modify the canvas now. } } Tip By itself, a Canvas or Panel cannot get the focus. You need to do something such as use a MouseListener object that can determine when the mouse is clicked on the Canvas or Panel and then call
Methods
focusGained(FocusEvent e) focusLost(FocusEvent e)
Interface InputMethodListener
public abstract interface InputMethodListener extends EventListener The InputMethodListener should be implemented by listeners interested in InputMethod events, such as listeners for text-editing components. Note The InputMethod framework is designed to let you use an English keyboard to enter characters of other alphabets, such as Kanji, which would otherwise need a very specialized keyboard and components.
Methods
Interface ItemListener
public abstract interface ItemListener extends EventListener This interface should be implemented by classes used for listener objects that are interested in ItemEvents. This would include listeners registered on Checkboxes and Lists. The following code snippet shows the use of an ItemListener:
List subjectList = new List(5, false); subjectList.add("History"); subjectList.add("Religion"); subjectList.add("Java Programming"); subjectList.add("Philosophy"); subjectList.add(new SubjectListener()); . . . class SubjectListener implements ItemListener { public void itemStateChanged(ItemEvent ie) { System.out.println(ie.getSource()); } } Tip
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=161 (3 of 40) [6/2/2002 11:01:40 PM]
The itemStateChanged() method is called only when an item in the List component is selected or deselected. If you do not need immediate notification and can wait for all data entry to be completed, you do not need to bother with an ItemListener.
Methods
public void
itemStateChanged(ItemEvent e)
Interface KeyListener
public abstract interface KeyListener extends EventListener The KeyListener interface should be implemented by any listener object interested in keyboard events.
Methods
Interface MouseListener
public abstract interface MouseListener extends EventListener The MouseListener interface is implemented by any objects that are interested in MouseEvents. This ranges from a mouse click event to a MouseEvent that is generated when the mouse enters a Component. Caution Be careful when using this interface to distinguish between a mouse click and a mouse press. If the user presses a mouse button but does not release it, and your MouseListener implementation only concerns itself with mouseClicked(), you will never be notified of user interaction with the mouse. A mouse press is totally different from a click.
Methods
public void
mouseClicked(MouseEvent e)
Interface MouseMotionListener
public abstract interface MouseMotionListener extends EventListener The MouseMotionListener interface is implemented by objects that want to process mouse motion events. This includes moving the mouse within a Component, as well as clicking and dragging the mouse within or outside of a Component. Events specifically related to mouse button events and a mouse entering or leaving a Component are handled by the MouseListener interface.
Methods
mouseDragged(MouseEvent e) mouseMoved(MouseEvent e)
Interface TextListener
public abstract interface TextListener extends EventListener This interface is implemented by listener objects that want to receive text events. A text event occurs when text in a Component, such as a TextField or TextArea, is modified in some way.
Methods
public void
textValueChanged(TextEvent e)
Interface WindowListener
public abstract interface WindowListener extends EventListener This interface is implemented by listener objects that want to be notified of Window-related events, such as a window being minimized or closed. Of the methods here, the most important one to implement is windowClosing(). This method is called primarily when someone clicks the close box (in Win32), chooses the close option (Solaris), or clicks the title bar of a frame or a dialog.
Methods
Class ActionEvent
public class ActionEvent extends AWTEvent An ActionEvent is instantiated whenever a Component-defined action event occurs, which is generally clicking a Button, selecting a MenuItem, or choosing a Checkbox. The following code snippet shows how to deal with Components that generate ActionEvents. In the GUI class:
class ButtonHandler implements ActionListener { GUI gui; public ButtonHandler(GUI g) { gui = g; // Save a reference to GUI for callback // purposes, such as accessing a TextField of // the GUI or calling a method of the GUI class. } public void actionPerformed(ActionEvent e) // Button clicks produce ActionEvents. { // Call tier 2 remote object. String book = gui.titleTF.getText(); catalog.checkOut(book); } }
Fields
Constructors
public public
Methods
ActionEvent(Object source, int id, String command) ActionEvent(Object source, int id, String command, int modifiers)
Class AdjustmentEvent
An AdjustmentEvent object is created when an adjustment event is created, generally by moving a scrollbar.
Fields
Constructors
public
Methods
Class FocusAdapter
This Adapter class is a convenience class for processing focus events. All the methods have empty implementations.
Constructors
public
Methods
FocusAdapter()
focusGained(FocusEvent e) focusLost(FocusEvent e)
public class FocusEvent extends ComponentEvent A FocusEvent is generated when the keyboard focus changes from one Component to another.
Fields
Constructors
public public
FocusEvent(Component source, int id, boolean temporary) FocusEvent(Component source, int id)
Methods
isTemporary() paramString()
public abstract class InputEvent extends ComponentEvent This is the base class for all InputEvent classes. InputEvents are events caught by listeners before the event is passed to the source component for processing. This allows a click on a button to be intercepted and consumed as an InputEvent before the button is activated by the mouse clock on the button.
Fields
Methods
isShiftDown() isControlDown()
Class ItemEvent
This event type is generated when an item is selected or deselected, such as when a Checkbox or an item in a List component is clicked. The event should be processed by a listener that implements the
ItemListener interface. This means that the event will be processed in the itemStateChanged() method of the listener object. Normally you listen for only ItemEvents if you want to be immediately aware of a selection or deselection. Otherwise, it is simpler to interrogate such components all at once, such as when the user clicks the OK button.
Fields
Constructors
public
Methods
Class KeyEvent
This event indicates that a keystroke occurred in a Component. A KeyEvent is generated whenever a key is pressed or released. The key code returned is a virtual key code and might not correspond to the same character on every keyboard. You would listen for KeyEvents both to detect keystrokes to run the program and to detect what is typed within a TextField or another sort of Component. The following code snippet shows using a KeyEvent object passed to a KeyListener method implementation:
// Check key strokes to be sure // the patron id is only numeric. patronID = new JtextField(); patronID.addKeyListener(new KeyChecker()) . . . } } In a KeyListener implementation:
class KeyChecker extends KeyAdapter // While using an adapter class only saves typing // (and typos), it is almost always beneficial to // extend an adapter class rather than implement an // event-listener interface. Note that adapter // classes only exist for event-listener interfaces // that contain multiple methods. There would be // little point in an adapter class for only one method, // since you wouldn't be able to avoid implementing that // one method if you were interested in that event type. { public void keyPressed(KeyEvent e) { displayKey(e); } public void keyReleased(KeyEvent e) { displayKey(e); } public void keyTyped(KeyEvent e) { displayKey(e); } private void displayKey(KeyEvent e) { switch (e.getID()) { // The ID is an int for the event type. case KeyEvent.KEY_TYPED: { System.out.print("The key typed is: " + e.getKeyChar() + "(" + (int)e.getKeyChar() + ")"); break; } case KeyEvent.KEY_PRESSED: { System.out.print("Key pressed: " + e.getKeyChar() + "(" + (int)e.getKeyChar() + ")"); break; }
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=161 (17 of 40) [6/2/2002 11:01:40 PM]
case KeyEvent.KEY_RELEASED: { System.out.print("Key released: " + e.getKeyChar() + "(" + (int)evt.getKeyChar() + ")"); break; } } // End Switch System.out.print ("The key code is: " + e.getKeyCode()); } }
Fields
public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public
static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static
final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final
int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int
KEY_FIRST KEY_LAST KEY_TYPED KEY_PRESSED KEY_RELEASED VK_ENTER VK_BACK_SPACE VK_TAB VK_CANCEL VK_CLEAR VK_SHIFT VK_CONTROL VK_ALT VK_PAUSE VK_CAPS_LOCK VK_ESCAPE VK_SPACE VK_PAGE_UP VK_PAGE_DOWN VK_END VK_HOME VK_LEFT VK_UP VK_RIGHT VK_DOWN VK_COMMA VK_MINUS VK_PERIOD VK_SLASH VK_0 VK_1 VK_2 VK_3 VK_4 VK_5 VK_6
public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public
static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static
final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final
int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int
VK_7 VK_8 VK_9 VK_SEMICOLON VK_EQUALS VK_A VK_B VK_C VK_D VK_E VK_F VK_G VK_H VK_I VK_J VK_K VK_L VK_M VK_N VK_O VK_P VK_Q VK_R VK_S VK_T VK_U VK_V VK_W VK_X VK_Y VK_Z VK_OPEN_BRACKET VK_BACK_SLASH VK_CLOSE_BRACKET VK_NUMPAD0 VK_NUMPAD1 VK_NUMPAD2 VK_NUMPAD3 VK_NUMPAD4 VK_NUMPAD5 VK_NUMPAD6 VK_NUMPAD7 VK_NUMPAD8 VK_NUMPAD9 VK_MULTIPLY VK_ADD VK_SEPARATER VK_SUBTRACT VK_DECIMAL VK_DIVIDE VK_DELETE VK_NUM_LOCK
public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public
static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static
final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final
int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int
VK_SCROLL_LOCK VK_F1 VK_F2 VK_F3 VK_F4 VK_F5 VK_F6 VK_F7 VK_F8 VK_F9 VK_F10 VK_F11 VK_F12 VK_F13 VK_F14 VK_F15 VK_F16 VK_F17 VK_F18 VK_F19 VK_F20 VK_F21 VK_F22 VK_F23 VK_F24 VK_PRINTSCREEN VK_INSERT VK_HELP VK_META VK_BACK_QUOTE VK_QUOTE VK_KP_UP VK_KP_DOWN VK_KP_LEFT VK_KP_RIGHT VK_DEAD_GRAVE VK_DEAD_ACUTE VK_DEAD_CIRCUMFLEX VK_DEAD_TILDE VK_DEAD_MACRON VK_DEAD_BREVE VK_DEAD_ABOVEDOT VK_DEAD_DIAERESIS VK_DEAD_ABOVERING VK_DEAD_DOUBLEACUTE VK_DEAD_CARON VK_DEAD_CEDILLA VK_DEAD_OGONEK VK_DEAD_IOTA VK_DEAD_VOICED_SOUND VK_DEAD_SEMIVOICED_SOUND VK_AMPERSAND
public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public public
static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static
final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final final
int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int int char
VK_ASTERISK VK_QUOTEDBL VK_LESS VK_GREATER VK_BRACELEFT VK_BRACERIGHT VK_AT VK_COLON VK_CIRCUMFLEX VK_DOLLAR VK_EURO_SIGN VK_EXCLAMATION_MARK VK_INVERTED_EXCLAMATION_MARK VK_LEFT_PARENTHESIS VK_NUMBER_SIGN VK_PLUS VK_RIGHT_PARENTHESIS VK_UNDERSCORE VK_FINAL VK_CONVERT VK_NONCONVERT VK_ACCEPT VK_MODECHANGE VK_KANA VK_KANJI VK_ALPHANUMERIC VK_KATAKANA VK_HIRAGANA VK_FULL_WIDTH VK_HALF_WIDTH VK_ROMAN_CHARACTERS VK_ALL_CANDIDATES VK_PREVIOUS_CANDIDATE VK_CODE_INPUT VK_JAPANESE_KATAKANA VK_JAPANESE_HIRAGANA VK_JAPANESE_ROMAN VK_CUT VK_COPY VK_PASTE VK_UNDO VK_AGAIN VK_FIND VK_PROPS VK_STOP VK_COMPOSE VK_ALT_GRAPH VK_UNDEFINED CHAR_UNDEFINED
Constructors
public public
Methods
source, int id, long when, int keyCode, char keyChar) source, int id, long when, int keyCode)
int void void char static String static String boolean String
getKeyCode() setKeyChar(char keyChar) setModifiers(int modifiers) getKeyChar() getKeyText(int keyCode) getKeyModifiersText(int modifiers) IsActionKey() paramString()
Class MouseEvent
A MouseEvent is created for any mouse-related action. It covers events processed by both MouseListeners and MouseMotionListeners. Here's a code snippet that shows how to get some information regarding a mouse click: In the GUI class:
class GUI extends Frame { int mouseX; int mouseY; . . . public void paint(Graphics g) { g.setColor(new Color(mouseX, mouseY, 150)); // Dynamically change the drawing color. fill3DRect(mouseX, mouseY, 20, 15, true); // Draw a 3D rectangle with its upper-left corner // where the mouse was clicked - that is, // 20 by 15 pixels and raised. } } // End GUI class snippet. In the event-handling class:
class MouseTracker extends MouseAdapter { GUI gui; // Callback object reference // to GUI instance, set in // MouseTracker constructor. public void mouseClicked(MouseEvent me) { gui.mouseX = me.getX(); gui.mouseY = me.getY(); } }
Fields
Constructors
public
Methods
MouseEvent(Component source, int id, long when, int modifiers, int x, int y, int clickCount, boolean popupTrigger)
Class PaintEvent
public class PaintEvent extends ComponentEvent A PaintEvent is a special kind of event used with paintUpdate to be sure that paintUpdate calls are delivered properly along with other events from the event queue. It is not normally dealt with by
application programs.
Fields
Constructors
public
Methods
A TextEvent is generated whenever the text in a TextComponent, such as a TextField, is changed in any way. It is processed by an object that implements the TextListener interface. Here is a code snippet to show how you might handle a TextEvent that indicates the text has changed. In the GUI class:
{ gui = g; ) public void textValueChanged(TextEvent e) { String text = gui.bookNotes.getText(); saveNote(text); } public boolean saveNote(String ct) { // Write the String ct to a file. } }
Fields
public static final int public static final int public static final int
Constructors
public
Methods
public String
paramString()
Class WindowAdapter
Like all adapter classes, WindowAdapter is a convenience class. It provides empty implementations of the methods in WindowListener. Although the use of an adapter class is optional, this is the most important of the adapter classes. It is quite unlikely that most programmers will ever deal with most of the WindowListener methods except for windowClosing(), so use of the WindowAdapter class is quite common and helpful. The following code snippet shows how to use the WindowAdapter. An application hardly ever needs to deal with any WindowEvent besides windowClosing, so this is definitely the time to use an adapter. Note You must implement windowClosing() to allow a user to close a window by clicking its close button or selecting Close from the window's system menu.
class GUI extends Frame { public GUI() { addWindowListener(new Terminator()); } } class Terminator extends WindowAdapter { public void windowClosing(WindowEvent e) { // You won't be back. System.exit(0); // Terminate the program and JVM. // Be sure to do any necessary cleanup // first, like closing streams. } }
Constructors
public
Methods
WindowAdapter()
Class WindowEvent
public class WindowEvent extends ComponentEvent A WindowEvent is processed by an object that implements the WindowListener interface. A WindowEvent is generated whenever a window changes its status. For example, a WindowEvent is created when the user clicks the close button on the title bar of a Frame. Iconifying and deiconifying a window are handled in a default way by the JVM. Clicking the close button, although it produces a WindowEvent, does nothing unless a listener object implements the windowClosing() method.
Fields
public static final int public static final int public static final int
Constructors
public
Methods
getWindow() paramString()
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
methods InputEvent constructors fields 2nd methods 2nd ItemEvent KeyEvent code 2nd constructors fields 2nd 3rd 4th 5th 6th methods Mou code constructors fields methods PaintEvent constructors fields methods TextEvent code constructors fields methods WindowAdapter constructors methods WindowEvent constructors fields methods code ActionEvent class FocusListener interface ItemListener interface KeyEvent class 2nd Mou TextEvent class constructors ActionEvent class AdjustmentEvent class FocusAdapter class FocusEvent class InputEvent class KeyEvent class Mou PaintEvent class TextEvent class WindowAdapter class WindowEvent class English keyboards characters of other alphabets fields ActionEvent class AdjustmentEvent class
FocusEvent class InputEvent class 2nd KeyEvent class 2nd 3rd 4th 5th 6th Mou PaintEvent class TextEvent class WindowEvent class FocusAdapter class constructors methods FocusEvent class constructors fields methods FocusListener interface code methods InputEvent class constructors fields 2nd methods 2nd InputMethodListener interface methods interfaces ActionListener methods AdjustmentListener methods FocusListener code methods InputMethodListener methods ItemListener code methods KeyListener methods MouseListener methods MouseMotionListener methods TextListener methods WindowListener methods ItemEvent class ItemListener interface code methods itemStateChanged() method java.awt.event ActionEvent class code constructors fields
methods ActionListener interface methods AdjustmentEvent class constructors fields methods AdjustmentListener interface methods classes ActionEvent ActionEvent, constructors ActionEvent, fields ActionEvent, methods AdjustmentEvent AdjustmentEvent, constructors AdjustmentEvent, fields AdjustmentEvent, methods FocusAdapter FocusAdapter, constructors FocusAdapter, methods FocusEvent FocusEvent, constructors FocusEvent, fields FocusEvent, methods InputEvent InputEvent, constructors InputEvent, fields 2nd InputEvent, methods 2nd ItemEvent KeyEvent KeyEvent, constructors KeyEvent, fields 2nd 3rd 4th 5th 6th KeyEvent, methods Mou 2nd 3rd 4th PaintEvent PaintEvent, constructors PaintEvent, fields PaintEvent, methods TextEvent TextEvent, constructors TextEvent, fields TextEvent, methods WindowAdapter WindowAdapter, constructors WindowAdapter, methods WindowEvent WindowEvent, constructors WindowEvent, fields WindowEvent, methods FocusAdapter class constructors methods FocusEvent class constructors fields
methods FocusListener interface code methods InputEvent class constructors fields 2nd methods 2nd InputMethodListener interface methods interfaces ActionListener ActionListener, methods AdjustmentListener AdjustmentListener, methods FocusListener FocusListener, methods InputMethodListener InputMethodListener, methods ItemListener ItemListener, methods KeyListener KeyListener, methods MouseListener MouseListener, methods MouseMotionListener MouseMotionListener, methods TextListener TextListener, methods WindowListener WindowListener, methods ItemEvent class ItemListener interface code methods KeyEvent class code 2nd constructors fields 2nd 3rd 4th 5th 6th methods KeyListener interface methods Mou code constructors fields methods MouseListener interface methods MouseMotionListener interface methods PaintEvent class constructors fields methods TextEvent class
code constructors fields methods TextListener interface methods WindowAdapter class constructors methods WindowEvent class constructors fields methods WindowListener interface methods keyboards English characters of other alphabets KeyEvent class code 2nd constructors fields 2nd 3rd 4th 5th 6th methods KeyListener interface methods methods ActionEvent class ActionListener interface AdjustmentEvent class AdjustmentListener interface FocusAdapter class FocusEvent class FocusListener interface InputEvent class 2nd InputMethodListener interface ItemListener interface itemStateChanged() KeyEvent class KeyListener interface Mou MouseListener interface MouseMotionListener interface PaintEvent class TextEvent class TextListener interface WindowAdapter class windowClosing() 2nd WindowEvent class WindowListener interface Mou code constructors fields methods mouse clicks and presses distinguishing
MouseListener interface methods MouseMotionListener interface methods packages java.awt.event ActionEvent class ActionEvent class, constructors ActionEvent class, fields ActionEvent class, methods ActionListener interface ActionListener interface, methods AdjustmentEvent class AdjustmentEvent class, constructors AdjustmentEvent class, fields AdjustmentEvent class, methods AdjustmentListener interface AdjustmentListener interface, methods classes, ActionEvent classes, ActionEvent, constructors classes, ActionEvent, fields classes, ActionEvent, methods classes, AdjustmentEvent classes, AdjustmentEvent, constructors classes, AdjustmentEvent, fields classes, AdjustmentEvent, methods classes, FocusAdapter classes, FocusAdapter, constructors classes, FocusAdapter, methods classes, FocusEvent classes, FocusEvent, constructors classes, FocusEvent, fields classes, FocusEvent, methods classes, InputEvent classes, InputEvent, constructors classes, InputEvent, fields 2nd classes, InputEvent, methods 2nd classes, ItemEvent classes, KeyEvent classes, KeyEvent, constructors classes, KeyEvent, fields 2nd 3rd 4th 5th 6th classes, KeyEvent, methods classes, Mou 2nd 3rd 4th classes, PaintEvent classes, PaintEvent, constructors classes, PaintEvent, fields classes, PaintEvent, methods classes, TextEvent classes, TextEvent, constructors classes, TextEvent, fields classes, TextEvent, methods classes, WindowAdapter classes, WindowAdapter, constructors classes, WindowAdapter, methods classes, WindowEvent classes, WindowEvent, constructors
classes, WindowEvent, fields classes, WindowEvent, methods FocusAdapter class FocusAdapter class, constructors FocusAdapter class, methods FocusEvent class FocusEvent class, constructors FocusEvent class, fields FocusEvent class, methods FocusListener interface FocusListener interface, methods InputEvent class InputEvent class, constructors InputEvent class, fields 2nd InputEvent class, methods 2nd InputMethodListener interface InputMethodListener interface, methods interfaces, ActionListener interfaces, ActionListener, methods interfaces, AdjustmentListener interfaces, AdjustmentListener, methods interfaces, FocusListener interfaces, FocusListener, methods interfaces, InputMethodListener interfaces, InputMethodListener, methods interfaces, ItemListener interfaces, ItemListener, methods interfaces, KeyListener interfaces, KeyListener, methods interfaces, MouseListener interfaces, MouseListener, methods interfaces, MouseMotionListener interfaces, MouseMotionListener, methods interfaces, TextListener interfaces, TextListener, methods interfaces, WindowListener interfaces, WindowListener, methods ItemEvent class ItemListener interface ItemListener interface, methods KeyEvent class KeyEvent class, constructors KeyEvent class, fields 2nd 3rd 4th 5th 6th KeyEvent class, methods KeyListener interface KeyListener interface, methods Mou 2nd 3rd 4th MouseListener interface MouseListener interface, methods MouseMotionListener interface MouseMotionListener interface, methods PaintEvent class PaintEvent class, constructors PaintEvent class, fields PaintEvent class, methods TextEvent class
TextEvent class, constructors TextEvent class, fields TextEvent class, methods TextListener interface TextListener interface, methods WindowAdapter class WindowAdapter class, constructors WindowAdapter class, methods WindowEvent class WindowEvent class, constructors WindowEvent class, fields WindowEvent class, methods WindowListener interface WindowListener interface, methods PaintEvent class constructors fields methods TextEvent class code constructors fields methods TextListener interface methods WindowAdapter class constructors methods windowClosing() method 2nd WindowEvent class constructors fields methods WindowListener interface methods
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 21. java.awt > java.awt.print Interface and Class Details < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019245188211022214046128187
public abstract interface Pageable A Pageable object represents a set of pages. It also can provide page format and printable information on a specific page in the set of pages that the Pageable object represents. Prior to Java 2, there was no way to readily associate multiple pages.
Fields
UNKNOWN_NUMBER_OF_PAGES
public abstract interface Printable An object that implements Printable is used along with a PageFormat object to provide information to a printer to allow it to print a graphics image.
Fields
PAGE_EXISTS NO_SUCH_PAGE
public int
Interface PrinterGraphics
public abstract interface PrinterGraphics This interface is implemented by Printable objects to provide a means to get a PrinterJob to print the target object. The PrinterJob provides a dialog to query the user regarding the page size, format, and printer to be used (in a platform-specific format).
Methods
getPrinterJob()
A Book object represents a collection of pages. These pages can be numbered and printed as a whole.
Constructors
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=162 (2 of 8) [6/2/2002 11:01:51 PM]
public
Methods
Book()
public int getNumberOfPages() public PageFormat getPageFormat(int pageIndex) throws IndexOutOfBoundsException public Printable getPrintable(int pageIndex) throws IndexOutOfBoundsException public void setPage(int pageIndex, Printable painter, PageFormat page) throws IndexOutOfBoundsException public void append(Printable painter, PageFormat page) public void append(Printable painter, PageFormat page, int numPages) Class PageFormat
public class
The format of a page, its size, and its orientation for printing, such as Landscape, is specified through a pageFormat object.
Fields
LANDSCAPE PORTRAIT
REVERSE_LANDSCAPE
public
Methods
PageFormat()
clone() getWidth() getImageableX() getImageableY() getImageableWidth() getImageableHeight() getPaper() getPaper(Paper paper) setOrientation(int orientation) throws IllegalArgumentException getOrientation() getMatrix()
The PrinterJob class, which replaces the JDK 1.1 PrintJob class, is used by an application to control printing. It can optionally (and usually does) display a print dialog appropriate to the platform. It provides methods to control printing. The following code snippet shows the basic flow in an ActionEvent listener class for printingthat is, in response to the user's choosing a menu item for printing:
public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof JButton) { PrinterJob pj = PrinterJob.getPrinterJob(); // In order to print, you need a PrinterJob. // This logically connects your program to the printer. PageFormat pf = pj.pageDialog(pj.defaultPage()); // Show a dialog that allows the user to format the page. pj.setPrintable(gui); if (pj.printDialog()) // Display a print dialog. { try { pj.print(); } catch (Exception e) { e.printStackTrace(); } } }
Constructors
public PrinterJob()
Methods
public static PrinterJob public abstract void public abstract void public abstract void public public public public public public public public public public abstract boolean abstract PageFormat abstract PageFormat PageFormat abstract PageFormat abstract void abstract abstract abstract abstract void int String void
getPrinterJob() setPrintable(Printable painter) setPrintable(Printable painter, PageFormat format) setPageable(Pageable document) throws NullPointerException printDialog() pageDialog(PageFormat page) defaultPage(PageFormat page) defaultPage() validatePage(PageFormat page) print() throws PrinterException setCopies(int copies) getCopies() getUserName() setJobName(String jobName)
PageFormat, constructors PageFormat, fields PageFormat, methods 2nd PrinterJob PrinterJob, constructors PrinterJob, methods 2nd interfaces Pageable Pageable, fields Pageable, methods Printable Printable, fields Printable, methods PrinterGraphics PrinterGraphics, methods Pageable interface fields methods PageFormat class constructors fields methods 2nd Printable interface fields methods PrinterGraphics interface methods PrinterJob class constructors methods 2nd methods Book class Pageable interface PageFormat class 2nd Printable interface PrinterGraphics interface PrinterJob class 2nd packages java.awt.print Book class Book class, constructors Book class, methods classes, Book classes, Book, constructors classes, Book, methods classes, PageFormat classes, PageFormat, constructors classes, PageFormat, fields classes, PageFormat, methods 2nd classes, PrinterJob classes, PrinterJob, constructors classes, PrinterJob, methods 2nd interfaces, Pageable interfaces, Pageable, fields interfaces, Pageable, methods interfaces, Printable
interfaces, Printable, fields interfaces, Printable, methods interfaces, PrinterGraphics interfaces, PrinterGraphics, methods Pageable interface Pageable interface, fields Pageable interface, methods PageFormat class PageFormat class, constructors PageFormat class, fields PageFormat class, methods 2nd Printable interface Printable interface, fields Printable interface, methods PrinterGraphics interface PrinterGraphics interface, methods PrinterJob class PrinterJob class, constructors PrinterJob class, methods 2nd Pageable interface fields methods PageFormat class constructors fields methods 2nd Printable interface fields methods PrinterGraphics interface methods PrinterJob class code 2nd constructors methods 2nd
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019245189074125128186243212
public abc getabc() public void setabc(some value) In JDK 1.1, all the AWT components are made JavaBean-compliant in this way, and that might explain why many methods with names that do not match this pattern were deprecated. Every property must have a get() method and a set() method that corresponds to the name of the property.
The developer of a JavaBean might also decide that the property sheet created by the IDE is insufficient and provide a special dialog for modifying properties. The IDE will then display this custom dialog instead of the default property sheet that it would have displayed based on introspection. This all is performed at design time. At runtime, the properties have already been set, and users are not generally given the opportunity to interact with a JavaBean's properties in this way. The introspection process just mentioned is based on the Reflection API. Under the covers, the introspection process interrogates a bean for its fields and methods. Again, the JavaBean specification requires that, for every property that can be accessed or modified externally, there must be a get() and set() method, except in the case of a boolean value, which can have an isabc() method that checks whether the property is set to true. The introspection mechanism assumes that a given bean follows this convention and will use getabc() and setabc() to manipulate the abc property of the JavaBean. The following provides a quick view of the interfaces and classes in the java.beans package.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
properties get() method set() method JavaBean properties java.beans JavaBeans abc property methods client-side components IDE (Integrated Development Environment) introspection Isabc() method JDK 1.1 AWT components methods Isabac() properties introspection purpose Reflection API javax.ejb packages JDK 1.1 AWT components methods abc property get() getabc() Isabac() set() setabc() packages java.beans 2nd javax.ejb properties abc methods beans get() method Isabc() method JavaBeams introspection JavaBeans introspection java.beans methods get() Isabac() set() set() method Reflection API JavaBeams set() method setabc() method
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 22. java.beans > Package Name: java.beans < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019245189074124165145225233
BeanDescriptor Beans EventSetDescriptor FeatureDescriptor IndexedPropertyDescriptor Introspector MethodDescriptor ParameterDescriptor PropertyChangeEvent PropertyChangeSupport PropertyDescriptor PropertyEditorManager PropertyEditorSupport SimpleBeanInfo VetoableChangeSupport Exceptions
IntrospectionException PropertyVetoException
All Books
Search
Java > PURE Java 2 > 22. java.beans > Package Name: java.beans.beancontext < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019245190247091171244093118
BeanContext BeanContextChild BeanContextChildComponentProxy BeanContextContainerProxy BeanContextMembershipListener BeanContextProxy BeanContextServiceProvider BeanContextServiceProviderBeanInfo BeanContextServiceRevokedListener BeanContextServices BeanContextServicesListener Classes
All Books
Search
Java > PURE Java 2 > 22. java.beans > java.beans Interface and Class Details < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019245190247091174149085164
public abstract interface AppletInitializer The AppletInitializer interface provides the mechanism for initializing a JavaBean, which is also an applet, when the bean is instantiated by java.beans.Beans.instantiate().
Methods
public abstract interface BeanInfo This interface is implemented by Bean providers to supply basic information about a Bean, including an icon for the Bean to place in a toolbar or similar Component.
Fields
getIcon(int iconKind)
public abstract interface Customizer This interface is implemented by a class so that the class can serve as a custom GUI to set the properties of a Bean. The implementing class should have a null constructor and inherit from java.awt.Component so that it can be added to a Container, like a Dialog.
Methods
Interface PropertyChangeListener
public abstract interface PropertyChangeListener extends EventListener This interface is implemented by objects that want to be listeners for changes to bound Bean properties. A bound property is a property that, when changes occur, causes notification to be sent to interested parties. The class PropertyChangeSupport serves as a convenience class to provide methods to add and remove PropertyChangeListener objects, and code to fire PropertyChangeEvents to those listener objects when the bound property is changed. A JavaBean can inherit from this convenience class or use it as an inner class. To implement a bound property, your bean needs to
q q
Implement methods to add and remove PropertyChangeListener objects. Because PropertyChangeSupport provides these, your methods simply wrap them. Modify the set() method for the bound property to call firePropertyChange() so that a PropertyChangeEvent is sent to registered PropertyChangeListener objects for the change. The values you supply to the firePropertyChange() method are the old and new values of the property. These are put into a PropertyChangeEvent, which is then used as the parameter for calling propertyChange() (done for you). This passes the PropertyChangeEvent to each registered listener. This method is the one method in the PropertyChangeListener interface. Implement the propertyChange() method in the listener class. Do whatever is appropriate for a change to the bound property.
public void
Interface PropertyEditor
public abstract interface PropertyEditor This interface is implemented by a Bean developer so that users can edit Bean properties. It supports multiple ways to display and edit those properties, although typically only a subset of those possibilities will be used by one PropertyEditor.
Methods
public public public public public public public public public public public
void Object boolean void String String void String[] Component boolean void
public void
setValue (Object value) getValue() isPaintable() paintValue(Graphics gfx, Rectangle box) getJavaInitializationString() getAsText() setAsText(String text) throws IllegalArgumentException getTags() getCustomEditor() supportsCustomEditor() addPropertyChangeListener (PropertyChangeListener listener) removePropertyChangeListener (PropertyChangeListener listener)
Interface VetoableChangeListener
public abstract interface VetoableChangeListener extends EventListener By default, there are no restrictions outside a Bean regarding its properties. It is possible, however, to use another object to constrain property changes in a Bean by giving another object the power to veto a change to a Bean's property. To do this, the other object has to implement this interface so that it listens for VetoableChanges to a Bean.
Methods
public void
Class BeanDescriptor
A BeanDescriptor object is returned by a BeanInfo object to provide global information about a Bean, such as the Bean's classname. A BeanDescriptor returns descriptors for properties, methods, and events. This pro vides an equivalent, more powerful version of Reflection. You can use it to learn about the JavaBean. You can use this information in your program. Builder tools use this information for creating property sheets and the like in an IDE.
Constructors
public public
Methods
getBeanClass() getCustomizerClass()
This class provides methods for general-purpose creation and manipulation of a Bean.
Constructors
public
Methods
Beans()
instantiate (ClassLoader cls, String beanName) throws IOException, ClassNotFoundException instantiate(ClassLoader cls, String beanName, BeanContext beanContext) throws IOException, ClassNotFoundException instantiate(ClassLoader cls, String beanName, BeanContext beanContext, AppletInitializer initializer) throws IOException, ClassNotFoundException getInstanceOf(Object bean, Class targetType) isInstanceOf(Object bean, Class targetType) isDesignTime() isGuiAvailable() setDesignTime(boolean isDesignTime) throws SecurityException setGuiAvailable(boolean isGuiAvailable) throws SecurityException
Class IndexedPropertyDescriptor
A Bean may have a property that is in the form of an array of property values. This class provides methods for accessing specific values of that property array.
Constructors
public public
public
IndexedPropertyDescriptor (String propertyName, ClassbeanClass) throws IntrospectionException IndexedPropertyDescriptor(String propertyName, Class beanClass, String getterName, String setterName, String indexedGetterName, String indexedSetterName) throws IntrospectionException IndexedPropertyDescriptor(String propertyName, Method getter, Method setter, Method indexedGetter, Method indexedSetter) throws IntrospectionException
Methods
public Method public void public Method public void public Class Class Introspector
getIndexedReadMethod() setIndexedReadMethod(Method getter) throws IntrospectionException getIndexedWriteMethod() setIndexedWriteMethod(Method setter) throws IntrospectionException getIndexedPropertyType()
This class is used to provide tools with information about the properties, events, and methods of a JavaBean. The Introspection class can be used in place of Reflection, even with a class that does not represent a JavaBean. It is much easier to use than Reflection, so if you need to interrogate an object or class, consider using this instead of the Reflection APIs in this way:
public static final int public static final int public static final int
Methods
getBeanInfo (Class beanClass) throws IntrospectionException getBeanInfo(Class beanClass, int flags) throws IntrospectionException
public static BeanInfo public public public public public static static static static static String String[] void void void
getBeanInfo(Class beanClass, Class stopClass) throws IntrospectionException decapitalize(String name) getBeanInfoSearchPath() setBeanInfoSearchPath(String[] path) flushCaches() flushFromCaches(Class clz)
Class PropertyChangeEvent
public class PropertyChangeEvent extends EventObject Whenever a bound or constrained property changes, a PropertyChangeEvent is fired. To process this event, an object must implement the PropertyChangeListener interface or the VetoableChangeListener.
Constructors
public
Methods
getPropertyName() getNewValue()
Class PropertyDescriptor
A PropertyDescriptor is an object that is used to describe a JavaBean property so that the property can be interpreted and manipulated.
Constructors
public public
public
PropertyDescriptor (String propertyName, Class beanClass) throws IntrospectionException PropertyDescriptor(String propertyName, Class beanClass, String getterName, String setterName) throws IntrospectionException PropertyDescriptor(String propertyName, Method getter, Method setter) throws IntrospectionException
Methods
public Class public Method public void public Method public void public public public public public public boolean void boolean void void Class
getPropertyType() getReadMethod() setReadMethod(Method getter) throws IntrospectionException getWriteMethod() setWriteMethod(Method setter) throws IntrospectionException isBound() setBound(boolean bound) isConstrained() setConstrained(boolean constrained) setPropertyEditorClass(Class propertyEditorClass) getPropertyEditorClass()
Class SimpleBeanInfo
This is a basic class for a Bean to use to provide information through the methods specified in the BeanInfo interface. The methods by default return a hoop value, so any method that a developer wants to use must be overridden in a subclass. When the introspector sees the hoop value, it automatically performs low-level introspection to ana lyze the target bean.
Constructors
public SimpleBeanInfo()
Methods
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
Introspector, methods PropertyChangeEvent PropertyChangeEvent, constructors PropertyChangeEvent, methods PropertyDescriptor PropertyDescriptor, constructors PropertyDescriptor, methods SimpleBeanInfo SimpleBeanInfo, constructors SimpleBeanInfo, methods PropertyChangeEvent 2nd constructors methods PropertyChangeEvents PropertyChangeListener PropertyChangeSupport PropertyDescriptor constructors methods SimpleBeanInfo constructors methods constructors BeanDescriptor class Beans class IndexedPropertyDescriptor class 2nd PropertyChangeEvent class PropertyDescriptor class SimpleBeanInfo class Customizer interface methods fields BeanInfo interface Introspector class hoop values methods IndexedPropertyDescriptor class constructors 2nd methods interfaces AppletInitializer methods BeanInfo fields methods Customizer methods java.beans AppletInitializer AppletInitializer, methods BeanInfo BeanInfo, fields BeanInfo, methods Customizer Customizer, methods PropertyChangeListener PropertyChangeListener, methods PropertyEditor PropertyEditor, methods 2nd
VetoableChangeListener VetoableChangeListener, methods PropertyChangeListener methods PropertyEditor methods 2nd VetoableChangeListener methods Introspection class Reflection Introspector class fields methods java.bean Bean class BeanDescriptor classes Bean hoop values methods Introspection class Reflection methods hoop values PropertyChangeEvent class Reflection Introspection class java.beans AppletInitializer interface methods BeanDescriptor class constructors methods BeanInfo interface fields methods Beans global information, providing Beans class constructors methods 2nd bound properties implementing 2nd classes BeanDescriptor BeanDescriptor, constructors BeanDescriptor, methods Beans Beans, constructors Beans, methods 2nd IndexedPropertyDescriptor IndexedPropertyDescriptor, constructors 2nd IndexedPropertyDescriptor, methods Introspector Introspector, fields Introspector, methods PropertyChangeEvent PropertyChangeEvent, constructors PropertyChangeEvent, methods PropertyDescriptor
PropertyDescriptor, constructors PropertyDescriptor, methods SimpleBeanInfo SimpleBeanInfo, constructors SimpleBeanInfo, methods constructors BeanDescriptor class Beans class IndexedPropertyDescriptor class 2nd PropertyChangeEvent class PropertyDescriptor class SimpleBeanInfo class Customizer interface methods fields BeanInfo interface Introspector class IndexedPropertyDescriptor class constructors 2nd methods interfaces AppletInitializer AppletInitializer, methods BeanInfo BeanInfo, fields BeanInfo, methods Customizer Customizer, methods PropertyChangeListener PropertyChangeListener, methods PropertyEditor PropertyEditor, methods 2nd VetoableChangeListener VetoableChangeListener, methods Introspector class fields methods methods AppletInitializer interface BeanDescriptor class BeanInfo interface Beans class 2nd Customizer interface IndexedPropertyDescriptor class Introspector class PropertyChangeEvent class PropertyChangeListener interface PropertyDescriptor class PropertyEditor interface 2nd SimpleBeanInfo class VetoableChangeListener interface properties bound;implementing 2nd PropertyChangeEvent class constructors methods PropertyChangeListener interface methods PropertyDescriptor class
constructors methods PropertyEditor interface methods 2nd SimpleBeanInfo class constructors methods VetoableChangeListener interface methods java.beans.Beans.instantiate() methods AppletInitializer interface BeanDescriptor class BeanInfo interface Beans class 2nd Customizer interface hoop values IndexedPropertyDescriptor class Introspector class PropertyChangeEvent class PropertyChangeListener interface PropertyDescriptor class PropertyEditor interface 2nd SimpleBeanInfo class VetoableChangeListener interface packages java.beans AppletInitializer interface AppletInitializer interface, methods BeanDescriptor class BeanDescriptor class, constructors BeanDescriptor class, methods BeanInfo interface BeanInfo interface, fields BeanInfo interface, methods Beans class Beans class, constructors Beans class, methods 2nd Customizer interface Customizer interface, methods IndexedPropertyDescriptor class IndexedPropertyDescriptor class, constructors 2nd IndexedPropertyDescriptor class, methods Introspector class Introspector class, fields Introspector class, methods PropertyChangeEvent class PropertyChangeEvent class, constructors PropertyChangeEvent class, methods PropertyChangeListener interface PropertyChangeListener interface, methods PropertyDescriptor class PropertyDescriptor class, constructors PropertyDescriptor class, methods PropertyEditor interface PropertyEditor interface, methods 2nd SimpleBeanInfo class SimpleBeanInfo class, constructors SimpleBeanInfo class, methods
VetoableChangeListener interface VetoableChangeListener interface, methods properties bound implementing 2nd PropertyChangeEvent class 2nd constructors methods PropertyChangeEvents class PropertyChangeListener class PropertyChangeListener interface methods PropertyChangeSupport class PropertyDescriptor class constructors methods PropertyEditor interface methods 2nd Reflection Introspection class SimpleBeanInfo class constructors methods VetoableChangeListener interface methods
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019245190247089186115153254
All Books
Search
Java > PURE Java 2 > 23. java.io > Package Name: java.io < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019245190247089183035028031
DataInput DataOutput Externalizable FileFilter FilenameFilter ObjectInput ObjectInputValidation ObjectOutput ObjectStreamConstants Serializable Classes
CharArrayReader CharArrayWriter DataInputStream DataOutputStream File FileDescriptor FileInputStream FileOutputStream FilePermission FileReader FileWriter FilterInputStream FilterOutputStream FilterReader FilterWriter InputStream InputStreamReader LineNumberInputStream LineNumberReader ObjectInputStream ObjectInputStream.GetField ObjectOutputStream ObjectOutputStream.PutField ObjectStreamClass ObjectStreamField OutputStream OutputStreamWriter PipedInputStream PipedOutputStream PipedReader PipedWriter PrintStream PrintWriter PushbackInputStream PushbackReader RandomAccessFile Reader SequenceInputStream SerializablePermission StreamTokenizer StringBufferInputStream StringReader StringWriter Writer Exceptions
InvalidClassException InvalidObjectException IOException NotActiveException NotSerializableException ObjectStreamException OptionalDataException StreamCorruptedException SyncFailedException UnsupportedEncodingException UTFDataFormatException WriteAbortedException
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 23. java.io > Interface and Class Details < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019245190247088048186017064
public abstract interface Externalizable extends Serializable The Externalizable interface is implemented by classes that want to have complete control over the process of serialization. Under this scenario, only the class name is serialized by the JVM. Everything else has to be done by the object itself. Note Before deciding to use this interface, developers should consider overriding readObject and writeObject while still using normal serialization. Making an Externalizable class is a very complex process.
Methods
writeExternal (ObjectOutput out) throws IOException readExternal(ObjectInput in) throws IOException, ClassNotFoundException
public abstract interface FileFilter An object implementing the FileFilter interface uses its methods to select the type of files to display in a FileDialog. Note This hasn't worked in previous versions of Sun's JVM in Win32 environments. You couldn't filter files correctly.
Methods
public boolean
Interface FilenameFilter
The interface FilenameFilter is implemented by objects to filter files based on filenames. The interface is
public boolean
Interface Serializable
public abstract interface Serializable Object serialization is a key feature in Java, used by RMI and other mechanisms in Java. It is the process of converting an object into a format that can be sent over a network and converted back into an object at the other end of the connection. The serialized object is passed as a byte array. This conversion process is normally performed by the JVM without any coding required. The only exception occurs when a field should not be copied. In this case, the field can be marked as transient. This will cause Java to put a bookmark in the serialized object where the field would have been. Similarly, static fields are not copied. The serialization mechanism is intelligent enough to make only one copy of a duplicated object in the serialized form. The Serializable interface is easy to implement in a class, as it has no methods. Therefore, a class signature that says it "implements Serializable" is essentially signaling the JVM that the object is eligible for serialization. Not all objects can be serialized. If they are not defined as implementing Serializable, such as the class Thread, attempting to write them as an object will fail.
Fields
serialVersionUID
A BufferedInputStream provides a wrapper around a byte-level stream such as a FileInputStream. It allows for much more efficient I/O by permitting you to buffer the input. Tip You should always use buffering when doing I/O in Java, no matter what class you are using. Reading or writing data one byte or one character at a time is grossly inefficient. You should always try to do I/O in large chunks. The optimal size of the chunk may vary from operating system to operating system, but a page (1024 or 2048 bytes) would not be a bad choice in most situations.
Fields
Constructors
public public
Methods
public int public int public long public int public void public void public boolean public void
read() throws IOException read(byte[] b, int off, int len) throws IOException skip(long n) throws IOException available() throws IOException mark(int readlimit) reset() throws IOException markSupported() close() throws IOException
Class BufferedOutputStream
This class provides a stream to wrap a byte-oriented output stream, such as FileOutputStream, so that an object can perform buffered IO, which is much more efficient than a non-buffered IO operation. Buffering data can significantly improve application performance in Java. The same caveat applies here as with BufferedInputStream. It is vital that you buffer output as well as input.
Fields
buf count
BufferedOutputStream(OutputStream out) BufferedOutputStream(OutputStream out, int size) write(int b) throws IOException write(byte[] b, Int off, int len)
public void
Class BufferedReader
The BufferedReader provides the same facilities that a BufferedInputStream does, except that as a Reader subclass, it performs buffered input operations on Unicode characters, not merely bytes. One could use this class's readLine() method to read a line of input from the command-line as a Unicode-based String object. Here's what you would do:
InputStreamReader isr = new InputStreamReader(System.in); // Convert from byte stream class to Reader class. BufferedReader br = new BufferedReader(isr); String input = br.readLine();
Constructors
read() throws IOException public int read(char[] cbuf, int off, int len) throws IOException public String readLine() throws IOException public long skip(long n) throws IOException public boolean ready() throws IOException public boolean markSupported() public void mark(int readAheadLimit) throws IOException public void reset() throws IOException public void close() throws IOException
Class BufferedWriter
public int
The BufferedWriter class provides for writing buffered output, as does the BufferedOutputStream, but has the additional benefit of writing data as Unicode characters, reflecting Java's internal character representation. Again, buffering data for I/O is critical for application performance.
Constructors
public public
Methods
public void public void public void public void public void public void
write(int c) throws IOException write(char[] cbuf, int off, int len) throws IOException write(String s, int off, int len) throws IOException newLine() throws IOException flush() throws IOException close() throws IOException
Class ByteArrayInputStream
A ByteArrayInputStream is a stream which holds data in the form of a byte array. A very useful application of this is for a ByteArrayOutputStream to be used for concatenating multiple, disparate data elements into one contiguous byte array, which can then be retrieved with a ByteArrayInputStream. This stream can be
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=171 (8 of 66) [6/2/2002 11:04:22 PM]
public public
Methods
available() read() read(byte[] b, int off, int len) skip(long n) mark(int readAheadLimit) markSupported() reset() close() throws IOException
Class ByteArrayOutputStream
A ByteArrayOutputStream stores data in an array of bytes internally. It can be used to store disparate data elements as a byte array that can be retrieved from a ByteArrayInputStream. These two streams can be used in concert in memory to concatenate disparate data elements together in a ByteArrayOutputStream and then to read them back as a byte array or string using a ByteArrayInputStream.
Fields
buf count
public public
Methods
public void public void public void public public public public void byte[] int String
write(int b) write(byte[] b, int off, int len) writeTo(OutputStream out) throws IOException reset() toByteArray() size() toString()
Class CharArrayReader
This class can read an array of Unicode characters, generally those created with a CharArrayWriter. It is similar to a ByteArrayInputStream except that a CharArrayReader functions at the level of characters, not bytes.
Fields
public public
Methods
read() throws IOException public int read(char[] b, int off, int len) throws IOException public long skip(long n) throws IOException public boolean ready() throws IOException public boolean markSupported() public void mark(int readAheadLimit) throws IOException public void reset() throws IOException public void close()
Class CharArrayWriter
public int
This class is for writing an array of Unicode characters. It can be used to create an array of characters to be read by a CharArrayReader.
Fields
buf count
public public
Methods
public public public public public public public public public public
void void void void void char[] int String void void
write(int c) write(char[] c, int off, int len) write(String str, int off, int len) writeTo(Writer out) throws IOException reset() toCharArray() size() toString() flush() close()
Class DataInputStream
A DataInputStream is a byte-oriented stream that can read Java primitive data types. It can also be used to send any string as a UTF string. It is very common to wrap a more primitive stream in a DataInputStream for reading from a stream. This class can also be used to read unsigned numeric data into Java ints, which are, of course, signed. You use a DataInputStream by "wrapping" an InputStream. This is a very common technique in Java for doing I/O. Here's a brief example:
FileInputStream fis = new FileInputStream("Books.txt"); DataInputStream dis = new DataInputStream(fis); String s = dis.readUTF();
Constructors
public
Methods
DataInputStream(InputStream in)
public final int public final int public final void public final void public final int public final boolean public final byte public final int public final short public final int public final char public final int public final long public final float public final double public final String public final String public static final String
Class DataOutputStream
read(byte[] b) throws IOException read(byte[] b, int off, int len) throws IOException readFully(byte[] b) throws IOException readFully(byte[] b, int off, int len) throws IOException skipBytes(int n) throws IOException readBoolean() throws IOException readByte() throws IOException readUnsignedByte() throws IOException readShort() throws IOException readUnsignedShort() throws IOException readChar() throws IOException readInt() throws IOException readLong() throws IOException readFloat() throws IOException readDouble() throws IOException readLine() throws IOException readUTF() throws IOException readUTF(DataInput in) throws IOException
The DataOutputStream class is used to write Java primitives to a stream, such as char, boolean, and long, rather than having to put separate bytes together. You use a DataOutputStream by wrapping it around a more basic OutputStream. Here's an example:
protected int
Constructors
written
Methods
public void public void public void public final void public final void public final void public final void public final void public final void public final void public final void public final void public final void public final void public final int
Class File
write(int b) throws IOException write(byte[] b, int off, int len) throws IOException flush() throws IOException writeBoolean(boolean v) throws IOException writeByte(int v) throws IOException writeShort(int v) throws IOException writeChar(int v) throws IOException writeInt(int v) throws IOException writeLong(long v) throws IOException writeFloat(float v) throws IOException writeDouble(double v) throws IOException writeBytes(String s) throws IOException writeChars(String s) throws IOException writeUTF(String str) throws IOException size()
The class File represents a File object. It does not represent a physical file, though it may be associated with a physical file or a directory. That is, it is perfectly possible to call a File constructor to make an instance of a File object that contains the name of a non-existent file. This will not cause an error when the File object is created. It will only cause an error when a program attempts to use the File object to read the non-existent file. Unlike file handling in other languages such as C, a file represented by a File object is not explicitly opened. It is implicitly opened when the File name or File object is supplied to a Stream, Reader, or Writer class constructor or method. For output operations, the physical file is created by this action. When a physical file is opened or created for a File object, there is no provision to specify any attributes, such as opening for read-only. Also, the same file cannot be written on two separate occasions and maintain the original contents. If a program writes to the physical file Test.txt, closes the file, and exits, the next Java program that tries to write to this file will get an empty file. If a program needs the contents to persist, it must open the file as a RandomAccessFile object. You can make a file lots of ways. Here's how you would create a File object based on a directory/filename combination:
//First make a File object representing a directory. File dir = new File("/export"); File theFile = new File(dir, "BookCatalog.dat");
Fields
Constructors
publicFile (String pathname) public File(String parent, String child) public File(File parent, String child)
Methods
getName() getParent() getParentFile() getPath() isAbsolute() getAbsolutePath() getAbsoluteFile() getCanonicalPath() throws IOException getCanonicalFile() throws IOException toURL() throws MalformedURLException
public public public public public public public public public public public public public public public public public public public public public public public
boolean boolean boolean boolean boolean boolean long long boolean boolean void String[] String[] File[] File[] File[] boolean boolean boolean boolean boolean static File[] static File
public static File public public public public public int int boolean int String
canRead() canWrite() exists() isDirectory() isFile() isHidden() lastModified() length() createNewFile() throws IOException delete() deleteOnExit() list() list(FilenameFilter filter) listFiles() listFiles(FilenameFilter filter) listFiles(FileFilter filter) mkdir() mkdirs() renameTo(File dest) setLastModified(long time) setReadOnly() listRoots() createTempFile(String prefix, String suffix, File directory) throws IOException createTempFile(String prefix, String suffix) throws IOException compareTo(File pathname) compareTo(Object o) equals(Object obj) hashCode() toString()
Class FileInputStream
A FileInputStream reads input from a stream one byte at a time. It is essentially the lowest-level of abstraction among the concrete classes in java.io. This a very primitive stream class and probably is not useful for much except for wrapping inside another class. For example:
FileInputStream fis = new FileInputStream("BookObj.dat"); ObjectInputStream ois = new ObjectInputStream(fis); Book b = (Book)ois.readObject();
Constructors
FileInputStream (String name) throws FileNotFoundException FileInputStream(File file) throws FileNotFoundException FileInputStream(FileDescriptor fdObj)
read() throws IOException read(byte[] b) throws IOException read(byte[] b, int off, int len) throws IOException skip(long n) throws IOException
available() throws IOException close() throws IOException getFD() throws IOException finalize() throws IOException
A FileOutputStream is a stream for writing bytes, generally to a file. It is the lowest level of abstraction among the concrete classes in java.io. Like a FileInputStream, a FileOutputStream is a fairly primitive object and not nearly as useful as other I/O classes. Therefore, it is better to wrap a FileOutputStream in another stream. For example:
FileOutputStream fos = new FileOutputStream("BookObj.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); Book b = new Book("Lewis, C.S.", "Out of the Silent Planet"); oos.writeObject(b);
Constructors
FileOutputStream (String name) throws FileNotFoundException FileOutputStream(String name, boolean append) throws FileNotFoundException FileOutputStream(File file) throws IOException FileOutputStream(FileDescriptor fdObj)
public void
write(int b) throws IOException public void write(byte[] b) throws IOException public void write(byte[] b, int off, int len) throws IOException public void close() throws IOException public final FileDescriptor getFD() throws IOException protected void finalize() throws IOException
Class FilePermission
A FilePermission object represents a pathname and the actions that can be performed upon it.
Constructors
public
Methods
Class FileReader
The class FileReader is a basic Reader class that reads input data at the character level from a file or other source. It assumes valid character mapping.
Constructors
FileReader (String fileName) throws FileNotFoundException FileReader(File file) throws FileNotFoundException FileReader(FileDescriptor fd)
Class FileWriter
The FileWriter class can write characters out to a file or other data store. It is a low-level of abstraction among the Writer subclasses.
Constructors
FileWriter (String fileName) throws IOException FileWriter(String fileName, boolean append) throws IOException FileWriter(File file) throws IOException FileWriter(FileDescriptor fd)
Class InputStream
InputStream is the base class for all the input stream classes. It is abstract itself and must be subclassed to make a class that can be instantiated. It provides the basic methods shared by all input streams, such as read and mark. You do not directly use this abstract class as a general rule.
Constructors
public
Methods
InputStream()
public abstract int public int public int public long public int public void public void public void public boolean
Class InputStreamReader
read() throws IOException read(byte[] b) throws IOException read(byte[] b, int off, int len) throws IOException skip(long n) throws IOException available() throws IOException close() throws IOException mark(int readlimit) reset() throws IOException markSupported()
public class InputStreamReader extends Reader An InputStreamReader is a basic Reader class and can be used by other Reader classes. It is useful for going from a byte stream to a character reader object. For example, you can wrap System.in in an InputStreamReader and wrap that in a BufferedReader to read lines of text from the keyboard, like this:
InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String input = br.readLine();
Constructors
public public
Methods
public String public int public int public boolean public void
Class LineNumberReader
getEncoding() read() throws IOException read(char[] cbuf, int off, int len) throws IOException ready() throws IOException close() throws IOException
public class LineNumberReader extends BufferedReader A LineNumberReader object reads input from a character-oriented stream and provides line numbers for the input. A line is demarcated by a line feed, carriage return, or a combination of both.
Constructors
public public
Methods
public void public int public int public int public String public long public void public void
setLineNumber(int lineNumber) getLineNumber() read() throws IOException read(char[] cbuf, int off, int len) throws IOException readLine() throws IOException skip(long n) throws IOException mark(int readAheadLimit) throws IOException reset() throws IOException
Class ObjectInputStream
The ObjectInputStream class can be used to read objects and primitives from a stream. In general, its use is for deserializing serialized objects. This deserialization is automatic unless a subclass overrides readObject or implements Externalizable. This class provides the highest level of abstraction for an input stream. There is no Reader-based equivalent. You could use it to get an object from elsewhere, such as an object sent across a socket. Here's a simple example:
FileInputStream fis = new FileInputStream("BookObj.dat"); ObjectInputStream ois = new ObjectInputStream(fis); Book b = (Book)ois.readObject();
Constructors
public protected
Methods
protected Object
public void
readObject() throws OptionalDataException, ClassNotFoundException, IOException readObjectOverride() throws OptionalDataException, ClassNotFoundException, IOException defaultReadObject()
public ObjectInputStream.GetField
public void
protected Class
public int public int public int public void public boolean public byte public int public short public int public char public int public long public float public double public void public void
throws IOException, ClassNotFoundException, NotActiveException readFields() throws IOException, ClassNotFoundException, NotActiveException registerValidation (ObjectInputValidation obj, int prio) throws NotActiveException, InvalidObjectException resolveClass(Object StreamClass v) throws IOException, ClassNotFoundException resolveObject(Object obj) throws IOException enableResolveObject(boolean enable) throws SecurityException readStreamHeader() throws IOException, StreamCorruptedException read() read(byte[] b, int off, int len) throws IOException available() close() throws IOException readBoolean() throws IOException readByte() throws IOException readUnsignedByte() throws IOException readShort() throws IOException readUnsignedShort() throws IOException readChar() throws IOException readInt() throws IOException readLong() throws IOException readFloat() throws IOException readDouble() throws IOException readFully(byte[] data) throws IOException readFully(byte[] data, int offset, int size) throws IOException skipBytes(int len) throws IOException readLine() throws IOException
public String
Class ObjectOutputStream
public class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants An ObjectOutputStream is a stream that operates at a high level of abstraction. It can be used to write objects and primitives to a stream. It also has a header that is sent first and must be read first from an ObjectInputStream. This stream would be used to write serialized objects so that they can be sent to another object or stored in a file.
Tip You might use this for writing a Vector to a Socket that contains customer information for an e-commerce application. By using a Vector, you reduce network round trips, because you don't have to send several discrete data items.
FileOutputStream fos = new FileOutputStream("BookObj.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); Book b = new Book("Lewis,C.S.", "That Hideous Strength"); oos.writeObject(b);
Constructors
public protected
Methods
protected void public void public final void public void public ObjectOutputStream.PutField public void public void protected void protected Object protected boolean protected void public void public void public void public void protected void public void public void public void public void public void public void
writeObjectOverride(Object obj) throws IOException useProtocolVersion(int version) throws IOException writeObject(Object obj) throws IOException defaultWriteObject() throws IOException putFields() throws IOException writeFields() throws IOException reset() throws IOException annotateClass(Class cl) throws IOException replaceObject(Object obj) throws IOException enableReplaceObject(boolean enable) throws SecurityException writeStreamHeader() throws IOException write(int data) throws IOException write(byte[] b) throws IOException write(byte[] b, int off, int len) throws IOException flush() throws IOException drain() throws IOException close() throws IOException writeBoolean(boolean data) throws IOException writeByte(int data) throws IOException writeShort(int data) throws IOException writeChar(int data) throws IOException writeInt(int data)
public void public void public void public void public void public void
Class OutputStream
throws IOException writeLong(long data) throws IOException writeFloat(float data) throws IOException writeDouble(double data) throws IOException writeBytes(String data) throws IOException writeChars(String data) throws IOException writeUTF(String data) throws IOException
This is an abstract class, which is the superclass for all out stream classes. It defines the basic methods used for all output streams, such as flush() and write(). You gen erally do not want to use this class, because it is too primitive.
Constructors
public
Methods
OutputStream()
write(int b) throws IOException write(byte[] b) throws IOException write(byte[] b, int off, int len) throws IOException flush()
public void
Class PipedInputStream
A PipedInputStream is used to read data from a PipedOutputStream. Typically, piped streams are used for communication between threads. One thread will generally write data to a PipedOutputStream and another thread will read data from a PipedInptStream connected to the PipedOutputStream. Generally, one thread should not use both of these two streams connected together. Otherwise, a deadlock can be created.
Fields
public public
Methods
public void protected void public int public int public int public void
Class PipedOutputStream
connect(PipedOutputStream src) throws IOException receive(int b) throws IOException read() throws IOException read(byte[] b, int off, int len) throws IOException available() throws IOException close() throws IOException
A PipedOutputStream can be used with a PipedInputStream to provide for communication between threads. One thread writes to a PipedOutputStream and another thread reads that data by connecting to the PipedOutputStream with a PipedInputStream.
Constructors
public public
Methods
public void public void public void public void public void
Class PrintStream
connect(PipedInputStream snk) throws IOException write(int b) throws IOException write(byte[] b, int off, int len) throws IOException flush() throws IOException close() throws IOException
PrintStream is a convenient class for printing output, either to files or to standard out. This class has multiple overloaded methods to print a variety of data types. Unlike most other IO classes, PrintStream does not generally throw any exceptions. You can use this in your code for putting in debugging statements like
System.out.println("Got this far"); You can also use it to write data as lines of text. For example:
String s1 = textfield1.getText(); String s2 = textfield2.getText(); PrintStream ps = new PrintStream(new FileOutputStream("Text.dat")); ps.println(s1); ps.println(s2); ps.close();
Constructors
public public
Methods
public void public void public boolean protected void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void
flush() close() checkError() setError() write(int b) write(byte[] buf, int off, int len) print(boolean b) print(char c) print(int i) print(long l) print(float f) print(double d) print(char[] s) print(String s) print(Object obj) println() println(boolean x) println(char x) println(int x) println(long x) println(float x) println(double x)
The PrintWriter class is similar to the PrintStream class except that it is used for writing Unicode characters, as opposed to raw bytes. Like a PrintStream, a PrintWriter can write to a file. The benefit of using a PrintWriter is that Java does not need to translate between characters and bytes in order to write to the file. It can simply write out Unicode characters.
Fields
protected Writer
Constructors
out
PrintWriter(Writer out) PrintWriter(Writer out, boolean autoFlush) PrintWriter(OutputStream out) PrintWriter(OutputStream out,
boolean autoFlush)
Methods
public void public void public boolean protected void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void public void
Class RandomAccessFile
flush() close() checkError() setError() write(int c) write(char[] buf, int off, int len) write(char[] buf) write(String s, int off, int len) write(String s) print(boolean b) print(char c) print(int i) print(long l) print(float f) print(double d) print(char[] s) print(String s) print(Object obj) println() println(boolean x) println(char x) println(int x) println(long x) println(float x) println(double x) println(char[] x) println(String x) println (Object x)
A RandomAccessFile provides a file object that supports reading from and writing to specific positions in the file, as well as appending to the end of the file. This is significantly different from a basic File object. If you attempt to write to a pre-existing physical file mapped to a File object, its contents are erased and replaced by the new output. To avoid this, a RandomAccessFile must be used. Because the RandomAccessFile class provides no support for buffering data, it is advisable to subclass this class with one that provides buffering of input and output. This can significantly improve performance.
Constructors
public public
Methods
RandomAccessFile(String name, String mode) throws FileNotFoundException RandomAccessFile(File file, String mode) throws IOException
public final FileDescriptor public int public int public int public final void public final void public int public void public void public void
getFD() throws IOException read() throws IOException read(byte[] b, int off, int len) throws IOException read(byte[] b) throws IOException readFully(byte[] b) throws IOException readFully(byte[] b, int off, int len) throws IOException skipBytes(int n) throws IOException write(int b) throws IOException write(byte[] b) throws IOException write(byte[] b, int off, int len) throws IOException
public long public void public long public void public void public final boolean public final byte public final int public final short public final int public final char public final int public final long public final float public final double public final String public final String public final void public final void public final void public final void public final void public final void public final void public final void public final void public final void public final void
getFilePointer() throws IOException seek(long pos) throws IOException length() throws IOException setLength(long newLength) throws IOException close() throws IOException readBoolean() throws IOException readByte() throws IOException readUnsignedByte() throws IOException readShort() throws IOException readUnsignedShort() throws IOException readChar() throws IOException readInt() throws IOException readLong() throws IOException readFloat() throws IOException readDouble() throws IOException readLine() throws IOException readUTF() throws IOException writeBoolean(boolean v) throws IOException writeByte(int v) throws IOException writeShort(int v) throws IOException writeChar(int v) throws IOException writeInt(int v) throws IOException writeLong(long v) throws IOException writeFloat(float v) throws IOException writeDouble(double v) throws IOException writeBytes(String s) throws IOException writeChars(String s) throws IOException writeUTF(String str) throws IOException
Class Reader
The Reader class is the abstract superclass of all the character-oriented input stream classes, or readers. It provides the minimum set of methods, similar to what the InputStream class defines for byte-oriented input streams, for character data.
Fields
protected Object
Constructors
lock
protected protected
Methods
public int public int public abstract int public long public boolean public boolean public void
read() throws IOException read(char[] cbuf) throws IOException read(char[] cbuf, int off, int len) throws IOException skip(long n) throws IOException ready() throws IOException markSupported() mark(int readAheadLimit) throws IOException
A SequenceInputStream object provides the capability to read multiple streams as one stream, in order of their specification.
Constructors
public public
Methods
available() throws IOException read() throws IOException read(byte[] b, int off, int len) throws IOException
public void
Class StreamTokenizer
The StreamTokenizer provides a means to read a stream as a series of tokens, read (or rather, returned) one at a time. A file in which fields are separated by colons, for example, could be read one field at a time, using the colon as the divider between tokens.
Fields
Constructors
public public
Methods
resetSyntax() wordChars(int low, int hi) whitespaceChars(int low, int hi) ordinaryChars(int low, int hi) ordinaryChar(int ch)
commentChar(int ch) quoteChar(int ch) parseNumbers() eolIsSignificant(boolean flag) slashStarComments(boolean flag) slashSlashComments(boolean flag) lowerCaseMode(boolean fl) nextToken() throws IOException pushBack() lineno() toString()
Class StringBufferInputStream
This is a byte-oriented stream which provides for reading data from a String as a stream. Only the lower eight bits of a character are used by this stream class. This entire class is deprecated because of this feature. A StringReader should always be used instead of a StringBufferInputStream.
Fields
Constructors
public
Methods
StringBufferInputStream(String s)
Class StringReader
The StringReader class is a character-based stream designed for reading Strings from an input source.
Constructors
public
Methods
StringReader(String s)
public int public int public long public boolean public boolean public void public void public void
Class StringWriter
read() throws IOException read(char[] cbuf, Int off, int len) throws IOException skip(long ns) throws IOException ready() throws IOException markSupported() mark(int readAheadLimit) throws IOException reset() throws IOException close()
public class StringWriter extends Writer The StringWriter class is a character-based stream that provides for writing characters from a buffer to a String.
Constructors
public
StringWriter()
public
Methods
StringWriter(int initialSize)
write(int c) write(char[] cbuf, int off, int len) write(String str) write(String str, int off, int len) toString() getBuffer() flush() close() throws IOException
Class Writer
The Writer class is the abstract base class for all other Writer, or character-oriented streams.
Fields
protected Object
Constructors
lock
protected protected
Methods
public void public void public abstract void public void public void public abstract void public abstract void
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
write(int c) throws IOException write(char[] cbuf) throws IOException write(char[] cbuf, int off, int len) throws IOException write(String str) throws IOException write(String str, int off, Int len) throws IOException flush() throws IOException close() throws IOException
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
fields BufferedReader constructors methods 2nd BufferedWriter constructors methods ByteArrayInputStream constructors fields methods ByteArrayOutputStream constructors fields methods CharArrayReader constructors fields methods CharArrayWriter constructors fields methods DataInputStream constructors methods 2nd DataOutputStream constructors fields methods 2nd File constructors fields methods 2nd FileInputStream constructors methods 2nd FileOutputStream constructors methods FilePermission constructors methods FileReader constructors FileWriter constructors InputStream constructors methods 2nd InputStreamReader constructors methods java.io BufferedInputStream BufferedInputStream, constructors BufferedInputStream, fields BufferedInputStream, methods 2nd BufferedOutputStream
BufferedOutputStream, constructors BufferedOutputStream, fields BufferedReader BufferedReader, constructors BufferedReader, methods 2nd BufferedWriter BufferedWriter, constructors BufferedWriter, methods ByteArrayInputStream ByteArrayInputStream, constructors ByteArrayInputStream, fields ByteArrayInputStream, methods ByteArrayOutputStream ByteArrayOutputStream, constructors ByteArrayOutputStream, fields ByteArrayOutputStream, methods CharArrayReader CharArrayReader, constructors CharArrayReader, fields CharArrayReader, methods CharArrayWriter CharArrayWriter, constructors CharArrayWriter, fields CharArrayWriter, methods DataInputStream DataInputStream, constructors DataInputStream, methods 2nd DataOutputStream DataOutputStream, constructors DataOutputStream, fields DataOutputStream, methods 2nd File File, constructors File, fields File, methods 2nd FileInputStream FileInputStream, constructors FileInputStream, methods 2nd FileOutputStream FileOutputStream, constructors FileOutputStream, methods FilePermission FilePermission, constructors FilePermission, methods FileReader FileReader, constructors FileWriter FileWriter, constructors InputStream InputStream, constructors InputStream, methods 2nd InputStreamReader InputStreamReader, constructors InputStreamReader, methods LineNumberReader LineNumberReader, constructors LineNumberReader, methods ObjectInputStream ObjectInputStream, constructors
ObjectInputStream, methods 2nd 3rd ObjectOutputStream ObjectOutputStream, constructors ObjectOutputStream, methods 2nd OutputStream OutputStream, constructors OutputStream, methods PipedInputStream PipedInputStream, constructors PipedInputStream, fields PipedInputStream, methods PipedOutputStream PipedOutputStream, constructors PipedOutputStream, methods PrintStream PrintStream, constructors PrintStream, methods 2nd PrintWriter PrintWriter, constructors PrintWriter, fields PrintWriter, methods 2nd RandomAccessFile RandomAccessFile, constructors RandomAccessFile, methods 2nd 3rd Reader Reader, constructors Reader, fields Reader, methods 2nd SequenceInputStream SequenceInputStream, constructors SequenceInputStream, methods StreamTokenizer StreamTokenizer, constructors StreamTokenizer, fields StreamTokenizer, methods 2nd StringBufferInputStream StringBufferInputStream, constructors StringBufferInputStream, fields StringBufferInputStream, methods StringReader StringReader, constructors StringReader, methods StringWriter StringWriter, constructors StringWriter, methods Writer Writer, constructors Writer, fields Writer, methods 2nd LineNumberReader constructors methods ObjectInputStream constructors methods 2nd 3rd ObjectOutputStream constructors methods 2nd OutputStream
constructors methods methods, defining PipedInputStream constructors fields methods PipedOutputStream constructors methods PrintStream constructors methods 2nd PrintWriter constructors fields methods 2nd RandomAccessFile constructors methods 2nd 3rd Reader constructors fields methods 2nd SequenceInputStream constructors methods StreamTokenizer constructors fields methods 2nd StringBufferInputStream constructors fields methods StringReader constructors methods StringWriter constructors methods Writer constructors fields methods 2nd constructors BufferedInputStream class BufferedOutputStream class BufferedReader class BufferedWriter class ByteArrayInputStream class ByteArrayOutputStream class CharArrayReader class CharArrayWriter class DataInputStream class DataOutputStream class File class FileInputStream class FileOutputStream class
FilePermission class FileReader class FileWriter class InputStream class InputStreamReader class LineNumberReader class ObjectInputStream class ObjectOutputStream class OutputStream class PipedInputStream class PipedOutputStream class PrintStream class PrintWriter class RandomAccessFile class Reader class SequenceInputStream class StreamTokenizer class StringBufferInputStream class StringReader class StringWriter class Writer class DataInputStream class constructors methods 2nd DataOutputStream class constructors fields methods 2nd Externalizable interface methods fields BufferedInputStream class BufferedOutputStream class ByteArrayInputStream class ByteArrayOutputStream class CharArrayReader class CharArrayWriter class DataOutputStream class File class PipedInputStream class PrintWriter class Reader class Serializable interface StreamTokenizer class StringBufferInputStream class Writer class File class constructors fields methods 2nd file objects FileFilter interface methods FileInputStream class constructors methods 2nd FilenameFilter interface methods FileOutputStream class
constructors methods FilePermission class constructors methods FileReader class constructors FileWriter class constructors I/O buffering InputStream class constructors methods 2nd InputStreamReader class constructors methods interfaces Externalizable methods FileFilter methods FilenameFilter methods java.io Externalizable Externalizable, methods FileFilter FileFilter, methods FilenameFilter FilenameFilter, methods Serializable Serializable, fields Serializable fields java.io BufferedInputStream class constructors fields methods 2nd BufferedOutputStream class constructors fields BufferedReader class constructors methods 2nd BufferedWriter class constructors methods ByteArrayInputStream class constructors fields methods ByteArrayOutputStream class constructors fields methods CharArrayReader class constructors fields
methods CharArrayWriter class constructors fields methods classes BufferedInputStream BufferedInputStream, constructors BufferedInputStream, fields BufferedInputStream, methods 2nd BufferedOutputStream BufferedOutputStream, constructors BufferedOutputStream, fields BufferedReader BufferedReader, constructors BufferedReader, methods 2nd BufferedWriter BufferedWriter, constructors BufferedWriter, methods ByteArrayInputStream ByteArrayInputStream, constructors ByteArrayInputStream, fields ByteArrayInputStream, methods ByteArrayOutputStream ByteArrayOutputStream, constructors ByteArrayOutputStream, fields ByteArrayOutputStream, methods CharArrayReader CharArrayReader, constructors CharArrayReader, fields CharArrayReader, methods CharArrayWriter CharArrayWriter, constructors CharArrayWriter, fields CharArrayWriter, methods DataInputStream DataInputStream, constructors DataInputStream, methods 2nd DataOutputStream DataOutputStream, constructors DataOutputStream, fields DataOutputStream, methods 2nd File File, constructors File, fields File, methods 2nd FileInputStream FileInputStream, constructors FileInputStream, methods 2nd FileOutputStream FileOutputStream, constructors FileOutputStream, methods FilePermission FilePermission, constructors FilePermission, methods FileReader FileReader, constructors FileWriter FileWriter, constructors
InputStream InputStream, constructors InputStream, methods 2nd InputStreamReader InputStreamReader, constructors InputStreamReader, methods LineNumberReader LineNumberReader, constructors LineNumberReader, methods ObjectInputStream ObjectInputStream, constructors ObjectInputStream, methods 2nd 3rd ObjectOutputStream ObjectOutputStream, constructors ObjectOutputStream, methods 2nd OutputStream OutputStream, constructors OutputStream, methods PipedInputStream PipedInputStream, constructors PipedInputStream, fields PipedInputStream, methods PipedOutputStream PipedOutputStream, constructors PipedOutputStream, methods PrintStream PrintStream, constructors PrintStream, methods 2nd PrintWriter PrintWriter, constructors PrintWriter, fields PrintWriter, methods 2nd RandomAccessFile RandomAccessFile, constructors RandomAccessFile, methods 2nd 3rd Reader Reader, constructors Reader, fields Reader, methods 2nd SequenceInputStream SequenceInputStream, constructors SequenceInputStream, methods StreamTokenizer StreamTokenizer, constructors StreamTokenizer, fields StreamTokenizer, methods 2nd StringBufferInputStream StringBufferInputStream, constructors StringBufferInputStream, fields StringBufferInputStream, methods StringReader StringReader, constructors StringReader, methods StringWriter StringWriter, constructors StringWriter, methods Writer Writer, constructors Writer, fields
Writer, methods 2nd constructors BufferedInputStream class BufferedOutputStream class BufferedReader class BufferedWriter class ByteArrayInputStream class ByteArrayOutputStream class CharArrayReader class CharArrayWriter class DataInputStream class DataOutputStream class File class FileInputStream class FileOutputStream class FilePermission class FileReader class FileWriter class InputStream class InputStreamReader class LineNumberReader class ObjectInputStream class ObjectOutputStream class OutputStream class PipedInputStream class PipedOutputStream class PrintStream class PrintWriter class RandomAccessFile class Reader class SequenceInputStream class StreamTokenizer class StringBufferInputStream class StringReader class StringWriter class Writer class DataInputStream class constructors methods 2nd DataOutputStream class constructors fields methods 2nd Externalizable interface methods fields BufferedInputStream class BufferedOutputStream class ByteArrayInputStream class ByteArrayOutputStream class CharArrayReader class CharArrayWriter class DataOutputStream class File class PipedInputStream class PrintWriter class Reader class Serializable interface StreamTokenizer class StringBufferInputStream class
Writer class File class constructors fields methods 2nd FileFilter interface methods FileInputStream class constructors methods 2nd FilenameFilter interface methods FileOutputStream class constructors methods FilePermission class constructors methods FileReader class constructors FileWriter class constructors InputStream class constructors methods 2nd InputStreamReader class constructors methods interfaces Externalizable Externalizable, methods FileFilter FileFilter, methods FilenameFilter FilenameFilter, methods Serializable Serializable, fields LineNumberReader class constructors methods methods BufferedInputStream class 2nd BufferedReader class 2nd BufferedWriter class ByteArrayInputStream class ByteArrayOutputStream class CharArrayReader class CharArrayWriter class DataInputStream class 2nd DataOutputStream class 2nd Externalizable interface File class 2nd FileFilter interface FileInputStream class 2nd FilenameFilter interface FileOutputStream class FilePermission class InputStream class 2nd InputStreamReader class LineNumberReader class
ObjectInputStream class 2nd 3rd ObjectOutputStream class 2nd OutputStream class PipedInputStream class PipedOutputStream class PrintStream class 2nd PrintWriter class 2nd RandomAccessFile class 2nd 3rd Reader class 2nd SequenceInputStream class StreamTokenizer class 2nd StringBufferInputStream class StringReader class StringWriter class Writer class 2nd ObjectInputStream class constructors methods 2nd 3rd ObjectOutputStream class constructors methods 2nd OutputStream class constructors methods PipedInputStream class constructors fields methods PipedOutputStream class constructors methods PrintStream class constructors methods 2nd PrintWriter class constructors fields methods 2nd RandomAccessFile class constructors methods 2nd 3rd Reader class constructors fields methods 2nd SequenceInputStream class constructors methods Serializable interface fields StreamTokenizer class constructors fields methods 2nd StringBufferInputStream class constructors fields methods StringReader class
constructors methods StringWriter class constructors methods Writer class constructors fields methods 2nd LineNumberReader class constructors methods methods BufferedInputStream class 2nd BufferedReader class 2nd BufferedWriter class ByteArrayInputStream class ByteArrayOutputStream class CharArrayReader class CharArrayWriter class DataInputStream class 2nd DataOutputStream class 2nd Externalizable interface File class 2nd FileFilter interface FileInputStream class 2nd FilenameFilter interface FileOutputStream class FilePermission class InputStream class 2nd InputStreamReader class LineNumberReader class ObjectInputStream class 2nd 3rd ObjectOutputStream class 2nd OutputStream class 2nd PipedInputStream class PipedOutputStream class PrintStream class 2nd PrintWriter class 2nd RandomAccessFile class 2nd 3rd Reader class 2nd SequenceInputStream class StreamTokenizer class 2nd StringBufferInputStream class StringReader class StringWriter class Writer class 2nd ObjectInputStream class constructors methods 2nd 3rd ObjectOutputStream class constructors methods 2nd objects file serializing OutputStream class constructors methods defining
packages java.io BufferedInputStream class BufferedInputStream class, constructors BufferedInputStream class, fields BufferedInputStream class, methods 2nd BufferedOutputStream class BufferedOutputStream class, constructors BufferedOutputStream class, fields BufferedReader class BufferedReader class, constructors BufferedReader class, methods 2nd BufferedWriter class BufferedWriter class, constructors BufferedWriter class, methods ByteArrayInputStream class ByteArrayInputStream class, constructors ByteArrayInputStream class, fields ByteArrayInputStream class, methods ByteArrayOutputStream class ByteArrayOutputStream class, constructors ByteArrayOutputStream class, fields ByteArrayOutputStream class, methods CharArrayReader class CharArrayReader class, constructors CharArrayReader class, fields CharArrayReader class, methods CharArrayWriter class CharArrayWriter class, constructors CharArrayWriter class, fields CharArrayWriter class, methods DataInputStream class DataInputStream class, constructors DataInputStream class, methods 2nd DataOutputStream class DataOutputStream class, constructors DataOutputStream class, fields DataOutputStream class, methods 2nd Externalizable interface Externalizable interface, methods File class File class, constructors File class, fields File class, methods 2nd FileFilter interface FileFilter interface, methods FileInputStream class FileInputStream class, constructors FileInputStream class, methods 2nd FilenameFilter interface FilenameFilter interface, methods FileOutputStream class FileOutputStream class, constructors FileOutputStream class, methods FilePermission class FilePermission class, constructors FilePermission class, methods FileReader class FileReader class, constructors FileWriter class
FileWriter class, constructors InputStream class InputStream class, constructors InputStream class, methods 2nd InputStreamReader class InputStreamReader class, constructors InputStreamReader class, methods LineNumberReader class LineNumberReader class, constructors LineNumberReader class, methods ObjectInputStream class ObjectInputStream class, constructors ObjectInputStream class, methods 2nd 3rd ObjectOutputStream class ObjectOutputStream class, constructors ObjectOutputStream class, methods 2nd OutputStream class OutputStream class, constructors OutputStream class, methods PipedInputStream class PipedInputStream class, constructors PipedInputStream class, fields PipedInputStream class, methods PipedOutputStream class PipedOutputStream class, constructors PipedOutputStream class, methods PrintStream class PrintStream class, constructors PrintStream class, methods 2nd PrintWriter class PrintWriter class, constructors PrintWriter class, fields PrintWriter class, methods 2nd RandomAccessFile class RandomAccessFile class, constructors RandomAccessFile class, methods 2nd 3rd Reader class Reader class, constructors Reader class, fields Reader class, methods 2nd SequenceInputStream class SequenceInputStream class, constructors SequenceInputStream class, methods Serializable interface Serializable interface, fields StreamTokenizer class StreamTokenizer class, constructors StreamTokenizer class, fields StreamTokenizer class, methods 2nd StringBufferInputStream class StringBufferInputStream class, constructors StringBufferInputStream class, fields StringBufferInputStream class, methods StringReader class StringReader class, constructors StringReader class, methods StringWriter class StringWriter class, constructors StringWriter class, methods
Writer class Writer class, constructors Writer class, fields Writer class, methods 2nd piped streams theads 2nd PipedInputStream class constructors fields methods PipedOutputStream class constructors methods PrintStream class constructors methods 2nd PrintWriter class constructors fields methods 2nd RandomAccessFile class constructors methods 2nd 3rd Reader class constructors fields methods 2nd SequenceInputStream class constructors methods Serializable interface fields serializing objects Sockets Vectors writing to streams piped threads 2nd StreamTokenizer class constructors fields methods 2nd StringBufferInputStream class constructors fields methods StringReader class constructors methods StringWriter class constructors methods threads piped streams 2nd Vectors writing to Sockets Writer class constructors
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244206044003009005254108
System.out.println(some primitive or object); The class Class is used to interrogate other classes and to help load classes dynamically into memory (for example, a JDBC driver). The String class is used to represent character strings. The System class provides several useful methods, such as arraycopy(), provides access to the Runtime object, and defines standard in, standard out, and standard err. The class Object is the base class of all other classes and objects in Java, including arrays.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244206044003010204116241
Interfaces
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244206044002226038102123
Classes
Boolean Byte Character Character.Subset Character.UnicodeBlock Class ClassLoader Compiler Double Float InheritableThreadLocal Integer Long Math Number Object Package Process Runtime RuntimePermission SecurityManager Short String StringBuffer System Thread ThreadGroup ThreadLocal Throwable Void
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244206044002229228151246
Exceptions
ArithmeticException ArrayIndexOutOfBoundsException ArrayStoreException ClassCastException ClassNotFoundException CloneNotSupportedException Exception IllegalAccessException IllegalArgumentException IllegalMonitorStateException IllegalStateException IllegalThreadStateException IndexOutOfBoundsException InstantiationException InterruptedException NegativeArraySizeException NoSuchFieldException NoSuchMethodException NullPointerException NumberFormatException RuntimeException SecurityException StringIndexOutOfBoundsException UnsupportedOperationException
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244206044002232022196121
Errors
AbstractMethodError ClassCircularityError ClassFormatError Error ExceptionInInitializerError IllegalAccessError IncompatibleClassChangeError InstantiationError InternalError LinkageError NoClassDefFoundError NoSuchFieldError NoSuchMethodError OutOfMemoryError StackOverflowError ThreadDeath UnknownError UnsatisfiedLinkError UnsupportedClassVersionError VerifyError VirtualMachineError
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 24. java.lang > Interface and Class Details < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244207153043018047010070
public abstract interface Cloneable This interface is used to indicate that the class can have its instances cloned. The clone() method, inherited from Object, is valid, although typically a user-defined class will need to define its own version of clone() to provide the behavior desired for copying an entire object with a deep copy. A deep copy is the process of copying all the fields of all the dependent objects of a given object being copied. If you have a class that contains a GregorianCalendar object, a deep copy would copy the object, instantiate a new GregorianCalendar object for the copied object, and copy all the fields from the GregorianCalendar object that is being copied. In general, if an object has no clone() method defined that does a deep copy, the JVM does a shallow copy, copying object references and not actually copying dependent objects contained within the cloned object. If you attempt to clone() an object whose class does not implement Cloneable, you will receive a CloneNotSupportedException. This interface has no methods. Interface Comparable
public abstract interface Comparable This interface is used to order objects based on the class's natural order, which is generally ascending order. It shows up most visibly in the Collection framework classes in java.util, but it is valid for any class to implement this method. The class is then required to implement a compareTo() method, which compares objects based on the natural order of the class. Lists and other Collection implementations can use the Comparable interface for sorting through the Collections.sort() method.
Methods
int
compareTo (Object o)
Interface Runnable
public abstract interface Runnable This interface is used for multithreading. Implementers of this interface provide a run() method, which is then used by a thread. The only code a thread will run is one coded in a run() method. A common place to see this interface implemented is in an Applet. The Applet provides a run() method so that the Applet can use MediaTracker to monitor the downloading of images in a separate thread of execution. In this way, the GUI part of the Applet can continue functioning while image downloading is taking place. Any Applet that needs to download several images
should use this technique in order to get the user interface painted and functioning as soon as possible.
Methods
void
run()
Class Boolean
This is a wrapper class for boolean primitives. It is useful for passing a boolean primitive as a parameter to a method that accepts only an instance of Object or a subclass of Object, such as the methods of Vector.
Fields
toString() valueOf(String s)
This is a wrapper class for byte primitives. It is useful for passing a byte primitive as a parameter to a method that accepts only an instance of Object or a subclass of Object, such as the methods of Vector.
Fields
Constructors
Methods
byte int int static Byte double boolean float int int long static byte static byte short String static String static Byte static Byte Class Character
byteValue() compareTo(Byte anotherByte) compareTo(Object o) decode(String nm) doubleValue() equals(Object obj) floatValue() hashCode() intValue() longValue() parseByte(String s) parseByte(String s, int radix) shortValue() toString() toString(byte b) valueOf(String s) valueOf(String s, int radix)
The Character class provides a wrapper class for char primitives. It is useful for passing a char primitive as a parameter to a method that accepts only an instance of Object or a subclass of Object, such as the methods of Vector. Note The character attribute tables for specific versions of Unicode are available at ftp://ftp.unicode.org/Public/
Fields
static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static static
byte byte byte byte byte byte byte byte byte byte byte byte byte int char int char byte byte byte byte byte byte byte byte byte byte byte byte byte Class byte byte
COMBINING_SPACING_MARK CONNECTOR_PUNCTUATION CONTROL CURRENCY_SYMBOL DASH_PUNCTUATION DECIMAL_DIGIT_NUMBER ENCLOSING_MARK END_PUNCTUATION FORMAT LETTER_NUMBER LINE_SEPARATOR LOWERCASE_LETTER MATH_SYMBOL MAX_RADIX MAX_VALUE MIN_RADIX MIN_VALUE MODIFIER_LETTER MODIFIER_SYMBOL NON_SPACING_MARK OTHER_LETTER OTHER_NUMBER OTHER_PUNCTUATION OTHER_SYMBOL PARAGRAPH_SEPARATOR PRIVATE_USE SPACE_SEPARATOR START_PUNCTUATION SURROGATE TITLECASE_LETTER TYPE UNASSIGNED UPPERCASE_LETTER
Constructors
char int int static int boolean static char static int static int int static boolean static boolean
charValue() compareTo(Character anotherCharacter) compareTo(Object o) digit(char ch, int radix) equals(Object obj) forDigit(int digit, int radix) getNumericValue(char ch) getType(char ch) hashCode() isDefined(char ch) isDigit(char ch)
static static static static static static static static static static static static static static static static static String static static
boolean boolean boolean boolean boolean boolean boolean boolean boolean boolean boolean boolean boolean boolean boolean boolean char char char
isIdentifierIgnorable(char ch) isISOControl(char ch) isJavaIdentifierPart(char ch) isJavaIdentifierStart(char ch) isJavaLetter(char ch) (deprecated) isJavaLetterOrDigit(char ch) (deprecated) isLetter(char ch) isLetterOrDigit(char ch) isLowerCase(char ch) isSpace(char ch) (deprecated isSpaceChar(char ch) isTitleCase(char ch) isUnicodeIdentifierPart(char ch) isUnicodeIdentifierStart(char ch) isUpperCase(char ch) isWhitespace(char ch) toLowerCase(char ch) toString() toTitleCase(char ch) toUpperCase(char ch)
Class Class
The class Class can be used to provide information about an object or class definition. It can also be used to dynamically load a class using the forName() method in this way:
Class.forName("Myjdbcdriver"); You can also get information using this class. For example, here is how to get the name of another class:
String className = anObject.getClass().getName(); Class Class provides numerous methods used in reflection for interrogating an object or class definition for fields,
static Class static Class Class[] ClassLoader Class Constructor Constructor[] Class[] Constructor Constructor[] Field Field[] Method Method[] Class Field Field[] Class[] Method Method[] int String Package ProtectionDomain URL InputStream Object[] Class boolean boolean boolean boolean boolean Object String Class ClassLoader
forName(String className) forName(String name, boolean initialize, ClassLoader loader) getClasses() getClassLoader() getComponentType() getConstructor(Class[] parameterTypes) getConstructors() getDeclaredClasses() getDeclaredConstructor(Class[] parameterTypes) getDeclaredConstructors() getDeclaredField(String name) getDeclaredFields() getDeclaredMethod(String name, Class[] parameterTypes) getDeclaredMethods() getDeclaringClass() getField(String name) getFields() getInterfaces() getMethod(String name, Class[] parameterTypes) getMethods() getModifiers() getName() getPackage() getProtectionDomain() getResource(String name) getResourceAsStream(String name) getSigners() getSuperclass() isArray() isAssignableFrom(Class cls) isInstance(Object obj) isInterface() isPrimitive() newInstance() toString()
ClassLoader is the abstract base class for all class loaders, both those defined in the Java 2 SDK and custom, user-defined class loaders. The purpose of a custom class loader is to help control the loading of classes. It is used when your application requires help in addition to that offered by the facilities defined in the primordial class loader. Most applications do not need a custom class loader. It is needed less often in Java 2 than it is in previous Java versions, in part due to the Extension mechanism.
Constructors
protected protected
Methods
protected Class protected String protected Class protected URL protected Enumeration protected Class protected Package protected Package[] ClassLoader URL InputStream Enumeration static ClassLoader static URL static InputStream static Enumeration
defineClass(byte[] b, int off, int len) (deprecated) defineClass(String name, byte[] b, int off, int len) defineClass(String name, byte[] b, int off, int len, ProtectionDomain protectionDomain) definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) findClass(String name) findLibrary(String libname) findLoadedClass(String name) findResource(String name) findResources(String name) findSystemClass(String name) getPackage(String name) getPackages() getParent() getResource(String name) getResourceAsStream(String name) getResources(String name) getSystemClassLoader() getSystemResource(String name) getSystemResourceAsStream(String name) getSystemResources(String name)
loadClass(String name) loadClass(String name, boolean resolve) resolveClass(Class c) setSigners(Class c, Object[] signers)
The Compiler class does nothing but serve as a placeholder for Java to native code compilers, such as a JIT (just-in-time) compiler.
Methods
Class Double
The Double class is a wrapper for double primitives. It is useful for passing a double primitive as a parameter to a method that accepts only an instance of Object or a subclass of Object, such as the methods of Vector.
Fields
Constructors
float int int boolean static boolean boolean static boolean static double long static double short String static String static Double Class Float
floatValue() hashCode() intValue() isInfinite() isInfinite(double v) isNaN() isNaN(double v) longBitsToDouble(long bits) longValue() parseDouble(String s) shortValue() toString() toString(double d) valueOf(String s)
The Float class is a wrapper class for float primitives. It is useful for passing a byte primitive as a parameter to a method that accepts only an instance of Object or a subclass of Object, such as the methods of Vector. The Float class also provides methods which are useful for converting command-line or GUI input from string data that represents floating point data into a float primitive.
Fields
Constructors
byte int int double boolean static int float int static float int boolean static boolean boolean static boolean long static float short String static String static Float Class Integer
byteValue() compareTo(Float anotherFloat) compareTo(Object o) doubleValue() equals(Object obj) floatToIntBits(float value) floatValue() hashCode() intBitsToFloat(int bits) intValue() isInfinite() isInfinite(float v) isNaN() isNaN(float v) longValue() parseFloat(String s) shortValue() toString() toString(float f) valueOf(String s)
public final class Integer extends Number implements Comparable The Integer class is a wrapper class for int primitives. Note that, unlike other wrapper classes, the name of this class does not match the name of the primitive data type. It is useful for passing an int primitive as a parameter to a method that accepts only an instance of Object or a subclass of Object, such as the methods of Vector.
The Integer class and other wrapper classes also are used for data conversion. When a user types data into a TextField, for example, the getText() method retrieves the data as a String. If the value is supposed to be numeric, maybe an int for some value or a float for money, you need to convert it. To convert the String to an int, you can code
int value = Integer.parseInt(textfld.getText()); This will convert the String in the TextField into an int called value.
Fields
static Integer double boolean float static Integer static Integer static Integer int int long static int static int short static String static String static String String static String static String static Integer static Integer Class Long
decode(String nm) doubleValue() equals(Object obj) floatValue() getInteger(String nm) getInteger(String nm, int val) getInteger(String nm, Integer val) hashCode() intValue() longValue() parseInt(String s) parseInt(String s, int radix) shortValue() toBinaryString(int i) toHexString(int i) toOctalString(int i) toString() toString(int i) toString(int i, int radix) valueOf(String s) valueOf(String s, int radix)
The Long class provides a wrapper class for long primitives. It is useful for passing a long primitive as a
parameter to a method that accepts only an instance of Object or a subclass of Object, such as the methods of Vector.
Fields
byte int int static Long double boolean float static Long static Long static Long int int long static long static long short static String static String static String String static String static String static Long static Long Class Math
byteValue() compareTo(Long anotherLong) compareTo(Object o) decode(String nm) doubleValue() equals(Object obj) floatValue() getLong(String nm) getLong(String nm, long val) getLong(String nm, Long val) hashCode() intValue() longValue() parseLong(String s) parseLong(String s, int radix) shortValue() toBinaryString(long i) toHexString(long i) toOctalString(long i) toString() toString(long i) toString(long i, int radix) valueOf(String s) valueOf(String s, int radix)
The Math class provides several functions for doing mathematical calculations, such as min, max, absolute value, square root, sine, cosines, and the like. Most of these methods are static. Static methods are used because you do not really want to create an instance of this class. You simply want to put a value into a formula and get a result back. Therefore, the Math class provides methods that act as mathematical functions to call with a value and from which you get a value back.
Fields
E PI
static static static static static static static static static static static static static static static static static static static static static static static static
double float int long double double double double double double double double double double double float int long double float int long double double
abs(double a) abs(float a) abs(int a) abs(long a) acos(double a) asin(double a) atan(double a) atan2(double a, double b) ceil(double a) cos(double a) exp(double a) floor(double a) IEEEremainder(double f1, double f2) log(double a) max(double a, double b) max(float a, float b) max(int a, int b) max(long a, long b) min(double a, double b) min(float a, float b) min(int a, int b) min(long a, long b) pow(double a, double b) random()
rint(double a) round(double a) round(float a) sin(double a) sqrt(double a) tan(double a) toDegrees(double angrad) toRadians(double angdeg)
Class Object
Object is the base class of all classes in Java, both those in the Java 2 SDK and all user-defined classes. Having Object at the top of all class hierarchies allows for flexible coding like this:
public boolean doSomething(Object obj) { if(obj instanceof someClass) { // Do something appropriate. } } This method can take an object of any class. Inside the method, if it matters, you can determine the exact class type. If the exact type doesn't matter, you just use the obj parameter as is. This makes for much more flexible code that will be much easier to use than code with separate methods for every data type in your program. This class also provides many methods used by all other classes, such as wait() and notify(), used in multithreaded programs, and equals() to compare two objects.
Constructors
Object ()
Methods
protected Object boolean protected void Class int void void String
A Runtime object represents the runtime environment of your program. You can use the Runtime object, but it is a singleton, and you cannot make your own instance of it. You can call getRuntime() to get the Runtime object. You can use the object for calling the methods of this class, such as
Runtime.getRuntime().gc() which is a request to run garbage collection. Note that this is a request. Garbage collection runs as a low priority thread. If and when the CPU is idle, the Garbage Collector might run. Garbage collection also takes place when there is no more memory available in the JVM. You can also query the environment like this:
Runtime.getRuntime().totalMemory(); This returns total memory. Another useful, but a platform-dependent method is exec(). You can use this to run a command or program on the same system. Windows users should note that you cannot simply say
Runtime.getRuntime().exec("dir"); Instead, you must do an exec() on Command.com and pass it a parameter that is the command you want to execute.
Methods
Process Process
Process Process void long void InputStream OutputStream static Runtime void void void static void long void void
exec(String[] cmdarray, String[] envp) exec(String command, String[] envp) exit(int status) freeMemory() gc() getLocalizedInputStream(InputStream in) (deprecated) getLocalizedOutputStream(OutputStream out) (deprecated) getRuntime() load(String filename) loadLibrary(String libname) runFinalization() runFinalizersOnExit(boolean value) (deprecated) totalMemory() traceInstructions(boolean on) traceMethodCalls(boolean on)
Class RuntimePermission
public final class RuntimePermission extends BasicPermission The RuntimePermission class represents permissions granted to your program, usually in a policy file. A RuntimePermission contains a category, such as FilePermission, and a target name, which might be a file or an absolute path to a file specified as a URL. It might also represent all files. The target name is tied to the type or category of permission. You can grant permission of type RuntimePermission or other permission classes, such as ReflectionPermission, in a policy file The default permissions available in Java 2 can be seen in the policytool.
Constructors
The SecurityManager class is the base class for program security. By default, Java only grants permissions that have been specifically given. Browsers generally instantiate a SecurityManager object, which does not allow Applets to read or write to the client system, change ThreadGroup properties, read certain system properties, or connect to a host besides the one it came from. Applications generally (with the notable exception of RMI) do not run with a SecurityManager installed in memory. After a SecurityManager object is instantiated in a JVM, it cannot be replaced. In Java 2, the behavior of the SecurityManager has changed, so that now, when a given operation such as creating a FileInputStream is requested, the SecurityManager object (or an instance of a subclass instance) checks the permissions granted to that code to decide if the operation will be allowed. Note For more information about SecurityManager changes made in Java 2 and advice regarding porting of 1.1-style security managers, see the release documentation at
java.sun.com/products/jdk/1.2/docs/guide/security/index.html
Fields
protected boolean
Constructors
inCheck (deprecated)
SecurityManager ()
Methods
checkAccept(String host, int port) checkAccess(Thread t) checkAccess(ThreadGroup g) checkAwtEventQueueAccess() checkConnect(String host, int port) checkConnect(String host, int port, Object context)
void void void void void void void void void void void void void void void void void void void void void void boolean void void protected int protected int protected ClassLoader protected Class protected Class[] boolean Object ThreadGroup protected boolean protected boolean Class Short
checkCreateClassLoader() checkDelete(String file) checkExec(String cmd) checkExit(int status) checkLink(String lib) checkListen(int port) checkMemberAccess(Class clazz, int which) checkMulticast(InetAddress maddr) checkMulticast(InetAddress maddr, byte ttl) checkPackageAccess(String pkg) checkPackageDefinition(String pkg) checkPermission(Permission perm) checkPermission(Permission perm, Object context) checkPrintJobAccess() checkPropertiesAccess() checkPropertyAccess(String key) checkRead(FileDescriptor fd) checkRead(String file) checkRead(String file, Object context) checkSecurityAccess(String target) checkSetFactory() checkSystemClipboardAccess() checkTopLevelWindow(Object window) checkWrite(FileDescriptor fd) checkWrite(String file) classDepth(String name) (deprecated classLoaderDepth() (deprecated) currentClassLoader() (deprecated) currentLoadedClass() (deprecated) getClassContext() getInCheck() (deprecated) getSecurityContext() getThreadGroup() inClass(String name) (deprecated) inClassLoader() (deprecated)
The Short class is a wrapper class for short primitives. This is useful for passing a short primitive to a method, such as the methods of a Vector, which only take instances of classes, not primitives.
Fields
byte int int static Short double boolean float int int
byteValue() compareTo(Object o) compareTo(Short anotherShort) decode(String nm) doubleValue() equals(Object obj) floatValue() hashCode() intValue()
short short
longValue() parseShort(String s) parseShort(String s, int radix) shortValue toString() toString(short s) valueOf(String s) valueOf(String s, int radix)
Class String
The String class represents a character string. There are several methods to convert other kinds of data, such as a byte array, into a String. Strings live in two places, depending upon how they are created:
q q
String author1 = "Lewis, C.S."; the String is placed in the String literal pool. If you create a String like this:
String author2 = new String("Lewis, C.S."); this String is placed in the heap. This distinction is important because this code
if(author1.equalsIgnoreCase(author2) returns true. The first tests for the equality of object references. The equals() method or equalsIgnoreCase, however, tests for equality of String contents. If you code this:
String author3 = "Lewis, C.S."; if(author2 == author3) it will return true. When a String needs to be put in the String literal pool, Java first checks to see if it is already there. If it is, the object reference is simply returnedin this case easily to author3. If it is not, a duplicate copy is placed in the String literal pool. You can create duplicate Strings with the new String() approach, however, because new causes separate memory to be allocated for the String. The memory used for a String is special. A String, once defined, cannot be changed directly. If you attempt to change a String, such as concatenating to it, Java in fact creates a new buffer, copies the String into it, and adds the piece you are concatenating. The original String's memory area is then discarded. This plugs several security holes so that no one can dynamically change the contents of a String by merely manipulating memory. The definition of String is also final. You cannot override or subclass the class to change this immutability.
Fields
static Comparator
Constructors
CASE_INSENSITIVE_ORDER
String() String(byte[] bytes) String(byte[] ascii, int hibyte) String(byte[] bytes, int offset, String(byte[] ascii, int hibyte, String(byte[] bytes, int offset, String(byte[] bytes, String enc) String(char[] value) String(char[] value, int offset, String(String value) String(StringBuffer buffer)
Methods
(deprecated) int length) int offset, int count) (deprecated) int length, String enc)
int count)
char int int int String static String static String boolean boolean boolean byte[] void
charAt(int index) compareTo(Object o) compareTo(String anotherString) compareToIgnoreCase(String str) concat(String str) copyValueOf(char[] data) copyValueOf(char[] data, int offset, int count) endsWith(String suffix) equals(Object anObject) equalsIgnoreCase(String anotherString) getBytes() getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
(deprecated) byte[] void int int int int int String int int int int int boolean offset, boolean String boolean boolean String String char[] String String String String String String static String static String static String static String static String static String static String static String static String Class StringBuffer
getBytes(String enc) getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) hashCode() indexOf(int ch) indexOf(int ch, int fromIndex) indexOf(String str) indexOf(String str, int fromIndex) intern() lastIndexOf(int ch) lastIndexOf(int ch, int fromIndex) lastIndexOf(String str) lastIndexOf(String str, int fromIndex) length() regionMatches(boolean ignoreCase, int toffset, String other, int
regionMatches(int toffset, String other, int ooffset, int len) replace(char oldChar, char newChar) startsWith(String prefix) startsWith(String prefix, int toffset) substring(int beginIndex) substring(int beginIndex, int endIndex) toCharArray() toLowerCase() toLowerCase(Locale locale) toString() toUpperCase() toUpperCase(Locale locale) trim() valueOf(boolean b) valueOf(char c) valueOf(char[] data) valueOf(char[] data, int offset, int count) valueOf(double d) valueOf(float f) valueOf(int i) valueOf(long l) valueOf(Object obj)
A String is an immutable, first-degree object. In fact, when you try to change it, Java creates a new area in memory to create the new version. A StringBuffer object, on the other hand, is mutable and allows you to change its contents in place. Therefore, if your program needs to do lots of String manipulation, as a word processing program might, you should use StringBuffers rather than Strings to save memory.
Constructors
StringBuffer StringBuffer StringBuffer StringBuffer StringBuffer StringBuffer StringBuffer StringBuffer StringBuffer StringBuffer int char StringBuffer StringBuffer void void StringBuffer StringBuffer StringBuffer StringBuffer StringBuffer StringBuffer StringBuffer StringBuffer
append(boolean b) append(char c) append(char[] str) append(char[] str, int offset, int len) append(double d) append(float f) append(int i) append(long l) append(Object obj) append(String str) capacity() charAt(int index) delete(int start, int end) deleteCharAt(int index) ensureCapacity(int minimumCapacity) getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) insert(int offset, boolean b) insert(int offset, char c) insert(int offset, char[] str) insert(int index, char[] str, int offset, int len) insert(int offset, double d) insert(int offset, float f) insert(int offset, int i) insert(int offset, long l)
StringBuffer StringBuffer int StringBuffer StringBuffer void void String String String Class System
insert(int offset, Object obj) insert(int offset, String str) length() replace(int start, int end, String str) reverse() setCharAt(int index, char ch) setLength(int newLength) substring(int start) substring(int start, int end) toString()
The System class is a singleton that provides several useful methods. It defines standard in, out and err. It allows you to get system properties, with getProperty() or getProperties(). It allows you to copy arrays with arraycopy(). It provides a means to get the system Runtime object.
Fields
err in out
static void static static static static static long void void String Properties
arraycopy(Object src, int src_position, Object dst, int dst_position, int length) currentTimeMillis() exit(int status) gc() getenv(String name) (deprecated) getProperties()
static static static static static static static static static static static static static static static
String String SecurityManager int void void String void void void void void void String void
getProperty(String key) getProperty(String key, String def) getSecurityManager() identityHashCode(Object x) load(String filename) loadLibrary(String libname) mapLibraryName(String libname) runFinalization() runFinalizersOnExit(boolean value) (deprecated) setErr(PrintStream err) setIn(InputStream in) setOut(PrintStream out) setProperties(Properties props) setProperty(String key, String value) setSecurityManager(SecurityManager s)
Class Thread
A Thread is a unit of work or execution context. It is used for accomplishing multiple tasks at one time. A Thread is not serializable. This makes sense, because you cannot use its context to set the environment from an object. A Thread has four states:
q q
Initialized, accomplished with a constructor Runnable, accomplished with start()The start() method makes the Thread ready to execute, but the Thread is put into a queue, where it awaits its turn at the CPU. The started Thread will execute when the JVM or CPU schedules the Thread to run. Nonrunnable, accomplished with wait(), join(), suspend(), or yield()A nonrunnable Thread can be made runnable again. Dead, accomplished with stop() or by completing its run() methodA dead Thread cannot be started or made runnable again.
Threads always run the code in a run()method defined in a class that implements the Runnable interface. This is the only way to create code that a Thread will execute, even if the run() method calls other methods.
Fields
Thread() Thread(Runnable target) Thread(Runnable target, String name) Thread(String name) Thread(ThreadGroup group, Runnable target) Thread(ThreadGroup group, Runnable target, String name) Thread(ThreadGroup group, String name)
Methods
static int void int static Thread void static void static int ClassLoader String int ThreadGroup void static boolean boolean boolean boolean void void void void void void void void void static void static void void void void void String static void
activeCount() checkAccess() countStackFrames() (deprecated) currentThread() destroy() dumpStack() enumerate(Thread[] tarray) getContextClassLoader() getName() getPriority() getThreadGroup() interrupt() interrupted() isAlive() isDaemon() isInterrupted() join() join(long millis) join(long millis, int nanos) resume() (deprecated) run() setContextClassLoader(ClassLoader cl) setDaemon(boolean on) setName(String name) setPriority(int newPriority) sleep(long millis) sleep(long millis, int nanos) start() stop() (deprecated) stop(Throwable obj) (deprecated) suspend() (deprecated) toString() yield()
Class ThreadGroup
A Thread always runs as part of a ThreadGroup. You can modify several Threads by modifying the properties of the ThreadGroup to which the Threads belong.
Constructors
int int boolean void void int int int int int String ThreadGroup void boolean boolean void boolean void void void
activeCount() activeGroupCount() allowThreadSuspension(boolean b) (dprecated) checkAccess() (deprecated) destroy() enumerate(Thread[] list) enumerate(Thread[] list, boolean recurse) enumerate(ThreadGroup[] list) enumerate(ThreadGroup[] list, boolean recurse) getMaxPriority() getName() getParent() interrupt() isDaemon() isDestroyed() list() parentOf(ThreadGroup g) resume() (deprecated) setDaemon(boolean daemon) setMaxPriority(int pri)
The Throwable class is the base class of all Error and Exception classes. Generally, programs do not deal with the Throwable class directly, but only with its subclasses. Other classes, however, can use the methods inherited from Throwable directly.
Constructors
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
Character, methods 2nd Class Class, methods 2nd ClassLoader ClassLoader, constructors ClassLoader, methods 2nd Compiler Compiler, methods Double Double, fields Double, methods 2nd Float Float, fields Float, methods 2nd Integer Integer, fields Integer, methods 2nd Long Long, constructors Long, fields Long, methods 2nd Math Math, fields Math, methods 2nd Object Object, constructors Object, methods 2nd Runtime Runtime, methods 2nd RuntimePermission SecurityManager SecurityManager, constructors SecurityManager, fields SecurityManager, methods 2nd Short Short, fields Short, methods 2nd String String, fields String, methods 2nd StringBuffer StringBuffer, methods 2nd System System, fields System, methods 2nd Thread Thread, fields Thread, methods 2nd ThreadGroup ThreadGroup, methods 2nd Throwable Throwable, methods Long constructors fields methods 2nd Math fields methods 2nd Object
constructors methods 2nd Runtime methods 2nd RuntimePermission SecurityManager constructors documentation, Java Web site fields methods 2nd Short fields methods 2nd String fields methods 2nd StringBuffer methods 2nd System fields methods 2nd Thread fields methods 2nd states 2nd ThreadGroup methods 2nd Throwable methods ClassLoader class constructors methods 2nd clone() method Cloneable interface Collections.sort() method Comparable interface methods compareTo() method Compiler class methods compilers JIT (just-in-time) constructors Character class ClassLoader class Long class Object class SecurityManager class copying objects deep copies shallow copies Dead state deep copies of objects documentation SecurityManager class Java Web site Double class fields methods 2nd double primitives
Error class Exception class exec() method fields Boolean class Byte class Character class 2nd Double class Float class Integer class Long class Math class SecurityManager class Short class String class System class Thread class Float class fields methods 2nd float primitives forName() method FTP sites Unicode character attribute tables getProperties() method getProperty() method getRuntime() method GregorianCalendar object Initialized state int primitives Integer class fields methods 2nd interfaces Cloneable Comparable methods java.lang Cloneable Comparable Comparable, methods Runnable Runnable, methods Runnable methods Java Web site SecurityManager class documentation java.lang Boolean class fields methods Byte class fields methods 2nd Character class constructors fields 2nd methods 2nd Class class methods 2nd
classes Boolean Boolean, fields Boolean, methods Byte Byte, fields Byte, methods 2nd Character Character, constructors Character, fields 2nd Character, methods 2nd Class Class, methods 2nd ClassLoader ClassLoader, constructors ClassLoader, methods 2nd Compiler Compiler, methods Double Double, fields Double, methods 2nd Float Float, fields Float, methods 2nd Integer Integer, fields Integer, methods 2nd Long Long, constructors Long, fields Long, methods 2nd Math Math, fields Math, methods 2nd Object Object, constructors Object, methods 2nd Runtime Runtime, methods 2nd RuntimePermission SecurityManager SecurityManager, constructors SecurityManager, fields SecurityManager, methods 2nd Short Short, fields Short, methods 2nd String String, fields String, methods 2nd StringBuffer StringBuffer, methods 2nd System System, fields System, methods 2nd Thread Thread, fields Thread, methods 2nd ThreadGroup ThreadGroup, methods 2nd
Throwable Throwable, methods ClassLoader class constructors methods 2nd Cloneable interface Comparable interface methods Compiler class methods constructors Character class ClassLoader class Long class Object class SecurityManager class Double class fields methods 2nd fields Boolean class Byte class Character class 2nd Double class Float class Integer class Long class Math class SecurityManager class Short class String class System class Thread class Float class fields methods 2nd Integer class fields methods 2nd interfaces Cloneable Comparable Comparable, methods Runnable Runnable, methods Long class constructors fields methods 2nd Math class fields methods 2nd methods Boolean class Byte class 2nd Character class 2nd Class class 2nd ClassLoader class 2nd Comparable interface Compiler class Double class 2nd
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=178 (38 of 43) [6/2/2002 11:12:44 PM]
Float class 2nd Integer class 2nd Long class 2nd Math class 2nd Object class 2nd Runnable interface Runtime class 2nd SecurityManager class 2nd Short class 2nd String class 2nd StringBuffer class 2nd System class 2nd Thread class 2nd ThreadGroup class 2nd Throwable class Object class constructors methods 2nd Runnable interface methods Runtime class methods 2nd RuntimePermission class SecurityManager class constructors fields methods 2nd Short class fields methods 2nd String class fields methods 2nd StringBuffer class methods 2nd System class fields methods 2nd Thread class fields methods 2nd ThreadGroup class methods 2nd Throwable class methods JIT (just-in-time) compiler join() method just-in-time (JIT) compiler Long class constructors fields methods 2nd long primitives Math class fields methods 2nd MediaTracker methods aarraycopy() Boolean class
Byte class 2nd Character class 2nd Class class 2nd ClassLoader class 2nd close() Collections.sort() Comparable interface compareTo() Compiler class Double class 2nd exec() Float class 2nd forName() getProperties() getProperty() getRuntime() Integer class 2nd join() Long class 2nd Math class 2nd Object class 2nd run() 2nd 3rd Runnable interface Runtime class 2nd SecurityManager class 2nd Short class 2nd start() stop() String class 2nd StringBuffer class 2nd suspend() System class 2nd Thread class 2nd ThreadGroup class 2nd Throwable class wait() yield() Nonrunnable state Object class constructors methods 2nd objects deep copies GregorianCalendar shallow copies packages java.lang Boolean class Boolean class, fields Boolean class, methods Byte class Byte class, fields Byte class, methods 2nd Character class Character class, constructors Character class, fields 2nd Character class, methods 2nd Class class Class class, methods 2nd ClassLoader class
ClassLoader class, constructors ClassLoader class, methods 2nd Cloneable interface Comparable interface Comparable interface, methods Compiler class Compiler class, methods Double class Double class, fields Double class, methods 2nd Float class Float class, fields Float class, methods 2nd Integer class Integer class, fields Integer class, methods 2nd Long class Long class, constructors Long class, fields Long class, methods 2nd Math class Math class, fields Math class, methods 2nd Object class Object class, constructors Object class, methods 2nd Runnable interface Runnable interface, methods Runtime class Runtime class, methods 2nd RuntimePermission class SecurityManager class SecurityManager class, constructors SecurityManager class, fields SecurityManager class, methods 2nd Short class Short class, fields Short class, methods 2nd String class String class, fields String class, methods 2nd StringBuffer class StringBuffer class, methods 2nd System class System class, fields System class, methods 2nd Thread class Thread class, fields Thread class, methods 2nd ThreadGroup class ThreadGroup class, methods 2nd Throwable class Throwable class, methods permissions RuntimePermission class primitives byte char double float
int long short programs SecurityManager class run() method 2nd 3rd Runnable interface methods Runnable state Runtime class methods 2nd RuntimePermission class security SecurityManager class SecurityManager class constructors documentation Java Web site fields methods 2nd shallow copies of objects Short class fields methods 2nd short primitives sites FTP. start() method states Dead Initialized Nonrunnable Runnable Thread class 2nd stop() method String class fields methods 2nd StringBuffer class methods 2nd Strings changing suspend() method System class fields methods 2nd tables character attributes Unicode FTP site Thread class fields methods 2nd states 2nd ThreadGroup class methods 2nd Throwable class methods Unicode FTP site character attribute tables wait() method
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244207153046197221044164
Note Most normal business applications never need to use these APIs. There are, however, methods in the class Class that are of general usefulness such as forName() and getName(). The first lets you load a class dynamically on demand, and the second lets you get at the object that sent an event, any kind if event. This method is much more general in application than a method such as getActionCommand(), if you want to determine the source of an ActionEvent.
The Reflection APIs were designed principally for Java Beans. Reflection enables a Java IDE, for example, to figure out what the fields and methods of an imported Java Bean class are, and enables a developer to set those fields, called Properties in the case of a Bean, through the IDE, even though the IDE has never previously seen the class definition.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244204148052068221099216
Interfaces
Member
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244204148053202241033030
Classes
All Books
Search
Java > PURE Java 2 > 25. java.lang.reflect > Interface and Class Details < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019244204148053207005177111
public abstract interface Member This interface provides a mechanism for identifying information about a specific field (data member) method or constructor.
Fields
Class AccessibleObject
The AccessibleObject class is the base class for the Field, Constructor, and Method classes. It permits a program to set a flag to modify the accessibility of an object's data members, constructors, and methods from the default provided by the access control modifiers (such as package default, protected, and private). Caution Be aware of what Reflection permits. Unless your class prevents it, my program can inspect and execute methods in your object or modify its data members using Reflection. You should consider, therefore, whether you want to allow or restrict this privilege.
Constructors
protected
Methods
AccessibleObject()
This class provides support for accessing Array information and for dynamically creating arrays.
Methods
static static static static static static static static static static static static static static static static static static static static static
Object boolean byte char double float int int long short Object Object void void void void void void void void void
get(Object array, int index) getBoolean(Object array, int index) getByte(Object array, intI index) getChar(Object array, int index) getDouble(Object array, int index) getFloat(Object array, int index) getInt(Object array, int I index) getLength(Object array) getLong(Object array, int index) getShort(Object array, int index) newInstance(Class componentType, int length) newInstance(Class componentType, int[] dimensions) set(Object array, int index, Object value) setBoolean(Object array, int index, boolean z) setByte(Object array, int index, byte b) setChar(Object array, int index, char c) setDouble(Object array, int index, double d) setFloat(Object array, int index, float f) setInt(Object array, int index, int i) setLong(Object array, int index, long l) setShort(Object array, int index, short s)
Class Constructor
The constructor is used to obtain information about a class's constructor(s) and to call a constructor. The main methods for obtaining the constructors of a class are
getConstructors() getConstructor() getDeclaredConstructor() getDeclaredConstructors() They are all found in the class java.lang.Class.
Methods
boolean Class Class[] int String Class[] int Object String Class Field
equals(Object obj) getDeclaringClass() getExceptionTypes() getModifiers() getName() getParameterTypes() hashCode() newInstance(Object[] initargs) toString()
The Field class is used to get the Fields (data members) in a class or interface, and from those Field objects, the modifiers of each field. The values of a Field can also be set through get() and set() methods. A Field is obtained with one of the following methods from the class Class:
getField() getDeclaredField() getDeclaredFields() getFields() For an example, see Chapter 13, "Reflection."
Methods
equals(Object obj) get(Object obj) getBoolean(Object obj) getByte(Object obj) getChar(Object obj)
Class double float int long int String Class int void void voidset voidset voidset voidset voidset voidset voidset String
getDeclaringClass() getDouble(Object obj) getFloat(Object obj) getInt(Object obj) getLong(Object obj) getModifiers() getName() getType() hashCode() set(Object obj, Object value) setBoolean(Object obj, boolean z) Byte(Object obj, byte b) Char(Object obj, char c) Double(Object obj, double d) Float(Object obj, float f) Int(Object obj, int i) Long(Object obj, long l) Short(Object obj, short s) toString()
Class Method
The Method class is used to obtain the names, modifiers, and parameters of the methods of a class. You can get the methods through one of the following methods in the class Class:
getMethods(), getMethod(String, Class[]), getDeclaredMethods(), getDeclaredMethod(String, Class[]) A method discovered through Reflection can be executed with the invoke() method.
Methods
boolean Class Class[] int String Class[] Class int Object String
equals(Object obj) getDeclaringClass() getExceptionTypes() getModifiers() getName() getParameterTypes() getReturnType() hashCode() invoke(Object obj, Object[] args) toString()
Class Modifier
The Modifier class is used to discover the modifiers for a class or member using
Class.getModifiers()
Member.getModifiers() The Modifier class contains several public final static int data members that can be used to compare the modifiers of a class or member. They can be used to determine if a class is abstract, for example. When you get the Modifier object for a class, you essentially get an int that includes all the modifier information. You can test this int to see if it includes a specific value, or simply print all the modifiers with the toString() method.
Fields
static static static static static static static static static static static static
int int int int int int int int int int int int
ABSTRACT FINAL INTERFACE NATIVE PRIVATE PROTECTED PUBLIC STATIC STRICT SYNCHRONIZED TRANSIENT VOLATILE
Constructors
Modifier()
Methods
static static static static static static static static static static static static static
boolean boolean boolean boolean boolean boolean boolean boolean boolean boolean boolean boolean String
isAbstract(int mod) isFinal(int mod) isInterface(int mod) isNative(int mod) isPrivate(int mod) isProtected(int mod) isPublic(int mod) isStatic(int mod) isStrict(int mod) isSynchronized(int mod) isTransient(int mod) isVolatile(int mod) toString(int mod)
Class ReflectPermission
extends BasicPermission
The ReflectPermission is the Permission class for Reflection. ReflectPermission has no actions, and the only name defined is suppressAccessChecks, which suppresses access checking by reflected objects. If this permission is set in a policy file, the consequence is that an object reflecting the data members and methods of another object can see all the data members and methods, whether the access for these members or methods is public, protected, private, or package-level default. Caution The ability to interrogate and manipulate an object using ReflectPermission is a powerful feature. It should only be granted with an understanding of the consequences of this permission. It allows one object to read all the private data of another object. This not only violates encapsulation, but can also pose severe security risks for sensitive data. Outside of debugging code, grant this Permission with great care. I cannot stress enough that ReflectPermission discards the default behavior of the JVM for access permission.
Table 25.1, based on the Java API documentation, shows what permission ReflectPermission grants and the consequences of that permission.
Table 25.1. What ReflectPermission Grants and the Attendant Risks Permission Target Name What the Permission Grants Potential Risks of Allowing Permission Sensitive data accessible by malicious SuppressAccess Checks Access to all fields and the capability to invoke constructors and methods code regardless of access modifiers Constructors
Modifier, fields 2nd Modifier, methods 2nd ReflectPermission ReflectPermission, constructors Method 2nd methods Modifier constructors fields 2nd methods 2nd Permission ReflectPermission ReflectPermission constructors permissions and risks Constructor class 2nd methods 2nd constructors AccessibleObject class Modifier class of classes methods ReflectPermission class Field class 2nd methods 2nd fields Member interface Modifier class 2nd interfaces java.lang.reflect Member Member, fields Member, methods Member fields methods java.lang.reflect AccessibleObject class constructors methods Array class methods 2nd classes AccessibleObject AccessibleObject, constructors AccessibleObject, methods Array Array, methods 2nd Constructor Constructor, methods 2nd Field Field, methods 2nd Method Method, methods Modifier Modifier, constructors Modifier, fields 2nd
Modifier, methods 2nd ReflectPermission ReflectPermission, constructors Constructor class methods 2nd constructors AccessibleObject class Modifier class ReflectPermission class Field class methods 2nd fields Member interface Modifier class 2nd interfaces Member Member, fields Member, methods Member interface fields methods Method class methods methods AccessibleObject class Array class 2nd Constructor class 2nd Field class 2nd Member interface Method class Modifier class 2nd Modifier class constructors fields 2nd methods 2nd ReflectPermission class constructors Member interface fields methods Method class 2nd methods methods AccessibleObject class Array class 2nd Constructor class 2nd constructors of classes Field class 2nd Member interface Method class Modifier class 2nd Modifier class constructors fields 2nd methods 2nd objects interrogating
manipulating ReflectPermission class packages java.lang.reflect AccessibleObject class AccessibleObject class, constructors AccessibleObject class, methods Array class Array class, methods 2nd Constructor class Constructor class, methods 2nd Field class Field class, methods 2nd Member interface Member interface, fields Member interface, methods Method class Method class, methods Modifier class Modifier class, constructors Modifier class, fields 2nd Modifier class, methods 2nd ReflectPermission class ReflectPermission class, constructors Permission class Reflection ReflectPermission permissions Reflection Reflection permissions ReflectPermission class ReflectPermission class constructors permissions risks
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244204148054063238035221
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244204148055132188130123
Classes
BigDecimal BigInteger
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244204148055128236128083
Class Details
Class BigDecimal
A BigDecimal object is an immutable, arbitrary precision, signed, decimal number. A BigDecimal has two components:
q q
An arbitrary precision, integer, unscaled value A nonnegative 32-bit integer scale that represents the number of digits to the right of the decimal point
You can perform arithmetic on a BigDecimal, as well as scaling and rounding. You have complete control over rounding. Here is a code snippet showing the use of a BigDecimal being used with a PreparedStatement for an SQL call:
PreparedStatement prepstmt = conn.prepareStatement ("UPDATE EMPLOYEE_TABLE " + SET SALARY = ? WHERE EMPNUM = ?"); prepstmt.setBigDecimal(1, 276672.00); // Arbitrary-precision decimal number prepstmt.setInt(2, 77539);
Fields
Constructors
BigDecimal (BigInteger val) BigDecimal(BigInteger unscaledVal, int scale) BigDecimal(double val) BigDecimal(String val)
Methods
BigDecimal BigDecimal int int BigDecimal BigDecimal double boolean float int int long BigDecimal BigDecimal BigDecimal
abs() add(BigDecimal val) compareTo(BigDecimal val) compareTo(Object o) divide(BigDecimal val, int roundingMode) divide(BigDecimal val, int scale, int roundingMode) doubleValue() equals(Object x) floatValue() hashCode() intValue() longValue() max(BigDecimal val) min(BigDecimal val) movePointLeft(int n)
BigDecimal BigDecimal BigDecimal int BigDecimal BigDecimal BigDecimal BigInteger String BigInteger static BigDecimal static BigDecimal Class BigInteger
movePointRight(int n) multiply(BigDecimal val) negate() scale() setScale(int scale) setScale(int scale, int roundingMode) int signum() subtract(BigDecimal val) toBigInteger() toString() unscaledValue() valueOf(long val) valueOf(long unscaledVal, int scale)
The BigInteger class provides immutable, arbitrary precision integers. All operations on a BigInteger object function as if the BigInteger were in two's complement form, like a primitive int. In addition, any of the operations defined in java.lang.Math may be performed on a BigInteger, as well as all the operations that can be done on an int primitive. Other operations may be performed on a BigInteger as well, such as GCD computations and bit manipulations, among others. Unlike an int primitive, operations on BigIntegers do not overflow. Instead, the result is made as
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=187 (3 of 6) [6/2/2002 11:13:49 PM]
ONE ZERO
BigInteger (byte[] val) BigInteger(int signum, byte[] magnitude) BigInteger(int bitLength, int certainty, Random rnd) BigInteger(int numBits, Random rnd) BigInteger(String val) BigInteger(String val, int radix)
Methods
BigInteger BigInteger BigInteger BigInteger int int BigInteger int int BigInteger BigInteger[] double boolean BigInteger float BigInteger int int int boolean long BigInteger BigInteger BigInteger BigInteger BigInteger BigInteger BigInteger BigInteger BigInteger
abs() add(BigInteger val) and(BigInteger val) andNot(BigInteger val) bitCount() bitLength() clearBit(int n) compareTo(BigInteger val) compareTo(Object o) divide(BigInteger val) divideAndRemainder(BigInteger val) doubleValue() equals(Object x) flipBit(int n) floatValue() gcd(BigInteger val) getLowestSetBit() hashCode() intValue() isProbablePrime(int certainty) longValue() max(BigInteger val) min(BigInteger val) mod(BigInteger m) modInverse(BigInteger m) modPow(BigInteger exponent, BigInteger m) multiply(BigInteger val) negate() not() or(BigInteger val)
BigInteger BigInteger BigInteger BigInteger BigInteger int BigInteger boolean byte[] String String static BigInteger BigInteger
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
pow(int exponent) remainder(BigInteger val) setBit(int n) shiftLeft(int n) shiftRight(int n) signum() subtract(BigInteger val) testBit(int n) toByteArray() toString() toString(int radix) valueOf(long val) xor(BigInteger val)
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
BigInteger class integers BigInteger Class java.math BigDecimal class constructors fields methods 2nd BigInteger class constructors fields methods 2nd classes BigDecimal BigDecimal, constructors BigDecimal, fields BigDecimal, methods 2nd BigInteger BigInteger, constructors BigInteger, fields BigInteger, methods 2nd constructors BigDecimal class BigInteger class fields BigDecimal class BigInteger class methods BigDecimal class 2nd BigInteger class 2nd math integers BigInteger Class methods BigDecimal class 2nd BigInteger class 2nd packages java.math BigDecimal class BigDecimal class, constructors BigDecimal class, fields BigDecimal class, methods 2nd BigInteger class BigInteger class, constructors BigInteger class, fields BigInteger class, methods 2nd
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244204148048100198124207
InetAddress
thisComputer = InetAddress.getLocalHost();
In addition, java.net provides classes for making connections to a URLfor example, the URLConnection and HttpURLConnection classes. These two classes may be used to communicate from a Java applet or application to a CGI script or a servlet. These two classes can call get() method and post() method routines as well as calling getOutputStream() and then writing to this stream to a middle-tier program. The getOutputStream() method may be used get a stream for passing an object from an application, usually on tier 1, to a servlet on tier 2 instead of just String data, as you would pass in a get() or post() request.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244204148049155008198251
Interfaces
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244205063054218244072007
Classes
Authenticator ContentHandler DatagramPacket DatagramSocket DatagramSocketImpl HttpURLConnection InetAddress JarURLConnection MulticastSocket NetPermission PasswordAuthentication ServerSocket Socket SocketImpl SocketPermission URL URLClassLoader URLConnection URLDecoder URLEncoder URLStreamHandler
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244205063055087222074122
Exceptions
All Books
Java > PURE Java 2 > 27. java.net > Interface and Class Details < BACK
158122020158178182047242169203138024176246252243131023021084156019244205063055083062155034
public abstract interface ContentHandlerFactory This interface is used for a concrete factory (in design-pattern terminology) in creating content-handler objects for specific MIME types. It's used by the class URLStreamHandler to create ContentHandler instances for MIME types. Generally, application programs do not explicitly create ContentHandler objects directly. Note The ContentHandler is factory created by a concrete factory. You can have an abstract factory or a concrete factory. An abstract factory provides a factory method, like getSomeObject() in an abstract class. You don't ever instantiate the factory; you only use its factory method. URLStreamHandler is an abstract class. A concrete factory in this situation would be a concrete class (one you can instantiate) that subclasses the abstract class URLStreamHandler and provides a method for creating concrete ContentHandler objects. The goal here of using both abstract and concrete factories is to allow you to have a way to organize related concrete factoriesin this case, for all the kinds of content handlers that might be needed for various MIME types.
Methods
ContentHandler
createContentHandler(String mimetype)
Interface SocketImplFactory
public abstract interface SocketImplFactory This interface defines a concrete factory implemented by the classes ServerSocket and Socket to create implementations of these socket classes.
Methods
SocketImpl
createSocketImpl()
Interface SocketOptions
public abstract interface SocketOptions This interface declares methods for setting or getting socket options in a Socket, ServerSocket, or MulticastSocket. Use this interface if the default properties of a given socket implementation are not suitable for your
application but you don't need to create a custom socket implementation. Note You will not directly use SocketOptions methods or data members if you are not subclassing SocketImpl or DatagramSocketImpl.
Fields
Object void
Class ContentHandler
public abstract class ContentHandler extends Object The abstract class ContentHandler is the superclass of all classes that read an object from a URLConnection.
An application doesn't generally call the getContent method in this class directly. Instead, an application calls the getContent() method in class URL or URLConnection. The application's content-handler factoryan instance of a class that implements the interface ContentHandlerFactory set up by a call to setContentHandler()is called with a String that gives the MIME type of the object being received on the socket. The factory returns an instance of a subclass of ContentHandler, and its getContent method is called to create the object.
Constructors
ContentHandler ()
Methods
getContent(URLConnection urlc)
The DatagramPacket class implements a datagram packet, which is used to send information from one datagram socket to another. Datagram sockets and packets provide for connectionless communication, which is less reliable than TCP connections. When you construct a DatagramPacket object, you must first decide if it will be used for sending or receiving UDP. Here is a code snippet that shows how to do each (although there are more constructors than these two for DatagramPacket):
byte[199] recvBuffer; byte[199] sendBuffer; // Make a DatagramPacket to receive data Datagram receiver = new DatagramPacket(recvBuffer, recvBuffer.length); // Make a DatagramPacket to send data Datagram sender = new DatagramPacket(sendBuffer, sendBuffer.length, "rumba", 12553); Notice that the main difference between these two constructor calls is that a DatagramPacket for sending requires a logical address, which consists of the host and port number of where the packet should be sent.
Constructors
length) length, InetAddress address, int port) offset, int length) offset, int length, InetAddress address,
Methods
InetAddress byte[] int int int void void void void void
getAddress() getData() getLength() getOffset() getPort() setAddress(InetAddress iaddr) setData(byte[] buf) setData(byte[] buf, int offset, int length) setLength(int length) setPort(int iport)
Class DatagramSocket
This class represents a socket for sending and receiving datagram packets. There's no concept, as in TCP sockets, of a Socket and ServerSocket. Instead, you either create a DatagramSocket bound to a specific host and port, or you leave out the host and port and the system chooses an arbitrary port for you on the requesting host. Here is a simple code snippet to show the key pieces of using a DatagramSocket (although there is more involved, as shown in Chapter 14, "Java Network Programming" ):
DatagramSocket socket = new DatagramSocket(5555); try { DatagramPacket recvPacket = new DatagramPacket(inBuffer, inBuffer.length); // Create a DatagramPacket to receive client requests. socket.receive(recvPacket); // Wait for a client request. This code blocks until it gets input. clientAddress = recvPacket.getAddress(); clientPort = recvPacket.getPort(); String text = new String(recvPacket.getData(), 0,recvPacket.getLength()); } This snippet shows the creation of a DatagramSocket object and a DatagramPacket object. Then you use the socket to get the packet from a sender (not a caller). The code blocks until it gets a packet. Then the contents of the package are
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=193 (4 of 25) [6/2/2002 11:14:42 PM]
accessed. Tip Using DatagramPackets and DatagramSockets you can create a simple, asynchronous messaging system for applications that do not need instant responses, such as a workflow engine or inventory control system.
Constructors
void void void InetAddress InetAddress int int int int int void void void void void
close() connect(InetAddress address, int port) disconnect() getInetAddress() getLocalAddress() getLocalPort() getPort() getReceiveBufferSize() getSendBufferSize() getSoTimeout() receive(DatagramPacket p) send(DatagramPacket p) setReceiveBufferSize(int size) setSendBufferSize(int size) setSoTimeout(int timeout)
Class HttpURLConnection
This is a URLConnection with support for HTTP-specific features. It would be an appropriate choice if you wanted to use an applet to communicate over HTTP to a Web servereither to a CGI script or, better still, a servlet. Using a servlet on the server side, you can even send objects from the applet to the server by using getOutputStream() and then wrapping it in an ObjectOutputStream.
Fields
static static static static static static static static static static static static static static static static static static static static static static static static static
int int int int int int int int int int int int int int int int int int int int int int int int int
HTTP_ACCEPTED HTTP_BAD_GATEWAY HTTP_BAD_METHOD HTTP_BAD_REQUEST HTTP_CLIENT_TIMEOUT HTTP_CONFLICT HTTP_CREATED HTTP_ENTITY_TOO_LARGE HTTP_FORBIDDEN HTTP_GATEWAY_TIMEOUT HTTP_GONE HTTP_INTERNAL_ERROR HTTP_LENGTH_REQUIRED HTTP_MOVED_PERM HTTP_MOVED_TEMP HTTP_MULT_CHOICE HTTP_NO_CONTENT HTTP_NOT_ACCEPTABLE HTTP_NOT_AUTHORITATIVE HTTP_NOT_FOUND HTTP_NOT_MODIFIED HTTP_OK HTTP_PARTIAL HTTP_PAYMENT_REQUIRED HTTP_PRECON_FAILED
static int static int static int static int static int static int static int static int static int static int protected String protected int protected String
Constructors
HTTP_PROXY_AUTH HTTP_REQ_TOO_LONG HTTP_RESET HTTP_SEE_OTHER HTTP_SERVER_ERROR HTTP_UNAUTHORIZED HTTP_UNAVAILABLE HTTP_UNSUPPORTED_TYPE HTTP_USE_PROXY HTTP_VERSION method responseCode responseMessage
protected
Methods
HttpURLConnection(URL u)
abstract void InputStream static boolean Permission String int String static void void abstract boolean Class InetAddress
disconnect() getErrorStream() getFollowRedirects() getPermission() getRequestMethod() getResponseCode() getResponseMessage() setFollowRedirects(boolean set) setRequestMethod(String method) usingProxy()
The InetAddress class represents an IP address for a host. You can use methods of this class to obtain the identity of the current hostgetLocalHost(), get the name of a host based on its IP addressgetHostname(), get an IP address
using the host namegetHostByName(), and so forth. Here is a sample of some of what you can do with this class:
InetAddress theHost = InetAddress.getLocalHost(); System.out.println("Local host: " + theHost); String hostName = theHost.getHostName(); String hostAddress = theHost.getHostAddress(); System.out.println("Host Name: " + hostName); System.out.println("Host Address: " + hostAddress); System.out.println("Host address by host name: " + InetAddress.getByName(hostName)); This code must be wrapped in a try block.
Methods
boolean byte[] static InetAddress[] static InetAddress String String static InetAddress int boolean String Class JarURLConnection
equals(Object obj) getAddress() getAllByName(String host) getByName(String host) getHostAddress() getHostName() getLocalHost() hashCode() isMulticastAddress() toString()
The JarURLConnection class represents a URLConnection to a jar file. You can use a JarURLConnection object to read a jar file as a URL. Here's the format of a jar URL:
jar:<url>!/{ entry} A JarURLConnection class could be used to extract or read a single file from a given jar file. You could, therefore, read a single class from a jar file on the local host, or an applet could read a single class from a jar file on a Web server on demand. Here are some examples of getting a single jar entry, a whole jar file, or a jar directory. In the examples, !/ serves as a separator. Jar entry:
jar:http://www.dantooine.com//boy.jar!/COM/dantooine/ Once you have the JarURLConnection, you can access the jar file or enter like any other URLConnection object or use the specific java.util classes that support dealing with jar files.
Fields
protected URLConnection
Constructors
jarFileURLConnection
protected
Methods
JarURLConnection(URL url)
Attributes Certificate[] String JarEntry abstract JarFile URL Attributes Manifest Class MulticastSocket
A Socket in Java can communicate to one host and port. A UDP DatagramSocket can communicate to one host and port at a time. A MulticastSocket can send a DatagramPacket to multiple ports on multiple hosts at one time. This could allow for broadcasting a message, for example, and would be useful in a messaging or publish/subscribe architecture. Like a regular UDP DatagramSocket, a UDP MulticastSocket is less reliable than a TCP Socket, but it's more flexible. Here is a simple code snippet showing you the basics of using a MulticastSocket object. When you use this class, you need to add recipients to a group. All the members of the group then get whatever is sent from the MulticastSocket object:
// Create data for the socket to send. byte[] msg = { 'S', 'U', 'N', 'W', '', '9', '3', '.', '5'}; InetAddress groupAddr = InetAddress.getByName("225.3.3.8"); // There is a specific range reserved for multicast sockets. MulticastSocket multiSock = new MulticastSocket(9953); multiSock.joinGroup(groupAddr); DatagramPacket stockQuote = new DatagramPacket(msg, msg.length, groupAddr, 9953); // The DatagramPacket constructor specifies the data, // its length, the group's IP address, and port of the // MulticastSocket. multiSock.send(stockQuote); // Get group responses! byte[] response = new byte[1000]; DatagramPacket receiver = new DatagramPacket(response, response.length); multiSock.receive(receiver) // You can also leave the group. multiSock.leaveGroup(groupAddr); Using the MulticastSocket class, you can build your own publish-subscribe application.
Constructors
getInterface() getTimeToLive() getTTL() (Deprecated) joinGroup(InetAddress mcastaddr) leaveGroup(InetAddress mcastaddr) send(DatagramPacket p, byte ttl) setInterface(InetAddress inf) setTimeToLive(int ttl) setTTL(byte ttl) (Deprecated)
Class PasswordAuthentication
The PasswordAuthentication class is used by an authenticator to authenticate a user. The PasswordAuthentication object merely holds a user ID and password.
Constructors
A ServerSocket object is the server-side socket used in a TCP/IP client/server application. When you create a ServerSocket, you specify a particular port and then use an accept() on that port. Here's an example:
ServerSocket ss = new ServerSocket(7777); ss.accept(); The accept() call blocks until a client connects to it. You can also use a ServerSocket to prevent more than one copy of your program from being run on a given host. If you create a ServerSocket on a given port, even if you never use the ServerSocket, a second copy of the program, when attempting to connect to the same port, will throw an exception. Then you can catch that exception and tell the user that only one copy of the program may run on the given host.
Constructors
ServerSocket (int port) ServerSocket(int port, int backlog) ServerSocket(int port, int backlog, InetAddress bindAddr)
Methods
Socket void InetAddress int int protected void static void void String Class Socket
accept() close() getInetAddress() getLocalPort() getSoTimeout() implAccept(Socket s) setSocketFactory(SocketImplFactory fac) setSoTimeout(int timeout) toString()
extends Object
A Socket object is a TCP socket, used for communicating between two hosts. A Socket is created generally with the host name and port address of the application to which you need to connect. When a client-side Socket connects to a ServerSocket, the accept() method of the ServerSocket returns a Socket object. These Socket objects are more reliable than UDP sockets because they make a real connection between hosts. Therefore, if it's imperative for a method to successfully send data to another host, TCP sockets should be chosen over UDP sockets. If it doesn't matter muchfor example, for sports scores that will be re-sent 30 seconds later, or for an atomic clock value that will not be valid laterthe lower overhead of UDP sockets is preferable for network bandwidth.
Constructors
Socket() Socket(InetAddress address, int port) Socket(InetAddress host, int port, boolean stream)
graphics/ccc.gif (Deprecated)
protected Socket(InetAddress address, int port, InetAddress, localAddr, int localPort) protected Socket(SocketImpl impl) protected Socket(String host, int port) protected Socket(String host, int port, boolean stream) (Deprecated) protected Socket(String host, int port, InetAddress localAddr, int localPort)
Methods
void InetAddress InputStream InetAddress int OutputStream int int int int int boolean void void static void
close() getInetAddress() getInputStream() getLocalAddress() getLocalPort() getOutputStream() getPort() getReceiveBufferSize() getSendBufferSize() getSoLinger() getSoTimeout() getTcpNoDelay() setReceiveBufferSize(int size) setSendBufferSize(int size) setSocketImplFactory(SocketImplFactory fac)
public abstract class SocketImpl extends Object implements SocketOptions SocketImpl is an abstract base class for all concrete socket implementations.
Fields
SocketImpl ()
Methods
protected protected protected protected protected protected protected protected protected protected protected protected
abstract void abstract int abstract void abstract void abstract void abstract void abstract void FileDescriptor InetAddress abstract InputStream int abstract OutputStream
accept(SocketImpl s) available() bind(InetAddress host, int port) close() connect(InetAddress address, int port) connect(String host, int port) create(boolean stream) getFileDescriptor() getInetAddress() getInputStream() getLocalPort() getOutputStream()
The URL class represents a Uniform Resource Locator, which is the "path" to a resource on the World Wide Web, such as a Web page, image, or class file. Information on the types of URLs and their formats can be found at http://www.ncsa.uiuc.edu/demoweb/url-primer.html This is an example of a URL. A URL may contain several parts:
q q
ProtocolSpecifies the protocol being used, such as http, ftp, file, or rmi. Domain nameThis is the main location of the resource, which is frequently a Web server, such as http://www.sun.com. File componentThis specifies additional information of where the resource is, including a directory structure and a filename, such as index.html. Port numberA URL may contain a specific port number. Generally, the port used for http is port 80, but this can be modified by an organization. RefAn extra piece of information that generally refers to a location within a given Web page, specified with a pound sign (#) and a location name (for example, #openingfiles). This is appended to the end of the URL. Therefore, you could have a URL that looks like this: http://www.purejava.com/source/chapter45/ejb#creating Note that this URL is fictitious.
Constructors
URL (String spec) URL(String protocol, String host, int port, String file) URL(String protocol, String host, int port, String file, URLStreamHandler handler) URL(String protocol, String host, String file) URL(URL context, String spec)
boolean Object String String int String String int URLConnection InputStream boolean protected void String ref) static void String String
equals(Object obj) getContent() getFile() getHost() getPort() getProtocol() getRef() hashCode() openConnection() openStream() sameFile(URL other) set(String protocol, String host, int port, String file, setURLStreamHandlerFactory(URLStreamHandlerFactory fac) toExternalForm() toString()
Class URLConnection
public abstract class URLConnection extends Object The URLConnection class is an abstract base class for all concrete URL connection subclasses, such as HttpURLConnection. This class provides several essential methods, such as openConnection() for creating a connection, getContent, get(), post(), and so on for getting or sending content over the connection, and several methods for interrogating the URL connection header information.
Fields
protected boolean
Constructors
useCaches
protected
Methods
URLConnection(URL url)
abstract void boolean Object String int String long static boolean static String boolean boolean boolean long static FileNameMap String String long int String long InputStream long OutputStream Permission String URL boolean protected static String static String void
graphics/ccc.gif
connect() getAllowUserInteraction() getContent() getContentEncoding() getContentLength() getContentType() getDate() getDefaultAllowUserInteraction() getDefaultRequestProperty(String key) getDefaultUseCaches() getDoInput() getDoOutput() getExpiration() getFileNameMap() getHeaderField(int n) getHeaderField(String name) getHeaderFieldDate(String name, long Default) getHeaderFieldInt(String name, int Default) getHeaderFieldKey(int n) getIfModifiedSince() getInputStream() getLastModified() getOutputStream() getPermission() getRequestProperty(String key) getURL() getUseCaches() guessContentTypeFromName(String fname) guessContentTypeFromStream(InputStream is) setAllowUserInteraction(boolean
allowuserinteraction) setContentHandlerFactory(ContentHandlerFactory fac) setDefaultAllowUserInteraction (boolean defaultallowuserinteraction) static void setDefaultRequestProperty(String key, String value) void setDefaultUseCaches(boolean defaultusecaches) void setDoInput(boolean doinput) void setDoOutput(boolean dooutput) static void setFileNameMap(FileNameMap map) void setIfModifiedSince(long ifmodifiedsince) void setRequestProperty(String key, String value) void setUseCaches(boolean usecaches) String toString() static void static void Class URLStreamHandler
extends Object
The class URLStreamHandler is the abstract base class for all protocol stream handlers. A protocol stream handler determines how to handle a specific network protocol and deal with specific data types on the stream.
Constructors
URLStreamHandler ()
Methods
protected String
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
openConnection(URL u) parseURL(URL u, String spec,int start, int limit) setURL(URL u, String protocol, String host, int port, String file, String ref) toExternalForm(URL u)
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
InetAddress methods JarURLConnection constructors fields methods java.net ContentHandler ContentHandler, constructors ContentHandler, methods DatagramkSocket DatagramPacket DatagramPacket, methods DatagramSocket, constructors DatagramSocket, methods 2nd HttpURLConnection HttpURLConnection, constructors HttpURLConnection, fields 2nd HttpURLConnection, methods InetAddress InetAddress, methods JarURLConnection JarURLConnection, constructors JarURLConnection, fields JarURLConnection, methods MulticastSocket 2nd MulticastSocket, constructors MulticastSocket, methods PasswordAuthentication PasswordAuthentication, constructors PasswordAuthentication, methods ServerSocket ServerSocket, constructors ServerSocket, methods Socket Socket, constructors Socket, methods 2nd SocketImpl SocketImpl, constructors SocketImpl, fields SocketImpl, methods 2nd URL URL, constructors URL, methods 2nd URLConnection URLConnection, constructors URLConnection, fields URLConnection, methods 2nd URLStreamHandler URLStreamHandler, constructors URLStreamHandler, methods MulticastSocket 2nd constructors methods PasswordAuthentication constructors methods ServerSocket constructors methods Socket constructors methods 2nd SocketImpl
constructors fields methods 2nd URL constructors methods 2nd URLConnection constructors fields methods 2nd URLStreamHandler constructors methods concrete factory ContentHandlerFactory class 2nd SocketImplFactory class constructors ContentHandler class DatagramSocket class HttpURLConnection class JarURLConnection class MulticastSocket class PasswordAuthentication class ServerSocket class Socket class SocketImpl class URL class URLConnection class URLStreamHandler class ContentHandler class constructors methods ContentHandlerFactory interface methods DatagramkSocket class DatagramPacket class methods datagrams sockets sending and receiving DatagramSocket class constructors methods 2nd factories abstract concrete ContentHandlerFactory class 2nd SocketImplFactory class fields HttpURLConnection class 2nd JarURLConnection class SocketImpl class SocketOptions interface URLConnection class files jar JarURLConnection class getContent() method getHostByName() method getLocalHost() method getOutputStream() method getSomeObject() method HttpURLConnection class constructors fields 2nd
methods InetAddress class methods interfaces ContentHandlerFactory methods java.net ContentHandlerFactory ContentHandlerFactory, methods SocketImplFactory SocketImplFactory, methods SocketOptions SocketOptions, fields SocketOptions, methods SocketImplFactory methods SocketOptions fields methods IP addresses InetAddress class jar files JarURLConnection class JarURLConnection class constructors fields methods java.net classes ContentHandler ContentHandler, constructors ContentHandler, methods DatagramkSocket DatagramPacket DatagramPacket, methods DatagramSocket, constructors DatagramSocket, methods 2nd HttpURLConnection HttpURLConnection, constructors HttpURLConnection, fields 2nd HttpURLConnection, methods InetAddress InetAddress, methods JarURLConnection JarURLConnection, constructors JarURLConnection, fields JarURLConnection, methods MulticastSocket 2nd MulticastSocket, constructors MulticastSocket, methods PasswordAuthentication PasswordAuthentication, constructors PasswordAuthentication, methods ServerSocket ServerSocket, constructors ServerSocket, methods Socket Socket, constructors Socket, methods 2nd SocketImpl SocketImpl, constructors SocketImpl, fields SocketImpl, methods 2nd URL URL, constructors
URL, methods 2nd URLConnection URLConnection, constructors URLConnection, fields URLConnection, methods 2nd URLStreamHandler URLStreamHandler, constructors URLStreamHandler, methods constructors ContentHandler class DatagramSocket class HttpURLConnection class JarURLConnection class MulticastSocket class PasswordAuthentication class ServerSocket class Socket class SocketImpl class URL class URLConnection class URLStreamHandler class ContentHandler class constructors methods ContentHandlerFactory interface methods DatagramkSocket class DatagramPacket class methods DatagramSocket class constructors methods 2nd fields HttpURLConnection class 2nd JarURLConnection class SocketImpl class SocketOptions interface URLConnection class HttpURLConnection class constructors fields 2nd methods InetAddress class methods interfaces ContentHandlerFactory ContentHandlerFactory, methods SocketImplFactory SocketImplFactory, methods SocketOptions SocketOptions, fields SocketOptions, methods JarURLConnection class constructors fields methods methods ContentHandler class ContentHandlerFactory interface DatagramPacket class DatagramSocket class 2nd HttpURLConnection class InetAddress class JarURLConnection class MulticastSocket class
PasswordAuthentication class ServerSocket class Socket class 2nd SocketImpl class 2nd SocketImplFactory interface SocketOptions interface URL class 2nd URLConnection class 2nd URLStreamHandler class MulticastSocket class 2nd constructors methods PasswordAuthentication class constructors methods ServerSocket class constructors methods Socket class constructors methods 2nd SocketImpl class constructors fields methods 2nd SocketImplFactory interface methods SocketOptions interface fields methods URL class constructors methods 2nd URLConnection class constructors fields methods 2nd URLStreamHandler class constructors methods methods accept() 2nd ContentHandler class ContentHandlerFactory interface DatagramPacket class DatagramSocket class 2nd getContent() getHostByName() getLocalHost() getOutputStream() getSomeObject() HttpURLConnection class InetAddress class JarURLConnection class MulticastSocket class PasswordAuthentication class ServerSocket class Socket class 2nd SocketImpl class 2nd SocketImplFactory interface SocketOptions interface URL class 2nd URLConnection class 2nd URLStreamHandler class
MulticastSocket class 2nd constructors methods packages java.net ContentHandler class ContentHandler class, constructors ContentHandler class, methods ContentHandlerFactory interface ContentHandlerFactory interface, methods DatagramkSocket class DatagramPacket class DatagramPacket class, methods DatagramSocket class, constructors DatagramSocket class, methods 2nd HttpURLConnection class HttpURLConnection class, constructors HttpURLConnection class, fields 2nd HttpURLConnection class, methods InetAddress class InetAddress class, methods JarURLConnection class JarURLConnection class, constructors JarURLConnection class, fields JarURLConnection class, methods MulticastSocket class 2nd MulticastSocket class, constructors MulticastSocket class, methods PasswordAuthentication class PasswordAuthentication class, constructors PasswordAuthentication class, methods ServerSocket class ServerSocket class, constructors ServerSocket class, methods Socket class Socket class, constructors Socket class, methods 2nd SocketImpl class SocketImpl class, constructors SocketImpl class, fields SocketImpl class, methods 2nd SocketImplFactory interface SocketImplFactory interface, methods SocketOptions interface SocketOptions interface, fields SocketOptions interface, methods URL class URL class, constructors URL class, methods 2nd URLConnection class URLConnection class, constructors URLConnection class, fields URLConnection class, methods 2nd URLStreamHandler class URLStreamHandler class, constructors URLStreamHandler class, methods PasswordAuthentication class constructors methods ports specifying protocol stream handlers security authenticating users users
authenticating separators (!/) ServerSocket class constructors methods Socket class constructors methods 2nd SocketImpl class constructors fields methods 2nd SocketImplFactory interface methods SocketOptions interface fields methods sockets datagrams sending and receiving options setting or getting streams protocol stream handlers URL class constructors methods 2nd URLConnection class constructors fields methods 2nd URLs parts of 2nd types of Web site URLStreamHandler class constructors methods users authenticating Web sites URLs types of
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019244205063050189125034133
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019246059042172093129255103
Interfaces
Remote
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019246059042174164123166081
Classes
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019246060213191000110238031
Exceptions
AccessException AlreadyBoundException ConnectException ConnectIOException MarshalException NoSuchObjectException NotBoundException RemoteException RMISecurityException ServerError ServerException ServerRuntimeException StubNotFoundException UnexpectedException UnknownHostException UnmarshalException
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 28. java.rmi > Interface and Class Details < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019246060213190138051065138
public abstract interface Remote The Remote interface must be extended by a remote interface. The Remote interface is the interface that defines the remotely accessible methods for a class. Generally that class implements the Remote interface and extends UnicastRemoteObject or extends Activatable, like this:
public interface Issueable extends Remote { // Remote method signatures. } public LibraryCard extends UnicastRemoteObject Implements Issueable { // Code to implement Issueable interface and do anything else // necessary, including specifying a constructor that throws // RemoteException. } This is a flag to the JVM to say that the interface is a remote interface. Remote does not declare any methods. Class MarshalledObject
A MarshalledObject instance contains a serialized version of the object passed in the marshalled object's constructor. The serialized object is used in marshalling and unmarshalling remote method parameters. The serialized object may have a codebase URL associated with it in the MarshalledObject instance. A MarshalledObject is needed to provide data values for initializing an Activatable object because there's no way to pass values for data members in the constructor of an Activatable object. All local objects used as parameters in remote object calls are marshalled with a MarshalledObject. In the following example, the MarshalledObject takes a single String parameter:
// Create a MarshalledObject to pass parameters // to the Book constructor. MarshalledObject mo = new MarshalledObject ("How Shall We Then Live?");
Constructors
Class Naming
The Naming class provides methods for binding or unbinding an object in the RMIRegistry so that other objects outside the JVM doing the bind operation can find the object. Naming also provides the mechanism Naming.lookup() to locate a remote object. The result of doing a lookup() is a remote object reference to an object that implements the remote interface referred to in the lookup() call. Using the naming service in RMI is much simpler than in CORBA. Since everything in RMI is in Java, there is no need to narrow references, and you don't need to narrow the reference to the naming service, the RMIRegistry, either. Here are two examples of using the Naming class. When registering a remote object in the RMIRegistry, you write code like this:
Book remoteBook = new Book("How Shall We Then Live?"); // Instantiate the remote object. To the caller // object, this is just another local object. Naming.rebind("RemoteBook", remoteBook); The name RemoteBook in quotes is the logical name by which a client can search for a remote reference to this object in the RMIRegistry. Its value can be anything you like, but it is helpful if it corresponds in some close way to the name of the class. Tip Although you could call bind() in the preceding code snippet, that is generally not done. You should use rebind(). The reason is that if the object was already bound and you call bind() again, you will get an exception for trying to bind an already bound object. If you call rebind(), you will not get this exception, no matter how many times you call rebind().
Note Technically, you do not have to bind an object to the RMIRegistry for it to be accessed as a remote object. If you have subclassed UnicastRemoteObject, you can instead do an exportObject() call. This is not the preferred method, but there are times when it is the best approach. In fact, in some cases, just the act of instantiating an object that extends UnicastRemoteObject is enough to make your object available for remote classes. However, this is somewhat more complicated than using the RMIRegistry.
Over on the client, you might call the lookup() method of the Naming class like this:
String server = "localhost"; String rmiUrl = "rmi://"; String remoteClass = "RemoteBook"; rmiUrl = rmiUrl + server + "/" + remoteClass; Borrowable remoteBook = (Borrowable)Naming.lookup(rmiUrl);
Methods
bind(String name, Remote obj) list(String name) lookup(String name) rebind(String name, Remote obj) unbind(String name)
Class RMISecurityManager
public class RMISecurityManager extends SecurityManager The RMISecurityManager is required to access remote methods and download stubs or activate remote objects. There are many limitations provided by the RMISecurityManager that are not provided for by the default java.policy file (JAVA_HOME/jre/lib/security). Generally, for RMI, you must create a policy file that grants more permissions. In particular, you must grant permission for seeing more system properties and for accessing a larger group of port numbers. You create an RMISecurityManager in your code by doing this:
System.setSecurityManager(new RMISecurityManager());
Constructors
RMISecurityManager()
Methods
void
checkPackageAccess(String pkgname)
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
constructors methods lookup() method 2nd MarshalledObject class constructors methods methods bind() exportObject() lookup() 2nd MarshalledObject class Naming class rebind() remote accessing RMISecurityManager class Naming class methods objects remote activating RMIRegistry binding binding or unbinding serialized packages java.rmi MarshalledObject class MarshalledObject class, constructors MarshalledObject class, methods Naming class Naming class, methods Remote interface 2nd RMISecurityManager class RMISecurityManager class, constructors RMISecurityManager class, methods rebind() method Remote interface 2nd remote methods accessing remote objects activating RMIRegistry objects binding binding or unbinding RMISecurityManager class constructors methods serialized objects stubs downloading
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 29. java.rmi.server and java.rmi.activation < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019246060213190129197111140
All Books
Search
Java > PURE Java 2 > 29. java.rmi.server and java.rmi.activation > Package Name: java.rmi.activation < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019246062077212054090048167
All Books
Search
Java > PURE Java 2 > 29. java.rmi.server and java.rmi.activation > Package Name: java.rmi.server < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019246062077213029064097098
LoaderHandler (deprecated) RemoteCall (deprecated) RemoteRef RMIClientSocketFactory RMIFailureHandler RMIServerSocketFactory ServerRef Skeleton (deprecated) Unreferenced Classes
LogStream (deprecated) ObjID Operation (deprecated) RemoteObject RemoteServer RemoteStub RMIClassLoader RMISocketFactory UID UnicastRemoteObject Exceptions
All Books
Java > PURE Java 2 > 29. java.rmi.server and java.rmi.activation > java.rmi.server Interface and Class Details < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019246062077213030138199200
public abstract interface RemoteRef extends Externalizable A RemoteRef object is a handle to a remote object. It's used by a RemoteStub to call a method on a remote object.
Fields
packagePrefix serialVersionUID
done(RemoteCall call) (Deprecated) getRefClass(ObjectOutput out) invoke(RemoteCall call) (Deprecated) invoke(Remote obj, Method method, Object[] params, long opnum) newCall(RemoteObject obj, Operation[] op, int opnum, long hash) (Deprecated) remoteEquals(RemoteRef obj) remoteHashCode() remoteToString()
Interface RMIClientSocketFactory
public abstract interface RMIClientSocketFactory An RMIClientSocketFactory interface defines the method for creating an RMICLientSocket, which is passed to a client when it uses a remote reference to a remote object. The RMIClientSocket may be downloaded to the client. Note You can define your own custom socket factories. For example, you might want to use sockets that do compression or encryption. See http://java.sun.com/products/jdk/1.2/docs/guide/rmi/rmisocketfactory.doc.html
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=204 (1 of 10) [6/2/2002 11:33:27 PM]
Methods
Socket
Interface RMIServerSocketFactory
public abstract interface RMIServerSocketFactory An RMIServerSocketFactory interface is used to obtain server sockets for an RMI call to a remote object.
Methods
ServerSocket
createServerSocket(int port)
Interface ServerRef
public abstract interface ServerRef extends RemoteRef ServerRef provides a handle to server-side remote object references for use in remote object calls.
Fields
static long
Methods
serialVersionUID
RemoteStub String
Class RemoteObject
The class RemoteObject provides remote versions of the methods for java.lang.Object: toString(), equals(), and hashCode(). If you do not subclass UnicastRemoteObject in your remote objects, you need to implement remote versions of these methods yourself. Applications generally do not use RemoteObject directly.
Fields
protected RemoteRef
Constructors
ref
protected protected
Methods
Class RMIClassLoader
The class RMIClassLoader is a core piece of RMI and is involved in loading classes and stubs from one host to another. Applications generally do not use this class directly, but RMIClassLoader is involved in loading RMI classes into the JVM.
Methods
getClassAnnotation(Class cl) getSecurityContext(ClassLoader loader) (Deprecated) loadClass(String name) (Deprecated) loadClass(String codebase, String name) loadClass(URL codebase, String name)
Class RemoteServer
The class RemoteServer is the immediate, abstract superclass for Activatable and UnicastRemoteObject. RemoteServer provides the framework for crucial RMI activities such as creating and exporting remote objects. A remote object must be exported before it can be accessed remotely. This happens automatically when you subclass Activatable or UnicastRemoteObject. Applications generally do not use this class directly.
Constructors
protected protected
Methods
Class UnicastRemoteObject
The class UnicastRemoteObject serves as the base class for most user-defined remote object classes. By subclassing UnicastRemoteObject, you obtain several important methods. If you do not extend UnicastRemoteObject, you must export your objects yourself and provide a remote implementation of hashCode(), equals(), and toString() because the versions inherited from java.lang.Object do not support remote objects. Here is a simple example of using this class:
class LibraryCard extends UnicastRemoteObject implements Issueable { // class definition goes here. }
Constructors
protected protected
static boolean
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
clone() exportObject(Remote obj) exportObject(Remote obj, int port) exportObject(Remote obj, int port, RMIClientSocketFactory csf, RMIServerSocketFactory ssf) unexportObject(Remote obj, boolean force)
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
ServerRef interface interfaces java.rmi.server RemoteRef RemoteRef, fields RemoteRef, methods RMIClientSocketFactory RMIClientSocketFactory, methods RMIServerSocketFactory RMIServerSocketFactory, methods ServerRef ServerRef, fields ServerRef, methods RemoteRef fields methods RMIClientSocketFactory methods RMIServerSocketFactory methods ServerRef fields methods java.lang.Object RemoteObject class methods java.rmi.server classes RemoteObject RemoteObject, constructors RemoteObject, fields RemoteObject, methods RemoteServer RemoteServer, constructors RemoteServer, methods RMIClassLoader RMIClassLoader, methods UnicastRemoteObject UnicastRemoteObject, constructors UnicastRemoteObject, methods constructors RemoteObject class RemoteServer class UnicastRemoteObject class fields RemoteObject class RemoteRef interface ServerRef interface interfaces RemoteRef RemoteRef, fields RemoteRef, methods RMIClientSocketFactory RMIClientSocketFactory, methods RMIServerSocketFactory RMIServerSocketFactory, methods ServerRef ServerRef, fields ServerRef, methods
methods RemoteObject class RemoteRef interface RemoteServer class RMIClassLoader class RMIClientSocketFactory interface RMIServerSocketFactory interface ServerRef interface UnicastRemoteObject class RemoteObject class constructors fields methods RemoteRef interface fields methods RemoteServer class constructors methods RMIClassLoader class methods RMIClientSocketFactory interface methods RMIServerSocketFactory interface methods ServerRef interface fields methods UnicastRemoteObject class constructors methods methods remote java.lang.Object RemoteObject class RemoteRef interface RemoteServer class RMIClassLoader class RMIClientSocketFactory interface RMIServerSocketFactory interface ServerRef interface UnicastRemoteObject class packages java.rmi.server RemoteObject class RemoteObject class, constructors RemoteObject class, fields RemoteObject class, methods RemoteRef interface RemoteRef interface, fields RemoteRef interface, methods RemoteServer class RemoteServer class, constructors RemoteServer class, methods RMIClassLoader class RMIClassLoader class, methods RMIClientSocketFactory interface RMIClientSocketFactory interface, methods RMIServerSocketFactory interface
RMIServerSocketFactory interface, methods ServerRef interface ServerRef interface, fields ServerRef interface, methods UnicastRemoteObject class UnicastRemoteObject class, constructors UnicastRemoteObject class, methods RemoteObject class constructors fields methods methods for java.lang.Object RemoteRef interface fields methods RemoteServer class constructors methods RMI custom socket factories Web site tutorial RMIClassLoader class methods RMIClientSocketFactory interface methods RMIServerSocketFactory interface methods ServerRef interface fields methods socket factories RMI custom Web site tutorial UnicastRemoteObject class constructors methods Web sites Java RMI custom socket factories tutorial RMI custom socket factories tutorial
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 29. java.rmi.server and java.rmi.activation > java.rmi.activation Class Details < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019246062077214110208232100
The Activatable class is the abstract base class you need to subclass to define a class that can have an instance remotely activated (that is, instantiated). The role of Activatable for a remote object is similar in this regard to
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=205 (1 of 10) [6/2/2002 11:33:38 PM]
the role of UnicastRemoteObject for a remote object. Here is a code snippet to show this class being used as a superclass:
public class ActivatableBook extends Activatable implements Borrowable { // Define class here. } You would make a class that extends Activatable, as opposed to UnicastRemoteObject, if it is important for a client to create remote objects on-the-fly without requiring one to already exist on the server system.
Constructors
Activatable(ActivationID id, int port) Activatable(ActivationID id, int port, RMIClientSocketFactory csf, RMIServerSocketFactory ssf) Activatable(String location, MarshalledObject data, boolean restart, int port) Activatable(String location, MarshalledObject data, boolean restart, int port, RMIClientSocketFactory csf, RMIServerSocketFactory ssf)
Methods
protected ActivationID static boolean static Remote static boolean static void Class ActivationDesc
exportObject(Remote obj, ActivationID id, int port) exportObject(Remote obj, ActivationID id, int port, RMIClientSocketFactory csf, RMIServerSocketFactory ssf) exportObject(Remote obj, String location, MarshalledObject data, boolean restart, int port) exportObject(Remote obj, String location, MarshalledObject data, boolean restart, int port, RMIClientSocketFactory csf, RMIServerSocketFactory ssf) getID() inactive(ActivationID id) register(ActivationDesc desc) unexportObject(Remote obj, boolean force) unregister(ActivativationID id)
This class provides objects that describe "activatable" objects in terms of the information needed to activate them. Here's what an ActivationDesc instance contains:
q q q q
The activation group The class name The class file location A marshalled object. This can be used to initialize fields of the activated object. It's needed because the constructor for an Activatable object does not permit custom parameters.
ActivationDesc desc = new ActivationDesc (actGrID, "ActivatableBook", // Activation group, class name. location, mo); // Class file location and // MarshalledObject instance. // Give rmid enough info to instantiate // the activatable object.
Constructors
ActivationDesc (ActivationGroupID groupID, String className, String location, MarshalledObject data) ActivationDesc(ActivationGroupID groupID, String className, String location, MarshalledObject data, boolean restart) ActivationDesc(String className, String location, MarshalledObject data) ActivationDesc(String className, String location, MarshalledObject data, boolean restart)
Methods
boolean String
publicabstract class ActivationGroup extends UnicastRemoteObject implements ActivationInstantiator An ActivationGroup object is responsible for instantiating the Activatable classes in the group. It notifies an ActivationMonitor of the status of the attempt to create the instance. The following code snippet shows how to get an ActivationGroup:
ActivationGroup.createGroup(actGrID, bookGroup, 0); // This method takes three parms: // ActivationGroupID id // ActivationGroupDesc desc // long incarnation
Constructors
protected
Methods
ActivationGroup(ActivationGroupID groupID)
protected void abstract void static ActivationGroup static ActivationGroupID static ActivationSystem protected void boolean static void Class ActivationGroupDesc
activeObject(ActivationID id, MarshalledObject mobj) activeObject(ActivationID id, Remote obj) createGroup(ActivationGroupID id, ActivationGroupDesc desc, long incarnation) currentGroupID() getSystem() inactiveGroup() inactiveObject(ActivationID id) setSystem(ActivationSystem system)
public final class ActivationGroupDesc extends Object implements Serializable An ActivationGroupDesc (activation group descriptor) object contains the information necessary to create an ActivationGroup instance.
The class name for the ActivationGroup The location, or code source, for the ActivationGroup's class definition A MarshalledObject instance that contains data for initializing the ActivationGroup object
ActivationGroupDesc.CommandEnvironment actCmdEnv = null; ActivationGroupDesc bookGroup = new ActivationGroupDesc(sysProps, actCmdEnv); The ActivationGroup is created by calling the ActivationGroup.createGroup() method, which invokes a constructor.
Constructors
ActivationGroupDesc (Properties overrides, ActivationGroupDesc.CommandEnvironment cmd) ActivationGroupDesc(String className, String location, MarshalledObject data, Properties overrides, ActivationGroupDesc.CommandEnvironment cmd)
Methods
public class ActivationID extends Object implements Serializable An ActivationID (activation identifier) object contains data for identifying a remote object that can be activated. This includes a unique identifier for the activatable object and a remote reference to the object's activator. An ActivationID can be obtained in multiple ways, including
q q q
Calling Activatable.register() Calling Activatable.exportObject() Calling the Activatable constructor, which both activates and exports an object
A remote reference to the activator for the object A unique identifier for the activatable object
Constructors
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
ActivationDesc class constructors methods ActivationGroup class constructors methods ActivationGroupDesc class constructors methods ActivationID class constructors methods classes Activatable Activatable, constructors Activatable, methods 2nd ActivationDesc ActivationDesc, constructors ActivationDesc, methods ActivationGroup ActivationGroup, constructors ActivationGroup, methods ActivationGroupDesc ActivationGroupDesc, constructors ActivationGroupDesc, methods ActivationID ActivationID, constructors ActivationID, methods constructors Activatable class ActivationDesc class ActivationGroup class ActivationGroupDesc class ActivationID class methods Activatable class 2nd ActivationDesc class ActivationGroup class ActivationGroupDesc class ActivationID class methods Activatable class 2nd ActivationDesc class ActivationGroup class ActivationGroup.createGroup() ActivationGroupDesc class ActivationID class packages java.rmi.activation Activatable class Activatable class, constructors Activatable class, methods 2nd ActivationDesc class ActivationDesc class, constructors ActivationDesc class, methods ActivationGroup class ActivationGroup class, constructors ActivationGroup class, methods ActivationGroupDesc class ActivationGroupDesc class, constructors ActivationGroupDesc class, methods
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019246062077215217060156215
All Books
Search
Java > PURE Java 2 > 30. java.security > Package Name: java.security < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019246063220010129062199198
AccessControlContext AccessController AlgorithmParameterGenerator AlgorithmParameterGeneratorSpi AlgorithmParameters AlgorithmParametersSpi AllPermission BasicPermission CodeSource DigestInputStream DigestOutputStream GuardedObject Identity IdentityScope KeyFactory KeyFactorySpi KeyPair KeyPairGenerator KeyPairGeneratorSpi KeyStore KeyStoreSpi MessageDigest MessageDigestSpi Permission
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=208 (1 of 3) [6/2/2002 11:34:51 PM]
PermissionCollection Permissions Policy ProtectionDomain Provider SecureClassLoader SecureRandom SecureRandomSpi Security SecurityPermission Signature SignatureSpi SignedObject Signer UnresolvedPermission Exceptions
AccessControlException DigestException GeneralSecurityException InvalidAlgorithmParameterException InvalidKeyException InvalidParameterException KeyException KeyManagementException KeyStoreException NoSuchAlgorithmException NoSuchProviderException PrivilegedActionException ProviderException SignatureException UnrecoverableKeyException
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
Java > PURE Java 2 > 30. java.security > Package Name: java.security.cert < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019246063220010133088173020
X509Extension Classes
All Books
Search
Java > PURE Java 2 > 30. java.security > java.security.cert Interface and Class Details < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019246063220011254070208202
public abstract interface Key extends Serializable The Key interface is the base interface from which several subinterfaces extend. The Key interface is never implemented directly. This interface defines common features of all Key implementations. All Key implementations have the following items:
q q q
An algorithmThe algorithm for generating the key An encoded formThe form of the key used for transmitting it outside of a JVM A formatThe name of the format of the encoded key
Fields
static long
Methods
serialVersionUID
Class AllPermission
The AllPermission class is a permission that implies all other permissions. Although this may be convenient for testing, you probably don't want to use this in a production environment, because it grants to code various permissions, including SocketPermission for all sockets, FilePermission for all operations, PropertyPermission for all properties, and others. In general, you want a more finely tuned set of permissions. That's why protection domains with permissions exit. They permit finely tuned sets of permissions for specific programs, code signers, or code domains. You normally set permissions in a policy file using policytool. However, it is possible to check for a given permission within your code. For example, you could write the following:
AllPermission ap = new AllPermission(); AccessController.checkPermission(ap); // If the checkPermission method determines that All Permission // is granted, it simply returns. If it determines that // AllPermission is not granted, this method throws an // AccessControlException.
Constructors
The CodeSource class describes a codebase that includes both code and the certificates of signers of that code, if any.
Constructors
This class is a simple holder for a key pair, which consists of a public key and a private key. A KeyPair object provides no security for its contents, so if it's serialized, it should probably be enciphered or treated with care like a private key. You can generate a pair of keys in Java using the keytool program. You might invoke it like this:
keytool -genkey -alias JavaMaster -keystore PureJava -storepass mykeys This command tells Java to generate a key pair and store it in a key/certificate database called PureJava, which belongs to the alias JavaMaster and is password-protected with the password mykeys.
Constructors
getPrivate() getPublic()
A KeyStore object, like its file-based counterpart (created with the keytool program), contains keys and certificates in memory. It has two types of entries:
q
A key entryAn encryption key stored in protected format to prevent unauthorized access. Generally, this is a private key, which would be stored with an associated certificate. The key/certificate pair would be suitable for self-signed code. Generally, this code and its related key/certificate pair would be used within an organization rather than across the Internet, where a certificate from a certificate authority (such as Verisign) is much more common and more appropriate. Because a key relates to signing code and the certificate authenticates the digital signature, you should generally not accept a self-signed certificate as sufficient authentication, nor should you expect others outside your organization to do so, as a rule. A trusted certificate entryThis is typically a public key certificate (the certifi cate contains the public key) that is trusted. In other words, the certificate is trusted by the keystore owner.
Entries in a keystore are identified by an alias, which is what goes into the signedBy field when you're creating a policy file for signed code. There's no specification here regarding how to make a KeyStore object persistent. This means implementers have great flexibility to use varying secure techniques.
Constructors
protected
Methods
aliases() containsAlias(String alias) deleteEntry(String alias) getCertificate(String alias) getCertificateAlias(Certificate cert) getCertificateChain(String alias) getCreationDate(String alias) getDefaultType()
static KeyStore static KeyStore Key Provider String boolean boolean void void void void int void Class MessageDigest
getInstance(String type) getInstance(String type, String provider) getKey(String alias, char[] password) getProvider() getType() isCertificateEntry(String alias) isKeyEntry(String alias) load(InputStream stream, char[] password) setCertificateEntry(String alias, Certificate cert) setKeyEntry(String alias, byte[] key, Certificate[] chain) setKeyEntry(String alias, Key key, char[] password, Certificate[] chain) size() store(OutputStream stream, char[] password)
The MessageDigest class implements a message digest. A message digest is essentially a hash value of a message sent across the Internet. Computationally, no two messages should create the same message digest (and it has been calculated that the sun will burn out before someone can determine the message content from the message digest). A message digest can be used to verify whether a message has been changed. If the recipient of the message creates a MessageDigest object in the same way and the message digest hash
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=210 (6 of 14) [6/2/2002 11:35:08 PM]
value is different from that sent in the message, you know the message was changed. It's entirely possible, however, that someone could intercept the message in transit, change it, and create a new message digest value. Therefore, by itself, a MessageDigest object is not a guarantee of message integrity. If you receive a message and then contact the sender and compare the sender's copy of the message digest with the message digest you received with the message, you can determine with confidence whether the original message was changed in transit. If both message digests match, the message was not changed. Here's a code snippet that shows the basics of making a digest for a pre-existing file:
MessageDigest md = MessageDigest.getInstance("md"); File theDoc = new File("MessageDoc.txt"); FileInputStream fis = new FileInputStream(theDoc); byte[] document = new byte[(int)theDoc.length()]; fis.read(document); md.update(document); byte[] mdDigest = md.digest(); This creates a MessageDigest object for a document called MessageDoc.txt.
Constructors
protected
Methods
MessageDigest(String algorithm)
Object byte[] byte[] int String int static MessageDigest static MessageDigest Provider static boolean void String void void void Class Permission
clone() digest() digest(byte[] input) digest(byte[] buf, int offset, int len) getAlgorithm() getDigestLength() getInstance(String algorithm) getInstance(String algorithm, String provider) getProvider() isEqual(byte[] digesta, byte[] digestb) reset() toString() update(byte input) update(byte[] input) update(byte[] input, int offset, int len)
This is the abstract superclass of several other Permission classes, such as FilePermission. Permissions are objects that may, if appropriate, optionally include actions. A protection domain consists of code and its related permission objects. You can grant permissions through code in some cases, but in general you grant permissions defined by the Java 2 SDK through a policy file, generated by the policytool.
Constructors
void abstract boolean abstract String String abstract int abstract boolean PermissionCollection String Class Policy
checkGuard(Object object) equals(Object obj) getActions() getName() hashCode() implies(Permission permission) newPermissionCollection() toString()
A Java Policy object represents the combination of permissions granted to an application in a specific protection domain. These permissions can be generated in a number of ways, including through the policytool, in which fine-grained permissions can be granted to all code, to a specific code domain, or to one or more code signers. There's only one Policy object per JVM, and it affects all the code in that JVM. You can obtain the policy object for a protection domain, along with its permissions, by coding something like this, given the CodeSource object:
Policy ()
Methods
abstract PermissionCollection static Policy abstract void static void Class ProtectionDomain
The ProtectionDomain class represents a group of classes and their associated permissions. Every program runs within its own protection domain.
Constructors
AllPermission, constructors AllPermission, methods CodeSource CodeSource, constructors CodeSource, methods KeyPair KeyPair, constructors KeyPair, methods KeyStore KeyStore, constructors KeyStore, methods 2nd MessageDigest MessageDigest, constructors MessageDigest, methods 2nd Permission Permission, constructors Permission, methods Policy Policy, constructors Policy, methods ProtectionDomain ProtectionDomain, constructors ProtectionDomain, methods KeyPair constructors methods KeyStore constructors methods 2nd MessageDigest constructors methods 2nd Permission constructors methods Policy constructors methods ProtectionDomain constructors methods CodeSource class constructors methods constructors AllPermission class CodeSource class KeyPair class KeyStore class MessageDigest class Permission class Policy class ProtectionDomain class digests MessageDigest class entries aliases
KeyStore object 2nd trusted certificate fields Key interface interfaces java.security.cert Key Key, fields Key, methods Key fields methods java.security.cert AllPermission class constructors methods classes AllPermission AllPermission, constructors AllPermission, methods CodeSource CodeSource, constructors CodeSource, methods KeyPair KeyPair, constructors KeyPair, methods KeyStore KeyStore, constructors KeyStore, methods 2nd MessageDigest MessageDigest, constructors MessageDigest, methods 2nd Permission Permission, constructors Permission, methods Policy Policy, constructors Policy, methods ProtectionDomain ProtectionDomain, constructors ProtectionDomain, methods CodeSource class constructors methods constructors AllPermission class CodeSource class KeyPair class KeyStore class MessageDigest class Permission class Policy class ProtectionDomain class fields Key interface interfaces Key Key, fields
Key, methods Key interface fields methods KeyPair class constructors methods KeyStore class constructors methods 2nd MessageDigest class constructors methods 2nd methods AllPermission class CodeSource class Key interface KeyPair class KeyStore class 2nd MessageDigest class 2nd Permission class Policy class ProtectionDomain class Permission class constructors methods Policy class constructors methods ProtectionDomain class constructors methods Key interface fields methods KeyPair class constructors methods KeyStore class constructors methods 2nd KeyStore object entries 2nd MessageDigest class constructors methods 2nd methods AllPermission class CodeSource class Key interface KeyPair class KeyStore class 2nd MessageDigest class 2nd Permission class Policy class ProtectionDomain class objects
KeyStore entries 2nd packages java.security.cert AllPermission class AllPermission class, constructors AllPermission class, methods CodeSource class CodeSource class, constructors CodeSource class, methods Key interface Key interface, fields Key interface, methods KeyPair class KeyPair class, constructors KeyPair class, methods KeyStore class KeyStore class, constructors KeyStore class, methods 2nd MessageDigest class MessageDigest class, constructors MessageDigest class, methods 2nd Permission class Permission class, constructors Permission class, methods Policy class Policy class, constructors Policy class, methods ProtectionDomain class ProtectionDomain class, constructors ProtectionDomain class, methods Permission class constructors methods permissions AllPermission class Policy class constructors methods policytool 2nd 3rd ProtectionDomain class constructors methods security permissions AllPermission class tools policytool 2nd 3rd
2002, O'Reilly & Associates, Inc.
All Books
Search
Java > PURE Java 2 > 30. java.security > java.security.cert Class Details < BACK Make Note | Bookmark
158122020158178182047242169203138024176246252243131023021084156019246048069097029024248170
The Certificate class is an abstract base class for a number of concrete Certificate classes. A certificate serves to validate that a digital signature belongs to the alleged signer, such as an individual or company. Certificates can be self-generated or obtained from a certificate authority (CA). A self-generated certificate would be appropriate for an intranet application. For an applet from an external source, in general, you would only want to accept a certificate from a certificate authority. You should also be aware that a certificate may be of little or great worth. It's possible to buy a certificate from a CA for a very small amount based on supplying only an email address to verify the signer's identity. Other certificates, which cost much more, are much more reliable. Accepting a certificate as valid is a matter of trust.
Constructors
protected
Methods
Certificate(String type)
boolean abstract abstract String int abstract abstract abstract Class CRL
byte[] PublicKey
equals(Object other) getEncoded() getPublicKey() getType() hashCode() toString() verify(PublicKey key) verify(PublicKey key, String sigProvider)
The CRL class is the abstract base class for all concrete Certificate Revocation List (CRL) classes, such as X509CRL. Most developers won't need to use this unless they're specifically involved in validating and invalidating certificates.
Constructors
protected
Methods
CRL(String type)
Class X509Certificate
The X509Certificate class is an abstract class that prescribes the methods for interacting with an X.509 certificate. This is a very common certificate format used for authenticating digital signatures. An X.509 contains several elements, including the signature, signature identifier, and signature algorithm. An X.509 certificate would be provided by someone or some company that digitally signed code or documents and sent them across the Internet, among other uses. This would also be the normal certificate type to use in an intranet for signed code or internally signed documents to verify the signature. The X509Certificate object is generated with a CertificateFactory.
Constructors
protected
Methods
X509Certificate()
abstract void
checkValidity()
abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract
void int Principal boolean[] boolean[] Date Date BigInteger String String byte[] byte[] Principal boolean[] byte[] int
checkValidity(Date date) getBasicConstraints() getIssuerDN() getIssuerUniqueID() getKeyUsage() getNotAfter() getNotBefore() getSerialNumber() getSigAlgName() getSigAlgOID() getSigAlgParams() getSignature() getSubjectDN() getSubjectUniqueID() getTBSCertificate() getVersion()
Class X509CRL
The X509CRL abstract class provides support for an X.509 Certificate Revocation List. An X509CRL object is a list with a timestamp of revoked X.509 certificates, signed by a certificate authority. You use a
FileInputStream fis = new FileInputStream("X509-CRList.dat"); CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); X509CRL x509CRL = (X509CRL)certFactory.generateCRL(fis); fis.close();
Constructors
protected
Methods
X509CRL()
boolean abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract abstract int abstract abstract
byte[] Principal Date X509CRLEntry Set String String byte[] byte[] byte[] Date int void void
equals(Object other) getEncoded() getIssuerDN() getNextUpdate() getRevokedCertificate(BigInteger serialNumber) getRevokedCertificates() getSigAlgName() getSigAlgOID() getSigAlgParams() getSignature() getTBSCertList() getThisUpdate() getVersion() hashCode() verify(PublicKey key) verify(PublicKey key, String sigProvider)
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
CRL (Class Revocation List), constructors CRL (Class Revocation List), methods X509Certificate X509Certificate, constructors X509Certificate, methods 2nd X509CRL X509CRL, constructors X509CRL, methods 2nd constructors Certificate class CRL (Class Revocation List) class X509Certificate class X509CRL class CRL (Class Revocation List) class constructors methods methods Certificate class CRL (Class Revocation List) class X509Certificate class 2nd X509CRL class 2nd X509Certificate class constructors methods 2nd X509CRL class constructors methods 2nd methods Certificate class CRL (Class Revocation List) class X509Certificate class 2nd X509CRL class 2nd packages java.security.cert Certificate class Certificate class, constructors Certificate class, methods CRL (Class Revocation List) class CRL (Class Revocation List) class, constructors CRL (Class Revocation List) class, methods X509Certificate class X509Certificate class, constructors X509Certificate class, methods 2nd X509CRL class X509CRL class, constructors X509CRL class, methods 2nd revoked X.509 certificates timestamp security certificates digital signatures 2nd revoked X.509 certificates timestamp X.509 certificates signatures digital certificates 2nd
timestamp revoked X.509 certificates X.509 certificates revoked timestamp X509Certificate class constructors methods 2nd X509CRL class constructors methods 2nd
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019246048069096216158055112
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019246048069099209148217202
Interfaces
Array Blob CallableStatement Clob Connection DatabaseMetaData Driver PreparedStatement Ref ResultSet ResultSetMetaData SQLData SQLInput SQLOutput Statement Struct
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019246048069099220040077008
Classes
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019246048069098109183227097
Exceptions
All Books
Search
Java > PURE Java 2 > 31. java.sql > Interface and Class Details < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019246048069098110116043100
public abstract interface Blob This interface provides the method definition for a Blob. This is the Java version of the SQL data type Blob, which maps a binary large object to a column in a relational table. Blobs can be useful in Java when you're working with a relational table to store an entire object. A Blob in Java differs from most SQL data types in that it stores a pointer to the data, rather than the data itself, in the column. The Blob is only valid during the transaction that created it. You can access a method through getBlob() and setBlob() in ResultSet, PreparedStatement, and CallableStatement.
Methods
getBinaryStream() getBytes(long pos, int length) length() position(Blob pattern, long start) position(byte[] pattern, long start)
Interface CallableStatement
public abstract interface CallableStatement extends PreparedStatement This interface declares the methods that can be used in JDBC to work with stored procedures. A stored procedure is one or more SQL statements (usually) stored in the database in a compiled form. When you create a CallableStatement object, you use this object to call the stored procedure, just as you would a method. CallableStatement indicates parameters in a stored procedure using the ? symbol. These parameters are numbered left to right, starting at 1. You use setxxx()methods to set the values of the parameters, and you use getxxx() methods to get back values from parameters of type OUT and from a result set, if CallableStatement returns one. JDBC supports IN and OUT parameters but not INOUT parameters. In the following code snippet, a stored procedure will be created and stored in the Books table of a library's RDBMS and then executed using a CallableStatement:
String storedProc = "create procedure SHOW_LEWIS_BOOKS" + "as " + "select TITLE, PUBLISHER, DATE " +
"from BOOKS" + "where AUTHOR = " + "'Lewis, C.S.'"; Statement stmt = conn.createStatement(); stmt.executeUpdate(storedProc); CallableStatement cs = con.prepareCall ("{call SHOW_LEWIS_BOOKS}"); ResultSet rs = cs.executeQuery(); // First, create the contents of a stored procedure, in this // case a SQL statement. Then put it in the database with // the executeUpdate() call. Then execute the stored // procedure by calling executeQuery on the // resulting CallableStatement.
Methods
Array BigDecimal BigDecimal Blob boolean byte byte[] Clob Date Date double float int long Object Object Ref short String Time Time Timestamp Timestamp void void void boolean
getArray(int i) getBigDecimal(int parameterIndex) getBigDecimal(int parameterIndex, int scale) (Deprecated) getBlob(int i) getBoolean(int parameterIndex) getByte(int parameterIndex) getBytes(int parameterIndex) getClob(int i) getDate(int parameterIndex) getDate(int parameterIndex, Calendar cal) getDouble(int parameterIndex) getFloat(int parameterIndex) getInt(int parameterIndex) getLong(int parameterIndex) getObject(int parameterIndex) getObject(int i, Map map) getRef(int i) getShort(int parameterIndex) getString(int parameterIndex) getTime(int parameterIndex) getTime(int parameterIndex, Calendar cal) getTimestamp(int parameterIndex) getTimestamp(int parameterIndex, Calendar cal) registerOutParameter(int parameterIndex, int sqlType) registerOutParameter(int parameterIndex, int sqlType, int scale) registerOutParameter(int paramIndex, int sqlType String typeName) wasNull()
Interface Connection
public abstract interface Connection The Connection interface provides method declarations for a database connection. A connection is required to communicate with a database. The normal means of getting an object that implements the Connection interface is
where dbURL is a URL that describes the information needed by the DriverManager object to locate an appropriate database JDBC driver and provides sufficient information for the driver to connect to the database system. It's critical that you be aware of the fact that, by default, a Connection object commits work after every single call. To prevent thiswhich you ought to doand commit when you think it's appropriate, call setAutoCommit(false). You should also be sure to set the isolation level by calling the following:
TheConnection.setTransactionIsolation (TRANSACTION_REPEATABLE_READ); // Set the transaction isolation level to Repeatable Read, // which means that dirty reads and nonrepeatable reads are // disallowed, but you may still get phantom rows.
Fields
void void void Statement Statement boolean String DatabaseMetaData int Map SQLWarning boolean boolean String CallableStatement CallableStatement PreparedStatement PreparedStatement void void void void void void Interface Driver
clearWarnings() close() commit() createStatement() createStatement(int resultSetType, int resultSetConcurrency) getAutoCommit() getCatalog() getMetaData() getTransactionIsolation() getTypeMap() getWarnings() isClosed() isReadOnly() nativeSQL(String sql) prepareCall(String sql) prepareCall(String sql, int resultSetType, int resultSetConcurrency) prepareStatement(String sql) prepareStatement(String sql, int resultSetType, int resultSetConcurrency) rollback() setAutoCommit(boolean autoCommit) setCatalog(String catalog) setReadOnly(boolean readOnly) setTransactionIsolation(int level) setTypeMap(Map map)
public abstract interface Driver The Driver interface specifies the methods a JDBC driver should implement. A JDBC driver is normally created and registered with DriverManager, like this:
Class.forName("MyDriver"); This causes the class definition to be loaded. As a rule, and in conformance with JDBCspecification, a Driver implementation class uses a static initializer to instantiate itself and register itself with DriverManager using the registerDriver() method. The use of the Driver interface allows a program to stay completely independent of the implementation of a Driver implementation class, so the class can be modified or even replaced by a different class without the program being changed at all, except possibly the Driver implementation classname. The Driver implementation is not specified and may vary greatly from vendor to vendor, both in logic and performance. JDBC drivers are of one of four types, and you should choose a driver based in part on this:
q q q q
Type 1JDBC-ODBC bridge. This is not recommended for a commercial program. Type 2JDBC native driver. This type uses C APIs to communicate directly with the DBMS. Type 3A vendor-neutral, network-based protocol. Type 4Java calls are made directly to the database. This type is generally supplied by a DBMS vendor.
acceptsURL(String url) connect(String url, Properties info) getMajorVersion() getMinorVersion() getPropertyInfo(String url, Properties info) jdbcCompliant()
public abstract interface PreparedStatement extends Statement A PreparedStatement object represents a SQL prepared statement. When a SQL statement is submitted to a DBMS, it must be parsed, compiled, optimized, and have a query plan generated. A PreparedStatement improves your program's performance, if you need to submit the same statement many times using different parameters, by creating and caching a compiled version of a SQL statement so that the statement does not need to be parsed, compiled, and optimized every single time it's executed. You build a PreparedStatement like this:
PreparedStatement ps = conn.createPreparedStatement ("select * from Employee where LastName = ? and Division = ?"); ps.setString(1, "Smith");
ps.setInt(2, 100); ResultSet rs = ps.executeQuery(); Here, ? stands for a modifiable value in the SQL statement. You set the value with a setxxx() method, which specifies a parameter number and a value. Then, the PreparedStatement is submitted to the Driver object. Generally, you would use the setxxx() methods and execute the PreparedStatement inside a loop, getting the parameter values from another table or from a file.
Methods
void void boolean ResultSet int ResultSetMetaData void void void void void void void void void void void void void void void void void void void void void void void void void void void void void Interface ResultSet
addBatch() clearParameters() execute() executeQuery() executeUpdate() getMetaData() setArray(int i, Array x) setAsciiStream(int parameterIndex, InputStream x, int length) setBigDecimal(int parameterIndex, BigDecimal x) setBinaryStream(int parameterIndex, InputStream x, int length) setBlob(int i, Blob x) setBoolean(int parameterIndex, boolean x) setByte(int parameterIndex, byte x) setBytes(int parameterIndex, byte[] x) setCharacterStream(int parameterIndex, Reader reader, int length) setClob(int i, Clob x) setDate(int parameterIndex, Date x) setDate(int parameterIndex, Date x, Calendar cal) setDouble(int parameterIndex, double x) setFloat(int parameterIndex, float x) setInt(int parameterIndex, int x) setLong(int parameterIndex, long x) setNull(int parameterIndex, int sqlType) setNull(int paramIndex, int sqlType, String typeName) setObject(int parameterIndex, Object x) setObject(int parameterIndex, Object x, int targetSqlType) setObject(int parameterIndex, Object x, int targetSqlType, int scale) setRef(int i, Ref x) setShort(int parameterIndex, short x) setString(int parameterIndex, String x) setTime(int parameterIndex, Time x) setTime(int parameterIndex, Time x, Calendar cal) setTimestamp(int parameterIndex, Timestamp x) setTimestamp(int parameterIndex, Timestamp x, Calendar cal) setUnicodeStream(int parameterIndex, InputStream x, int length) (Deprecated)
public abstract interface ResultSet ResultSet represents an object that contains the rows and columns returned from a query. With a JDBC 1.0style ResultSet, you can advance forward through the ResultSet using next() and then extract column values with one of a number of getxxx() methods. Using a JDBC 2.0 scrollable ResultSet, you can also move backwards in a ResultSet, go to the top of the ResultSet, jump to a specific row, and so forth. For example, assume a table with four columns: String Author, String Title, Date PubDate, and int numCopies. Here is a query that creates and processes a ResultSet for the query:
Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from Books"); While(rs.next() == true) { System.out.println("Author: " + rs.getString("Author")); // Specify the data type in the getxxx() method, like // getString(), and specify the column name or, // less desirably, the column number, to obtain // the contents of this column from this row. System.out.println("Title: " + rs.getString("Title"); System.out.println("Number of copies: " + Rs.getInt("numCopies"); }
Fields
boolean void void void void void void int boolean Array Array InputStream InputStream BigDecimal
absolute(int row) afterLast() beforeFirst() cancelRowUpdates() clearWarnings() close() deleteRow() findColumn(String columnName) first() getArray(int i) getArray(String colName) getAsciiStream(int columnIndex) getAsciiStream(String columnName) getBigDecimal(int columnIndex)
BigDecimal BigDecimal BigDecimal InputStream InputStream Blob Blob boolean boolean byte byte byte[] byte[] Reader Reader Clob Clob int String Date Date Date Date double double int int float float int int long long ResultSetMetaData Object Object Object Object Ref Ref int short short Statement String String Time Time Time Time Timestamp Timestamp Timestamp Timestamp int InputStream
getBigDecimal(int columnIndex, int scale) (Deprecated) getBigDecimal(String columnName) getBigDecimal(String columnName, int scale) (Deprecated) getBinaryStream(int columnIndex) getBinaryStream(String columnName) getBlob(int i) getBlob(String colName) getBoolean(int columnIndex) getBoolean(String columnName) getByte(int columnIndex) getByte(String columnName) getBytes(int columnIndex) getBytes(String columnName) getCharacterStream(int columnIndex) getCharacterStream(String columnName) getClob(int i) getClob(String colName) getConcurrency() getCursorName() getDate(int columnIndex) getDate(int columnIndex, Calendar cal) getDate(String columnName) getDate(String columnName, Calendar cal) getDouble(int columnIndex) getDouble(String columnName) getFetchDirection() getFetchSize() getFloat(int columnIndex) getFloat(String columnName) getInt(int columnIndex) getInt(String columnName) getLong(int columnIndex) getLong(String columnName) getMetaData() getObject(int columnIndex) getObject(int i, Map map) getObject(String columnName) getObject(String colName, Map map) getRef(int i) getRef(String colName) getRow() getShort(int columnIndex) getShort(String columnName) getStatement() getString(int columnIndex) getString(String columnName) getTime(int columnIndex) getTime(int columnIndex, Calendar cal) getTime(String columnName) getTime(String columnName, Calendar cal) getTimestamp(int columnIndex) getTimestamp(int columnIndex, Calendar cal) getTimestamp(String columnName) getTimestamp(String columnName, Calendar cal) getType() getUnicodeStream(int columnIndex) (Deprecated)
InputStream SQLWarning void boolean boolean boolean boolean boolean void void boolean boolean void boolean boolean boolean boolean void void void void void void void void void void void void void void void void void void void void void void void void void void void void void void void void void
getUnicodeStream(String columnName) (Deprecated) getWarnings() insertRow() isAfterLast() isBeforeFirst() isFirst() isLast() last() moveToCurrentRow() moveToInsertRow() next() previous() refreshRow() relative(int rows) rowDeleted() rowInserted() rowUpdated() setFetchDirection(int direction) setFetchSize(int rows) updateAsciiStream(int columnIndex, InputStream x, int length) updateAsciiStream(String columnName, InputStream x, int length) updateBigDecimal(int columnIndex, BigDecimal x) updateBigDecimal(String columnName, BigDecimal x) updateBinaryStream(int columnIndex, InputStream x, int length) updateBinaryStream(String columnName, InputStream x, int length) updateBoolean(int columnIndex, boolean x) updateBoolean(String columnName, boolean x) updateByte(int columnIndex, byte x) updateByte(String columnName, byte x) updateBytes(int columnIndex, byte[] x) updateBytes(String columnName, byte[] x) updateCharacterStream(int columnIndex, Reader x, int length) updateCharacterStream(String columnName, Reader reader, int length) updateDate(int columnIndex, Date x) updateDate(String columnName, Date x) updateDouble(int columnIndex, double x) updateDouble(String columnName, double x) updateFloat(int columnIndex, float x) updateFloat(String columnName, float x) updateInt(int columnIndex, int x) updateInt(String columnName, int x) updateLong(int columnIndex, long x) updateLong(String columnName, long x) updateNull(int columnIndex) updateNull(String columnName) updateObject(int columnIndex, Object x) updateObject(int columnIndex, Object x, int scale) updateObject(String columnName, Object x) updateObject(String columnName, Object x, int scale) updateRow()
void void void void void void void void boolean Interface Statement
updateShort(int columnIndex, short x) updateShort(String columnName, short x) updateString(int columnIndex, String x) updateString(String columnName, String x) updateTime(int columnIndex, Time x) updateTime(String columnName, Time x) updateTimestamp(int columnIndex, Timestamp x) updateTimestamp(String columnName, Timestamp x) wasNull()
public abstract interface Statement The Statement interface is implemented by objects used to execute dynamic (not prepared) SQL statements, especially for queries that return a ResultSet object.
Methods
void void void void void boolean int[] ResultSet int Connection int int int int boolean int ResultSet int int int SQLWarning void void void void void void void
addBatch(String sql) cancel() clearBatch() clearWarnings() close() execute(String sql) executeBatch() executeQuery(String sql) executeUpdate(String sql) getConnection() getFetchDirection() getFetchSize() getMaxFieldSize() getMaxRows() getMoreResults() getQueryTimeout() getResultSet() getResultSetConcurrency() getResultSetType() getUpdateCount() getWarnings() setCursorName(String name) setEscapeProcessing(boolean enable) setFetchDirection(int direction) setFetchSize(int rows) setMaxFieldSize(int max) setMaxRows(int max) setQueryTimeout(int seconds)
Class DriverManager
The DriverManager class exists as a singleton in the JVM and is responsible for managing JDBC drivers. The DriverManager maintains a list of drivers and their subprotocols. When an object requests a database connection with the getConnection() method, the DriverManager object finds a suitable driver in the internal list it maintains, if there is a suitable driver, and delegates to that driver the job of getting a connection and returning a Connection object to the caller. JDBC drivers get into the DriverManager's driver list in one of two ways:
q q
By calling registerDriver() in the driver's code By being named in the jdbc.drivers property, like this:
static static static static static static static static static static static static static static
void Connection Connection Connection Driver Enumeration int PrintStream PrintWriter void void void void void
deregisterDriver(Driver driver) getConnection(String url) getConnection(String url, Properties info) getConnection(String url, String user, String password) getDriver(String url) getDrivers() getLoginTimeout() getLogStream() (Deprecated) getLogWriter() println(String message) registerDriver(Driver driver) setLoginTimeout(int seconds) setLogStream(PrintStream out) (Deprecated) setLogWriter(PrintWriter out)
Class Timestamp
This class is a wrapper for java.util.Date that lets a java.sql.TimeStamp object represent a SQL timestamp.
Constructors
Timestamp (int year, int month, int date, int hour, int minute, int second, int nano) (Deprecated) Timestamp(long time)
Methods
after(Timestamp ts) before(Timestamp ts) equals(Object ts) equals(Timestamp ts) getNanos() setNanos(int n) toString() valueOf(String s)
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
Driver, methods PreparedStatement PreparedStatement, methods 2nd ResultSet ResultSet, fields ResultSet, methods 2nd 3rd 4th Statement Statement, methods 2nd PreparedStatement methods 2nd ResultSet fields methods 2nd 3rd 4th Statement methods 2nd java.sql Blob interface methods CallableStatement interface methods 2nd classes DriverManager DriverManager, methods 2nd Timestamp Timestamp, constructors Timestamp, methods 2nd Connection interface fields methods 2nd constructors Timestamp class Driver interface methods DriverManager class methods 2nd fields Connection interface ResultSet interface interfaces Blob Blob, methods CallableStatement CallableStatement, methods 2nd Connection Connection, fields Connection, methods 2nd Driver Driver, methods PreparedStatement PreparedStatement, methods 2nd ResultSet ResultSet, fields ResultSet, methods 2nd 3rd 4th Statement Statement, methods 2nd methods Blob interface CallableStatement interface 2nd Connection interface 2nd Driver interface DriverManager class 2nd
PreparedStatement interface 2nd ResultSet interface 2nd 3rd 4th Statement interface 2nd Timestamp class 2nd PreparedStatement interface methods 2nd ResultSet interface fields methods 2nd 3rd 4th Statement interface methods 2nd Timestamp class constructors methods 2nd JDBC driver methods JDBC drivers managing types 2nd jdbc.drivers property method declarations database connections methods Blob interface CallableStatement interface 2nd Connection interface 2nd Driver interface DriverManager class 2nd getxxx() JDBC driver PreparedStatement interface 2nd registerDriver() ResultSet interface 2nd 3rd 4th setAutoCommit(false) setxxx() 2nd Statement interface 2nd Timestamp class 2nd objects binary large objects packages java.sql Blob interface Blob interface, methods CallableStatement interface CallableStatement interface, methods 2nd Connection interface Connection interface, fields Connection interface, methods 2nd Driver interface Driver interface, methods DriverManager class DriverManager class, methods 2nd PreparedStatement interface PreparedStatement interface, methods 2nd ResultSet interface ResultSet interface, fields ResultSet interface, methods 2nd 3rd 4th Statement interface Statement interface, methods 2nd Timestamp class Timestamp class, constructors
Timestamp class, methods 2nd PreparedStatement interface methods 2nd procedures stored properties jdbc.drivers question mark (?) registerDriver() method ResultSet interface fields methods 2nd 3rd 4th setAutoCommit(false) method setxxx() method setxxx() methods Statement interface methods 2nd stored procedures Timestamp class constructors methods 2nd
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019241158101205205117223079
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019241158101204114217028220
Interfaces
AttributedCharacterIterator CharacterIterator
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019241158101204113131190146
Classes
Annotation AttributedCharacterIterator.Attribute AttributedString BreakIterator ChoiceFormat CollationElementIterator CollationKey Collator DateFormat DateFormatSymbols DecimalFormat DecimalFormatSymbols FieldPosition Format MessageFormat NumberFormat ParsePosition RuleBasedCollator SimpleDateFormat StringCharacterIterator
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019241158101204116124083190
Exception
ParseException
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019241158101204123185238116
Class Details
Class ChoiceFormat
The ChoiceFormat class is useful for formatting a range of numbers. It is often used in a MessageFormat for dealing with plurals. The choices are specified with a list of doubles in ascending order. The ChoiceFormat class differs from the other formatting classes in this package because it requires the use of a constructor, rather than a
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=223 (1 of 13) [6/2/2002 11:38:55 PM]
factory method, such as getInstance(). A ChoiceFormat is specified with two formats: limits and values. Here's a short code snippet that you could make part of a program:
double[] limitArray = { 1,2,3,4,5,6}; String[] formatArray = { "Jan","Feb","Mar","Apr","May","Jun"}; ChoiceFormat choiceForm = new ChoiceFormat(limitArray, formatArray); ParsePosition parsed = new ParsePosition(0); for (double i = 0.0; i <= 8.0; ++i) { parsed.setIndex(0); System.out.println(i + " -> " + choiceForm.format(i) + " -> " + choiceForm.parse(choiceForm.format(i),parsed)); }
Constructors
void Object boolean StringBuffer StringBuffer Object[] double[] int static double static double Number static double void String Class DateFormat
applyPattern(String newPattern) clone() equals(Object obj) format(double number, StringBuffer toAppendTo, FieldPosition status) format(long number, StringBuffer toAppendTo, FieldPosition status) getFormats() getLimits() hashCode() nextDouble(double d) nextDouble(double d, boolean positive) parse(String text, ParsePosition status) previousDouble(double d) setChoices(double[] limits, String[] formats) toPattern()
The DateFormat class is an abstract base class for classes that format and parse dates and times in a locale-independent way, such as SimpleDateFormat. You use them with a specific locale to get the date format you need. This formatting is illustrated in Chapter 18, "Text." DateFormat primarily provides factory methods for getting formatting objects and putting dates in specific formats. You can use these formatting objects to turn a Date object into a nicely formatted string such as
static int protected Calendar static int static int static int static int static int static int static int static int static int static int static int static int static int static int static int static int
AM_PM_FIELD calendar DATE_FIELD DAY_OF_WEEK_FIELD DAY_OF_WEEK_IN_MONTH_FIELD DAY_OF_YEAR_FIELD DEFAULT ERA_FIELD FULL HOUR_OF_DAY0_FIELD HOUR_OF_DAY1_FIELD HOUR0_FIELD HOUR1_FIELD LONG MEDIUM MILLISECOND_FIELD MINUTE_FIELD MONTH_FIELD
protected NumberFormat static int static int static int static int static int static int
Constructor
protected
Methods
DateFormat()
Object boolean String abstract StringBuffer StringBuffer static Locale[] Calendar static DateFormat static DateFormat static DateFormat static DateFormat static DateFormat static DateFormat static DateFormat NumberFormat static DateFormat static DateFormat static DateFormat TimeZone int boolean Date abstract Date Object void void void void Class DecimalFormat
clone() equals(Object obj) format(Date date) format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) format(Object obj, StringBuffer toAppendTo, FieldPosition fieldPosition) getAvailableLocales() getCalendar() getDateInstance() getDateInstance(int style) getDateInstance(int style, Locale aLocale) getDateTimeInstance() getDateTimeInstance (int dateStyle, int timeStyle) getDateTimeInstance (int dateStyle, int timeStyle, Locale aLocale) getInstance() getNumberFormat() getTimeInstance() getTimeInstance(int style) getTimeInstance(int style, Locale aLocale) getTimeZone() hashCode() isLenient() parse(String text) parse(String text, ParsePosition pos) parseObject(String source, ParsePosition pos) setCalendar(Calendar newCalendar) setLenient(boolean lenient) setNumberFormat(NumberFormat newNumberFormat) setTimeZone(TimeZone zone)
The DecimalFormat class is used to perform custom formatting of numeric data, using special characters to determine what separates digits, what (if anything) replaces zero in a number, and so forth.
Constructors
applyLocalizedPattern(String pattern) applyPattern(String pattern) clone() equals(Object obj) format(double number, StringBuffer result, FieldPosition fieldPosition)
StringBuffer DecimalFormatSymbols int int String String String String int boolean isDecimalSeparator Number void setDecimalFormat void setDecimalSeparator void void void void void void void void void void String String Class MessageFormat
format(long number, StringBuffer result, FieldPosition fieldPosition) getDecimalFormatSymbols() getGroupingSize() getMultiplier() getNegativePrefix() getNegativeSuffix() getPositivePrefix() getPositiveSuffix() hashCode() AlwaysShown() parse(String text, ParsePosition parsePosition) Symbols(DecimalFormatSymbols newSymbols) AlwaysShown(boolean newValue) setGroupingSize(int newValue) setMaximumFractionDigits(int newValue) setMaximumIntegerDigits(int newValue) setMinimumFractionDigits(int newValue) setMinimumIntegerDigits(int newValue) setMultiplier(int newValue) setNegativePrefix(String newValue) setNegativeSuffix(String newValue) setPositivePrefix(String newValue) setPositiveSuffix(String newValue) toLocalizedPattern() toPattern()
The MessageFormat class is used for formatting textual messages. It is particularly useful for dealing with complex messages that need varying position values for different languages.
Constructors
void Object boolean StringBuffer StringBuffer static String Format[] Locale int Object[] Object[] Object void void void String
applyPattern(String newPattern) clone() equals(Object obj) format(Object[] source, StringBuffer result, FieldPosition ignore) format(Object source, StringBuffer result, FieldPosition ignore) format(String pattern, Object[] arguments) getFormats() getLocale() hashCode() parse(String source) parse(String source, ParsePosition status) parseObject(String text, ParsePosition status) setFormat(int variable, Format newFormat) setFormats(Format[] newFormats) setLocale(Locale theLocale) toPattern()
Class NumberFormat
The NumberFormat class is the abstract base class for all number formatting classes. It provides several basic methods for formatting numbers in both standard locale- independent ways and custom ways.
Fields
FRACTION_FIELD INTEGER_FIELD
NumberFormat()
Methods
clone() equals(Object obj) format(double number) format(double number,StringBuffer toAppendTo, FieldPosition pos)
String abstract StringBuffer StringBuffer static Locale[] static NumberFormat static NumberFormat static NumberFormat static NumberFormat int Int int int static NumberFormat static NumberFormat static NumberFormat static NumberFormat int boolean boolean Number abstract Number Object void void void void void void Class SimpleDateFormat
format(long number) format(long number, StringBuffer toAppendTo, FieldPosition pos) format(Object number, StringBuffer toAppendTo, FieldPosition pos) getAvailableLocales() getCurrencyInstance() getCurrencyInstance(Locale inLocale) getInstance() getInstance(Locale inLocale) getMaximumFractionDigits() getMaximumIntegerDigits() getMinimumFractionDigits() getMinimumIntegerDigits() getNumberInstance() getNumberInstance(Locale inLocale) getPercentInstance() getPercentInstance(Locale inLocale) hashCode() isGroupingUsed() isParseIntegerOnly() parse(String text) parse(String text, ParsePosition pPos) parseObject(String source, ParsePosition pPos) setGroupingUsed(boolean newValue) setMaximumFractionDigits(int newValue) setMaximumIntegerDigits(int newValue) setMinimumFractionDigits(int newValue) setMinimumIntegerDigits(int newValue) setParseIntegerOnly(boolean value)
public class SimpleDateFormat extends DateFormat The SimpleDateFormat class enables you to format and parse dates in a locale- independent way and represent them according to a specific locale on-the-fly. For example, using the same Date object, you can print a short version of a date, such as 09/15/99, or a long version, such as September 15, 1999. You can also print a version of the date for a specific locale other than the United States, such as the form used in England: 15/9/99, in which the day precedes the month.
Symbol G y M d h H m s S E D F w W a k
Meaning Era designator Year Month in year Day in month Hour in am/pm (112) Hour in day (023) Minute in hour Second in minute Millisecond Day in week Day in year Day of week in month Week in year Week in month Am/pm marker Hour in day (124)
Example AD 1996 July & 07 10 12 0 30 55 978 Tuesday 189 2 (2nd Wed in July) 27 2 PM 24
K z ' ''
Hour in am/pm (011) Time zone Escape for text Single quote
The number of symbols in the pattern determines the format of the date. For an example, see Table 32.2.
Table 32.2. Samples of Formatting Symbol Use Format Pattern Example Result 1999.98.15 AD at 16:12:16 PDT "yyyy.MM.dd G 'at'hh:mm:ss z" Wed, September 15, '98 "EEE, MMM d, ''yy" Constructors
SimpleDateFormat() SimpleDateFormat(String pattern) SimpleDateFormat(String pattern, DateFormatSymbols formatData) SimpleDateFormat(String pattern, Locale loc)
Methods
void void Object boolean StringBuffer Date DateFormatSymbols int Date void void String String
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
applyLocalizedPattern(String pattern) applyPattern(String pattern) clone() equals(Object obj) format(Date date, StringBuffer toAppendTo, FieldPosition pos) get2DigitYearStart() getDateFormatSymbols() hashCode() parse(String text, ParsePosition pos) set2DigitYearStart(Date startDate) setDateFormatSymbols(DateFormatSymbols newForSym) toLocalizedPattern() toPattern()
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
constructors methods 2nd methods ChoiceFormat class 2nd DateFormat class 2nd DecimalFormat class 2nd MessageFormat class 2nd NumberFormat class 2nd SimpleDateFormat class 2nd NumberFormat class 2nd constructors fields methods 2nd numbers formatting 2nd packages java.text ChoiceFormat class 2nd DateFormat class 2nd DecimalFormat class 2nd MessageFormat class NumberFormat class 2nd SimpleDateFormat class 2nd 3rd parsingdates SimpleDateFormat class 2nd 3rd constructors methods 2nd text formatting
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019241158101202188050045000
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019241157244069120001218045
Interfaces
Collection Comparator Enumeration EventListener Iterator List ListIterator Map Map.Entry Observer Set SortedMap SortedSet
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019241157244069123251182081
Classes
AbstractCollection AbstractList AbstractMap AbstractSequentialList AbstractSet ArrayList Arrays BitSet Calendar Collections Date Dictionary EventObject GregorianCalendar HashMap HashSet Hashtable LinkedList ListResourceBundle Locale Observable Properties PropertyPermission PropertyResourceBundle Random ResourceBundle SimpleTimeZone Stack StringTokenizer TimeZone TreeMap TreeSet Vector WeakHashMap
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019241157244069126239069102
Exceptions
All Books
Search
Java > PURE Java 2 > 33. java.util > Interface and Class Details < BACK Make Note | Bookmark
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019241157244069113243048002
public abstract interface Collection The Collection interface is the base interface for all Collection interfaces and the basis of all Collection classes in the Collection framework. Collection is simply a set of objects that can be treated as individual items or as a group. The Collection interface specifies methods for dealing with individual objects, such as add(), and for dealing with a whole Collection at once, such as removeAll().
Methods
boolean add(Object o) boolean addAll(Collection c) void clear() boolean contains(Object o) boolean containsAll(Collection c) boolean equals(Object o) int hashCode() boolean isEmpty() Iterator iterator() boolean remove(Object o) boolean removeAll(Collection c) boolean retainAll(Collection c) int size() Object[] toArray() Object[]toArray(Object[] a) Interface Iterator
public abstract interface Iterator The Iterator interface specifies the methods for an object that can be used to iterate or step through a Collection. Here is a code snippet showing how you might iterate through a collection and remove from it any element that matches some condition:
public abstract interface List extends Collection The List interface specifies the methods for a Collection implemented as a list. A List implementation provides for a Collection that's ordered and allows duplicate objects. The following is a small code fragment that shows how to create, populate, and extract from a List. This code uses the Collections.shuffle() method, which rearranges the elements in the list in a random fashion. Therefore, the contents come out in random order.
public boolean shuffleAuthors(String authors[]) { // This method accepts a String array of authors. List authorList = new ArrayList(); // Create an object that implements the List interface. for (int i=0; i<authors.length; i++) authorList.add(authors[i]); Collections.shuffle(authorList, new Random()); System.out.println(authorList); Return true; }
Methods
void boolean boolean boolean void boolean boolean boolean Object int int boolean Iterator int ListIterator
add(int index, Object element) add(Object o) addAll(Collection c) addAll(int index, Collection c) clear() contains(Object o) containsAll(Collection c) equals(Object o) get(int index) hashCode() indexOf(Object o) isEmpty() iterator() lastIndexOf(Object o) listIterator()
ListIterator Object boolean boolean boolean Object int List Object[] Object[] Interface Map
listIterator(int index) remove(int index) remove(Object o) removeAll(Collection c) retainAll(Collection c) set(int index, Object element) size() subList(int fromIndex, int toIndex) toArray() toArray(Object[] a)
public abstract interface Map The Map interface specifies the methods for a Collection implemented as a Map. A Map is not technically a Collection in a normal sense. A Map implementation object consists of key/value pairs. A Map may not contain duplicate key values. By default, a Map has no ordering. A TreeMap, on the other hand, is an implementation that provides for ordering. The following is a code snippet that shows how to create a map object, add to it, and then determine the frequency of various values in it. In general, you do not want a Map to contain duplicate items, but this is semantically valid. It's a performance issue if a Map contains duplicate items.
public boolean getFrequency(String authors[]) { Map map = new HashMap(); // Initialize frequency table for (int i=0; i<authors.length; i++) { Integer frequency = (Integer) map.get(authors[i]); map.put(authors[i], (frequency == null ? ONE : new Integer(freq.intValue() + 1))); } System.out.println(map.size()+ " distinct authors detected:"); System.out.println(map); return true; }
Methods
void boolean boolean Set boolean Object int boolean Set Object void Object
clear() containsKey(Object key) containsValue(Object value) entrySet() equals(Object o) get(Object key) hashCode() isEmpty() keySet() put(Object key, Object value) putAll(Map t) remove(Object key)
int Collection
size() values()
Interface Observer
public abstract interface Observer The Observer interface specifies the methods for the "observer" participant in the Observer/Observable design pattern. This design pattern refers to an object-oriented scheme for having any number of objects passively observe an observable object. The observers want to know when the observable object performs one or more activities. Generally, the observable object notifies the observers that some event has occurred. Although the method names and exact framework are different, you can see this design pattern at work when using Java beans. When one of a bean's properties changes, the bean notifies any registered PropertyChangeListener.
Methods
void
Interface Set
public abstract interface Set extends Collection Set specifies the methods for a Collection, which is unordered but allows no duplicates. Here is a code snippet showing how to create and add an element to a HashSet:
public boolean buildSet(Book[] newBooks) { Set bookSet = new HashSet(); // Returns an object that implements the Set interface. for (int i=0; i<newBooks.length; i++) if (!bookSet.add(newBooks[i])) System.out.println("Duplicate detected: " + newBooks[i]); System.out.println(bookSet.size()+ " distinct new books: "+ bookSet); return true; }
Methods
Class ArrayList
public class ArrayList extends AbstractList implements List, Cloneable, Serializable An ArrayList object implements the List interface in the form of a "growable" array. In fact, you can create an array from an ArrayList, and vice versa.
Constructors
Methods
void boolean boolean boolean void Object boolean void Object int boolean int Object void Object int Object[] Object[] void Class BitSet
add(int index, Object element) add(Object o) addAll(Collection c) addAll(int index, Collection c) clear() clone() contains(Object elem) ensureCapacity(int minCapacity) get(int index) indexOf(Object elem) isEmpty() lastIndexOf(Object elem) remove(int index) removeRange(int fromIndex, int toIndex) set(int index, Object element) size() toArray() toArray(Object[] a) trimToSize()
A BitSet object is a vector of bits that can be stepped through with nonnegative integers. You may want to use a BitSet to store bit information rather than using AND, OR, or XOR operations on bits in an int.
Constructors
void void void Object boolean boolean int int void void int String void
and(BitSet set) andNot(BitSet set) clear(int bitIndex) clone() equals(Object obj) get(int bitIndex) hashCode() length() or(BitSet set) set(int bitIndex) size() toString() xor(BitSet set)
Class Calendar
The Calendar class is the base class for working with calendar-type information. The difference between Calendar and Date is that Date is for a specific moment in time, whereas Calendar is meant for actual dates. You should use a subclass of Calendar, because Calendar is an abstract class and cannot therefore be instantiated. The only one currently in the SDK is GregorianCalendar. If you need to create your own calendar class that doesn't use Gregorian dates, you should still try to subclass Calendar in order to get common data members and at least some methods. The Calendar class provides locale-specific support. For example, if you ask what the first day of the week is, the Calendar class returns "Sunday" for the U.S. but "Monday" for France.
Fields
static int static int static int protected boolean static int static int static int static int static int static int static int static int static int static int static int protected int[] static int static int static int protected boolean[] protected boolean static int static int static int static int static int static int static int static int static int static int static int static int static int static int static int static int static int protected long static int static int static int static int static int static int static int
Constructors
AM AM_PM APRIL areFieldsSet AUGUST DATE DAY_OF_MONTH DAY_OF_WEEK DAY_OF_WEEK_IN_MONTH DAY_OF_YEAR DECEMBER DST_OFFSET ERA FEBRUARY FIELD_COUNT fields FRIDAY HOUR HOUR_OF_DAY isSet isTimeSet JANUARY JULY JUNE MARCH MAY MILLISECOND MINUTE MONDAY MONTH NOVEMBER OCTOBER PM SATURDAY SECOND SEPTEMBER SUNDAY THURSDAY time TUESDAY DECIMBER WEDNESDAY WEEK_OF_MONTH WEEK_OF_YEAR YEAR ZONE_OFFSET
protected protected
Methods
abstract void boolean boolean void void Object protected void protected abstract void protected abstract void boolean int int int static Locale[] int abstract int static Calendar static Calendar static Calendar static Calendar abstract int abstract int int abstract int Date protected long TimeZone int protected int boolean boolean abstract void void void void void minute) void void void void void protected void void String Class Collections
add(int field, int amount) after(Object when) before(Object when) clear() clear(int field) clone() complete() computeFields() computeTime() equals(Object obj) get(int field) getActualMaximum(int field) getActualMinimum(int field) getAvailableLocales() getFirstDayOfWeek() getGreatestMinimum(int field) getInstance() getInstance(Locale aLocale) getInstance(TimeZone zone) getInstance(TimeZone zone, Locale aLocale) getLeastMaximum(int field) getMaximum(int field) getMinimalDaysInFirstWeek() getMinimum(int field) getTime() getTimeInMillis() getTimeZone() hashCode() internalGet(int field) isLenient() isSet(int field) roll(int field, boolean up) roll(int field, int amount) set(int field, int value) set(int year, int month, int date) set(int year, int month, int date, int hour, set(int year, int month, int date, int hour, int minute, int second) setFirstDayOfWeek(int value) setLenient(boolean lenient) setMinimalDaysInFirstWeek(int value) setTime(Date date) setTimeInMillis(long millis) setTimeZone(TimeZone value) toString()
int
The Collections class provides a number of methods for working with any kind of collection. It provides collection views (which allow you to extract elements from a collection) such as a KeySet, which is just the key values from a Map implementation. This class also provides a number of wrappers for Collection objects, such as a synchronized wrapper, to make a Collection implementation (which does not use synchronization) behave in a synchronized fashion.
Fields
EMPTY_LIST EMPTY_SET
static static static static static static static static static static static static static static static static static static static static static static static static
int int void Enumeration void Object Object Object Object List void Comparator void void Set void void Collection List Map Set SortedMap SortedSet Collection
binarySearch(List list, Object key) binarySearch(List list, Object key, Comparator c) copy(List dest, List src) enumeration(Collection c) fill(List list, Object o) max(Collection coll) max(Collection coll, Comparator comp) min(Collection coll) min(Collection coll, Comparator comp) nCopies(int n, Object o) reverse(List l) reverseOrder() shuffle(List list) shuffle(List list, Random rnd) singleton(Object o) sort(List list) sort(List list, Comparator c) synchronizedCollection(Collection c) synchronizedList(List list) synchronizedMap(Map m) synchronizedSet(Set s) synchronizedSortedMap(SortedMap m) synchronizedSortedSet(SortedSet s) unmodifiableCollection(Collection c)
Class GregorianCalendar
The GregorianCalendar class provides support for creating and manipulating calendar objects (that is, objects that represent a date and time). There are several fieldsmany from Calendarthat you can set, including time zones, daylight savings time, specific days and times to milliseconds, and so forth. Here's a code snippet to show how to create a GregorianCalendar object:
// Get time zone ids. String[] tzIds = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000); // Create a Pacific Standard Time time zone. pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, tzIds[0]); // Set up rules for daylight savings time. pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); // Create a GregorianCalendar with the Pacific Daylight // time zone and the current date and time. Calendar calendar = new GregorianCalendar(pdt); Date trialTime = new Date();
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=229 (11 of 32) [6/2/2002 11:39:54 PM]
calendar.setTime(trialTime); System.out.println("WEEK_OF_MONTH: " + TH)); This class would, of course, have to be instantiated from another object.
Fields
AD BC
GregorianCalendar () GregorianCalendar(int year, int month, int date) GregorianCalendar(int year, int month, int date, int hour, int minute) GregorianCalendar(int year, int month, int date, int hour, int minute, int second) GregorianCalendar(Locale aLocale) GregorianCalendar(TimeZone zone) GregorianCalendar(TimeZone zone, Locale aLocale)
Methods
void protected void protected void boolean int int int Date int int int int boolean void void void Class HashMap
add(int field, int amount) computeFields() computeTime() equals(Object obj) getActualMaximum(int field) getActualMinimum(int field) getGreatestMinimum(int field) getGregorianChange() getLeastMaximum(int field) getMaximum(int field) getMinimum(int field) hashCode() isLeapYear(int year) roll(int field, boolean up) roll(int field, int amount) setGregorianChange(Date date)
The HashMap class uses a hash table implementation to implement a Map, with keys and values ordered according to how the keys are hashed.
Constructors
void Object boolean boolean Set Object boolean Set Object void Object int Collection Class HashSet
clear() clone() containsKey(Object key) containsValue(Object value) entrySet() get(Object key) isEmpty() keySet() put(Object key, Object value) putAll(Map t) remove(Object key) size() values()
public class HashSet extends AbstractSet implements Set, Cloneable, Serializable The HashSet class implements the Set interface, which is a collection of unordered objects that have no duplicate elements. HashSet is built on a hash table (actually a HashMap).
Constructors
boolean void Object boolean boolean Iterator boolean int Class Locale
The Locale class provides support for representing specific geographical, political, or cultural regions. It does not merely represent a single language, but it can cover, for example, both the French language of France and the French dialect of Canada. You use a Locale object to access ResourceBundle objects to implement locale-specific values for text, numbers, and dates.
Fields
static static static static static static static static static static static static static static static static
Locale Locale Locale Locale Locale Locale Locale Locale Locale Locale Locale Locale Locale Locale Locale Locale
CANADA CANADA_FRENCH CHINA CHINESE ENGLISH FRANCE FRENCH GERMAN GERMANY ITALIAN ITALY JAPAN JAPANESE KOREA KOREAN PRC
Constructors
Locale (String language, String country) Locale(String language, String country, String variant)
Methods
Object boolean static Locale[] String static Locale String String String String String String String String String String static String[] static String[] String String int static void String Class Observable
clone() equals(Object obj) getAvailableLocales() getCountry() getDefault() getDisplayCountry() getDisplayCountry(Locale inLocale) getDisplayLanguage() getDisplayLanguage(Locale inLocale) getDisplayName() getDisplayName(Locale inLocale) getDisplayVariant() getDisplayVariant(Locale inLocale) getISO3Country() getISO3Language() getISOCountries() getISOLanguages() getLanguage() getVariant() hashCode() setDefault(Locale newLocale) toString()
The Observable class represents an observable object, as described in the Observer interface. The Observable object has the responsibility of notifying interested observers of events. This is based on the Observer/Observable design pattern. Many developers take a different approach to implementing this design pattern and prefer an interface to define an Observable object so that the Observable object can subclass some other class.
Constructors
Observable ()
Methods
void protected void int void void boolean void void protected void Class Properties
addObserver(Observer o) clearChanged() countObservers() deleteObserver(Observer o) deleteObservers() hasChanged() notifyObservers() notifyObservers(Object arg) setChanged()
The Properties class provides for a set of properties built on key/value pairs. A Properties object is a special kind of HashTable. All the keys and values must be Strings. There are several system properties that contain helpful information. Those can be obtained like this:
Properties props = System.getProperties(); Alternatively, you can ask for a specific property like this:
String fileSep = Stem.getProperty("file.separator"); Properties can be very useful in creating configuration files. Windows users are probably accustomed to INI files or the Windows Registry. You can store similar information, such as the proper directory for a program to use, in a Properties object stored in a file. See the "Properties" section in Chapter 19, "Utility Classes," for a complete example of getting and changing properties and storing them on disk.
Fields
protected Properties
Constructors
defaults
getProperty(String key) getProperty(String key, String defaultValue) list(PrintStream out) list(PrintWriter out) load(InputStream inStream) propertyNames() save(OutputStream out, String header) (Deprecated) setProperty(String key, String value) store(OutputStream out, String header)
Class PropertyPermission
The PropertyPermission class represents permissions granted for accessing various system properties. These should be granted with care because some of them relate to a client machine's file system. The properties are primarily system properties, such as os.name and user.name. There are two actions allowed on a property: read and write. These determine, as their names suggest, the ability to access and update a property value, respectively.
Constructors
A ResourceBundle object maps variables in a program, such as the label on a button, to locale-specific values. For example, a ResourceBundle for U.S. English would map GOOD to good, whereas a French one would map GOOD to bon, and a German one would map GOOD to gut. See Chapter 18, "Text," for more examples.
Fields
protected ResourceBundle
Constructors
parent
ResourceBundle ()
Methods
static ResourceBundle static ResourceBundle static ResourceBundle abstract Enumeration Locale Object String String[] protected abstract Object protected void Class StringTokenizer
getBundle(String baseName) getBundle(String baseName, Locale locale) getBundle(String baseName, Locale locale, ClassLoader loader) getKeys() getLocale() getObject(String key) getString(String key) getStringArray(String key) handleGetObject(String key) setParent(ResourceBundle parent)
public class StringTokenizer extends Object implements Enumeration The StringTokenizer class provides a mechanism for breaking a string up into logical pieces based on tokens, such as a colon or blank space. This class is much easier to use than StreamTokenizer. Here's a code snippet showing how you might use the StringTokenizer class:
StringTokenizer strtok = new StringTokenizer ("Demonstrating the StringTokenizer class"); while(strtok.hasMoreTokens()) { println(strtok.nextToken()); } // This will treat the blanks as delimiters of tokens // and print each word separately.
Constructors
StringTokenizer (String str) StringTokenizer(String str, String delim) StringTokenizer(String str, String delim, boolean returnTokens)
Methods
public class Vector extends AbstractList implements List, Cloneable, Serializable The Vector class existed before Java 2 but has been made part of the Collection framework. Unlike other classes in the Collection framework, however, a Vector's methods are still synchronized. Vector is a very useful class. It can grow dynamically, and you can put any kind of object (but not primitives) into it. You might use it, for example, to send e-commerce information from a client's system to a servlet, putting each distinct field in an element of a vector. Unless you need synchronized methods, however, you should use some other collection implementation, such as HashSet. Here's a code snippet to show how to create a Vector and add elements to it and then get its size.
Vector vect = new Vector(50,5); vect.addElement("Add a String"); vect.addElement(new Date()); vect.addElement(new Integer(50)); System.out.println("Size of vect is " + vect.size()); if(vect.isEmpty() == true) System.out.println("Vector is empty"); Caution You should never create a Vector without giving it both an initial size and an increment amount. If you do not specify the latter, when Java needs to add an element, it first creates a Vector twice the size of the existing one. This is not too bad for a Vector with five Integer objects, but it's a serious problem for a Vector with 5,000 GregorianCalendar objects.
Fields
void boolean boolean boolean void int void Object boolean boolean void Object Enumeration void boolean Object Object int int int void boolean Object int int Object boolean boolean void boolean void void boolean Object void void int List Object[] Object[]
add(int index, Object element) add(Object o) addAll(Collection c) addAll(int index, Collection c) addElement(Object obj) capacity() clear() clone() contains(Object elem) containsAll(Collection c) copyInto(Object[] anArray) elementAt(int index) elements() ensureCapacity(int minCapacity) equals(Object o) firstElement() get(int index) hashCode() indexOf(Object elem) indexOf(Object elem, int index) insertElementAt(Object obj, int index) isEmpty() lastElement() lastIndexOf(Object elem) lastIndexOf(Object elem, int index) remove(int index) remove(Object o) removeAll(Collection c) removeAllElements() removeElement(Object obj) removeElementAt(int index) removeRange(int fromIndex, int toIndex) retainAll(Collection c) set(int index, Object element) setElementAt(Object obj, int index) setSize(int newSize) size() subList(int fromIndex, int toIndex) toArray() toArray(Object[] a)
String void
toString() trimToSize()
Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing Make Note | Bookmark CONTINUE >
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
HashMap HashMap, constructors HashMap, methods 2nd HashSet HashSet, methods Locale Locale, constructors Locale, fields 2nd Locale, methods 2nd Observable Observable, constructors Observable, methods Properties Properties, constructors Properties, fields Properties, methods PropertyPermission PropertyPermission, constructors PropertyPermission, methods ResourceBundle ResourceBundle, constructors ResourceBundle, fields ResourceBundle, methods StringTokenizer StringTokenizer, constructors StringTokenizer, methods Vector Vector, constructors Vector, fields Vector, methods 2nd Locale constructors fields 2nd methods 2nd Observable constructors methods Properties constructors fields methods PropertyPermission constructors methods ResourceBundle constructors fields methods StringTokenizer constructors methods Vector constructors fields methods 2nd Collection interface methods 2nd Collections class fields methods 2nd Collections.shuffle() method
constructors ArrayList class BitSet class Calendar class GregorianCalendar class HashMap class Locale class Observable class Properties class PropertyPermission class ResourceBundle class StringTokenizer class Vector class design patterns Observer/Observable Observer interface fields Calendar class 2nd Collections class GregorianCalendar class Locale class 2nd Properties class ResourceBundle class Vector class GregorianCalendar class constructors fields methods 2nd HashMap class constructors methods 2nd HashSet class methods interfaces Collection methods 2nd Iterator methods java.util Collection Collection, methods 2nd Iterator Iterator, methods List List, methods 2nd Map Map, methods 2nd Observer Observer, methods Set Set, methods 2nd List methods 2nd Map methods 2nd Observer methods Set methods 2nd TreeMap Iterator interface
methods java.util ArrayList class constructors methods 2nd BitSet class constructors methods Calendar class constructors fields 2nd methods 2nd classes ArrayList ArrayList, constructors ArrayList, methods 2nd BitSet BitSet, constructors BitSet, methods Calendar Calendar, constructors Calendar, fields 2nd Calendar, methods 2nd Collections Collections, fields Collections, methods 2nd GregorianCalendar GregorianCalendar, constructors GregorianCalendar, fields GregorianCalendar, methods 2nd HashMap HashMap, constructors HashMap, methods 2nd HashSet HashSet, methods Locale Locale, constructors Locale, fields 2nd Locale, methods 2nd Observable Observable, constructors Observable, methods Properties Properties, constructors Properties, fields Properties, methods PropertyPermission PropertyPermission, constructors PropertyPermission, methods ResourceBundle ResourceBundle, constructors ResourceBundle, fields ResourceBundle, methods StringTokenizer StringTokenizer, constructors StringTokenizer, methods Vector Vector, constructors Vector, fields Vector, methods 2nd Collection interface
methods 2nd Collections class fields methods 2nd constructors ArrayList class BitSet class Calendar class GregorianCalendar class HashMap class Locale class Observable class Properties class PropertyPermission class ResourceBundle class StringTokenizer class Vector class fields Calendar class 2nd Collections class GregorianCalendar class Locale class 2nd Properties class ResourceBundle class Vector class GregorianCalendar class constructors fields methods 2nd HashMap class constructors methods 2nd HashSet class methods interfaces Collection Collection, methods 2nd Iterator Iterator, methods List List, methods 2nd Map Map, methods 2nd Observer Observer, methods Set Set, methods 2nd Iterator interface methods List interface methods 2nd Locale class constructors fields 2nd methods 2nd Map interface methods 2nd methods ArrayList class 2nd BitSet class Calendar class 2nd
Collection interface 2nd Collections class 2nd GregorianCalendar class 2nd HashMap class 2nd HashSet class Iterator interface List interface 2nd Locale class 2nd Map interface 2nd Observable class Observer interface Properties class PropertyPermission class ResourceBundle class Set interface 2nd StringTokenizer class Vector class 2nd Observable class constructors methods Observer interface methods Properties class constructors fields methods PropertyPermission class constructors methods ResourceBundle class constructors fields methods Set interface methods 2nd StringTokenizer class constructors methods Vector class constructors fields methods 2nd key/value pairs Properties class List interface methods 2nd Locale class constructors fields 2nd methods 2nd Map interface methods 2nd methods ArrayList class 2nd BitSet class Calendar class 2nd Collection interface 2nd Collections class 2nd Collections.shuffle() GregorianCalendar class 2nd HashMap class 2nd
HashSet class Iterator interface List interface 2nd Locale class 2nd Map interface 2nd Observable class Observer interface Properties class PropertyPermission class ResourceBundle class Set interface 2nd StringTokenizer class Vector class 2nd Observable class constructors methods Observer interface methods Observer/Observable design pattern Observer interface packages java.util ArrayList class ArrayList class, constructors ArrayList class, methods 2nd BitSet class BitSet class, constructors BitSet class, methods Calendar class Calendar class, constructors Calendar class, fields 2nd Calendar class, methods 2nd Collection interface Collection interface, methods 2nd Collections class Collections class, fields Collections class, methods 2nd GregorianCalendar class GregorianCalendar class, constructors GregorianCalendar class, fields GregorianCalendar class, methods 2nd HashMap class HashMap class, constructors HashMap class, methods 2nd HashSet class HashSet class, methods Iterator interface Iterator interface, methods List interface List interface, methods 2nd Locale class Locale class, constructors Locale class, fields 2nd Locale class, methods 2nd Map interface Map interface, methods 2nd Observable class Observable class, constructors Observable class, methods Observer interface Observer interface, methods
Properties class Properties class, constructors Properties class, fields Properties class, methods PropertyPermission class PropertyPermission class, constructors PropertyPermission class, methods ResourceBundle class ResourceBundle class, constructors ResourceBundle class, fields ResourceBundle class, methods Set interface Set interface, methods 2nd StringTokenizer class StringTokenizer class, constructors StringTokenizer class, methods Vector class Vector class, constructors Vector class, fields Vector class, methods 2nd permissions PropertyPermission class Properties class constructors fields methods PropertyPermission class constructors methods ResourceBundle class constructors fields methods Set interface methods 2nd Strings tokens StringTokenizer class constructors methods tokens Strings TreeMap interface Vector class constructors fields methods 2nd Vectors sizing
2002, O'Reilly & Associates, Inc.
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019241154112080095208006240
jar cf MyJar.jar *.class *.gif in which jar says to run the jar command. The cf flags tell the jar command to create a jar file (c is for create, and f is for file). MyJar.jar is the name of the jar file to create. This is followed by the list of files to put into the jar file. In this case, it is all Java class files and all the .gif files in the current directory. A jar file is a Java platform-neutral version of a PKWare zip file. You can extract the contents of a jar file by typing this:
jar xvf MyJar.jar The flags xvf tell the jar command to extract (x) from a file (f) in verbose mode (show on the screen what is happening). This is followed by the jar filename MyJar.jar. Since a jar file is platform-neutral, you can create one and extract one any place there is a Sun-compliant JVM. As a side benefit, because jar files are related to zip files, you can use the jar command on a platform that does not support zip files, such as Solaris, to unzip a zip file, like this:
jar xvf Myfiles.zip This will treat Myfiles.zip like a jar file and extract it, restoring the original directory structure. You can also execute a jar file from the command line or from a script. You specify the jar file in a URL and then use the JarRunner command to execute the jar file. Here is an example:
java JarRunner http://www.Vaporware.com/NetMonitor.jar Using this mechanism, you can execute the program that is contained in NetMonitor.jar, which means that the jar file itself is executable, and not just an
archive file.
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019241154112081095072045085
Classes
All Books
Search
CONTINUE >
158122020158178182047242169203138024176246252243131023021084156019241154112081092219054247
Exceptions
JarException
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK Make Note | Bookmark Last updated on 11/22/2001 PURE Java 2, 2002 Sams Publishing CONTINUE >
All Books
Search
158122020158178182047242169203138024176246252243131023021084156019241153088096228105011083
Class Details
Class JarEntry
This class represents a jar file entry, such as a class or jpeg file. The following code snippet shows how to get the entries in a jar file and query them.
JarFile jf = new JarFile("SomeJarFile.jar"); // Create a JarFile object from the file SomeJarFile.jar. Enumeration jarEnum = jf.entries(); while(jarEnum.hasMoreElements())
http://safari.oreilly.com/main.asp?bookname=0672316544&snode=234 (1 of 11) [6/2/2002 11:43:14 PM]
{ JarEntry je = (JarEntry)enumJar.nextElement(); // Create a JarEntry from a jar entry in the jar file. Attributes jarAttr = je.getAttributes(); Set keys = jarAttr.keySet(); // Get keys of Attributes as a KeySet. Collection values = jarAttr.values(); // Get values that go with keys as a Collection. // Next, you could iterate through keys and values // or whatever you want to do with them.
Constructors
getAttributes() getCertificates()
JarFile ZipFile
The class JarFile provides for reading the contents of a jar file, or any file that can be accessed as a RandomAccessFile. A JarFile object can also read an optional Manifest entry because it extends java.util.zip.ZipFile. A Manifest file contains information about the jar file, or meta data about the jar file. Here is a code snippet to show some basic things you can do with a JarFile object:
JarFile jf = new JarFile(someFileObject); Enumberation jarEnum = jf.entries(); JarEntry je = jf.getJarEntry("BookJpeg"); // Get a specific jar file entry. Manifest man = jf.getManifest(); // Retrieve the manifest for this jar file. Note Since JarFile extends ZipFile, there are also methods here and in JarEntry for processing zip file entries.
Fields
static String
MANIFEST_NAME
Constructors
JarFile (File file) JarFile(File file, boolean verify) JarFile(String name) JarFile(String name, boolean verify)
Methods
Class JarInputStream
public class JarInputStream extends ZipInputStream JarInputStream is a special kind of FilterInputStream that provides methods for reading the contents of a jar file, including the Manifest entry, if present. You might want to read one or more entries from a jar file yourself. For example, you might want to extract an image from a jar file. Here's a code snippet showing how to read individual entries as a stream:
try { JarInputStream jis = new JarInputStream (new FileInputStream("SomeJarFile.jar")); JarEntry je = null; while ((je = jis.getNextJarEntry()) != null) { // Process jar entry, such as // converting bytes into a jpeg Image object. jis.closeEntry(); // Close the entry as you would a stream. } jis.close(); // Close the JarInputStream.
Constructors
ZipEntry createZipEntry(String name) getManifest() getNextEntry() getNextJarEntry() read(byte[] b, int off, int len)
Class JarOutputStream
The JarOutputStream class provides methods for writing an entry in a jar file, including a Manifest entry.
Constructors
void
putNextEntry(ZipEntry ze)
Class Manifest
The Manifest class provides methods to work with Manifest entry names and attributes of the Manifest itself and the individual entries. The following code snippet shows the use of a Manifest object:
Manifest man = theJarFile.getManifest(); FileInputStream fis = new FileInputStream ("AjarFile.jar"); man.read(fis); // Read in a Manifest file from the specified // InputStream. The current Manifest contents are // merged with the contents of the file. The read() and // write() methods might be useful for examining the // deployment descriptor of an Enterprise JavaBean. Attributes manAttr = man.getMainAttributes(); Map manEntries = man.getEntries(); Attributes entryAttr = man.getAttributes("jarEntry");
Constructors
void void
Delivered for Maurice ling Swap Option Available: 7/15/2002 < BACK
entries, code to query 2nd entries, writing reading 2nd jar files entries writing entries, querying code 2nd reading 2nd JarEntry class constructors methods JarFile class constructors fields methods JarInputStream class constructors methods JarOutputStream class constructors methods java.util.jar classes JarEntry JarEntry, constructors JarEntry, methods JarFile JarFile, constructors JarFile, fields JarFile, methods JarInputStream JarInputStream, constructors JarInputStream, methods JarOutputStream JarOutputStream, constructors JarOutputStream, methods Manifest Manifest, constructors Manifest, methods constructors JarEntry class JarFile class JarInputStream class JarOutputStream class Manifest class fields JarFile class JarEntry class constructors methods JarFile class constructors fields methods JarInputStream class constructors
methods JarOutputStream class constructors methods Manifest class constructors methods methods JarEntry class JarFile class JarInputStream class JarOutputStream class Manifest class Manifest class constructors methods methods JarEntry class JarFile class JarInputStream class JarOutputStream class Manifest class packages java.util.jar JarEntry class JarEntry class, constructors JarEntry class, methods JarFile class JarFile class, constructors JarFile class, fields JarFile class, methods JarInputStream class JarInputStream class, constructors JarInputStream class, methods JarOutputStream class JarOutputStream class, constructors JarOutputStream class, methods Manifest class Manifest class, constructors Manifest class, methods reading jar files 2nd writing entries for jar files
2002, O'Reilly & Associates, Inc.