You are previewing Java 7 Recipes: A Problem-Solution Approach.
O'Reilly logo
Java 7 Recipes: A Problem-Solution Approach

Book Description

Java 7 Recipes offers solutions to common programming problems encountered every day while developing Java-based applications. Fully updated with the newest features and techniques available, Java 7 Recipes provides code examples involving Servlets, Java FX 2.0, XML, Java Swing, and much more. Content is presented in the popular problem-solution format: Look up the programming problem that you want to solve. Read the solution. Apply the solution directly in your own code. Problem solved!

The problem-solution approach sets Java 7 Recipes apart from other books on the topic. Java 7 Recipes is focused less on the language itself and more on what you can do with it that is useful. The book respects your time by always focusing on a task that you might want to perform using the language. Solutions come first. Explanations come later. You are free to crib from the book and apply the code examples directly to your own projects.

  • Covers all-new release of Java: Java 7

  • Focuses especially on up-and-coming technologies such as Java FX 2.0

  • Respects your time by focusing on practical solutions you can implement in your own code

What you'll learn

  • Develop Java SE applications using the latest in Java SE technology

  • Create great-looking user interfaces with Java Swing and Java FX 2.0

  • Build web-based applications around Java Servlet technology

  • Generate graphics and work with media such as sound and video

  • Implement web services and applications to invoke them

  • Exploit the power of XML from within your Java applications

Who this book is for

This book is for experienced Java programmers new to Java 7 and those with some experience with Java 7.

Table of Contents

  1. Title
  2. Dedication
  3. Contents at a Glance
  4. Contents
  5. About the Authors
  6. About the Technical Reviewers
  7. Acknowledgments
  8. Introduction
    1. Who This Book Is For
    2. How This Book Is Structured
  9. Chapter 1: Getting Started with Java 7
    1. 1-1. Downloading and Installing Java 7
    2. 1-2. Configuring the CLASSPATH
    3. 1-3. Writing an Executable Java Class
    4. 1-4. Compiling and Executing Java Class
    5. 1-5. Printing Output
    6. 1-6. Passing Arguments via the Command Line
    7. 1-7. Obtaining and Setting Environment Variable Values
    8. 1-8. Documenting Code with Javadoc
    9. 1-9. Marking Features as Deprecated
    10. 1-10. Creating Methods in a Class
    11. 1-11. Passing Arguments to Methods and Returning Values
    12. 1-12. Organizing Code with Packages
    13. 1-13. Accepting Keyboard Input from the Command Line
  10. Chapter 2: Strings
    1. 2-1. Obtaining a Subsection of a String
    2. 2-2. Comparing Strings
    3. 2-3. Trimming Whitespace
    4. 2-4. Changing the Case of a String
    5. 2-5. Concatenating Strings
    6. 2-6. Converting Strings to Numeric Values
    7. 2-7. Iterating Over the Characters of a String
    8. 2-8. Finding Text Matches
    9. 2-9. Replacing All Text Matches
    10. 2-10. Determining Whether a File Name Ends with a Given String
  11. Chapter 3: Numbers and Dates
    1. 3-1. Rounding Float and Double Values to Integers
    2. 3-2. Formatting Double and Long Decimal Values
    3. 3-3. Comparing int Values
    4. 3-4. Comparing Floating-Point Numbers
    5. 3-5. Performing Calculations with Complex Numbers
    6. 3-6. Formatting and Parsing Complex Numbers
    7. 3-7. Calculating Monetary Values
    8. 3-8. Randomly Generating Values
    9. 3-9. Obtaining the Current Date
    10. 3-10. Adding and Subtracting Days, Months, and Years
    11. 3-11. Finding the Difference Between Two Dates
    12. 3-12. Formatting Dates for Display
    13. 3-13. Comparing Dates
    14. 3-14. Writing Readable Numeric Literals
    15. 3-15. Declaring Binary Literals
  12. Chapter 4: Data Structures, Conditionals, and Iteration
    1. 4-1. Defining a Fixed Set of Related Constants
    2. 4-2. Designing Intelligent Constants
    3. 4-3. Executing Code Based Upon a Specified Value
    4. 4-4. Working with Fix-Sized Arrays
    5. 4-5. Using Generic Types
    6. 4-6. Working with Dynamic Arrays
    7. 4-7. Making Your Objects Iterable
    8. 4-8. Iterating Over a Map
  13. Chapter 5: Input and Output
    1. 5-1. Serializing Java Objects
    2. 5-2. Serializing Java Objects More Efficiently
    3. 5-3. Serializing Java Objects as XML
    4. 5-4. Creating a Socket Connection and Sending Serializable Objects Across the Wire
    5. 5-5. Obtaining the Java Execution Path
    6. 5-6. Copying a File
    7. 5-7. Moving a File
    8. 5-8. Creating a Directory
    9. 5-9. Iterating Over Files in a Directory
    10. 5-10. Querying (and Setting) File Metadata
    11. 5-11. Monitoring a Directory for Changes
    12. 5-12. Reading Property Files
    13. 5-13. Uncompressing Compressed Files
  14. Chapter 6: Exceptions and Logging
    1. 6-1. Catching Exceptions
    2. 6-2. Guaranteeing that Cleanup Code Runs, No Matter What
    3. 6-3. Throwing Exceptions
    4. 6-4. Catching Multiple Exceptions
    5. 6-5. Catching the Uncaught Exceptions
    6. 6-6. Managing Resources with try/catch Blocks
    7. 6-7. Creating an Exception Class
    8. 6-8. Rethrowing the caught Exception
    9. 6-9. Logging Events in Your Application
    10. 6-10. Rotating and Purging Logs
    11. 6-11. Logging Exceptions
  15. Chapter 7: Object-Oriented Java
    1. 7-1. Controlling Access to Members of a Class
    2. 7-2. Making Private Fields Accessible
    3. 7-3. Creating a Class That Can Have Only One Instance
    4. 7-4. Generating Instances of a Class
    5. 7-5. Creating Reusable Objects
    6. 7-6. Defining an Interface for a Class
    7. 7-7. Constructing Instances of the Same Class with Different Values
    8. 7-8. Interacting with a Class via Interfaces
    9. 7-9. Making a Class Cloneable
    10. 7-10. Comparing Objects
    11. 7-11. Extending the Functionality of a Class
    12. 7-12. Defining a Class Template
    13. 7-13. Increasing Class Encapsulation
  16. Chapter 8: Concurrency
    1. 8-1. Starting a Background Task
    2. 8-2. Updating (and Iterating) a Map
    3. 8-3. Inserting a Key into a Map Only If the Key is not Already Present
    4. 8-4. Iterating Through a Changing Collection
    5. 8-5. Coordinating Different Collections
    6. 8-6. Splitting Work into Separate Threads
    7. 8-7. Coordinating Threads
    8. 8-8. Creating Threadsafe Objects
    9. 8-9. Implementing Threadsafe Counters
    10. 8-10. Breaking Down Tasks into Discrete Units Of Work
  17. Chapter 9: Debugging and Unit Testing
    1. 9-1. Understanding Exceptions
    2. 9-2: Locking Down Behavior of Your Classes
    3. 9-3. Scripting Your Unit Tests
    4. 9-4. Determining Code Coverage
    5. 9-5. Finding Bugs Early
    6. 9-6. Monitoring Garbage Collection in your Application
    7. 9-7: Spotting Memory Leaks in Your Application
    8. 9-8. Getting a Thread Dump
  18. Chapter 10: Unicode, Internationalization, and Currency Codes
    1. 10-1. Converting Unicode Characters to Digits
    2. 10-2. Creating and Working with Locales
    3. 10-3. Setting the Default Locale
    4. 10-4. Searching Unicode with Regular Expressions
    5. 10-5. Overriding the Default Currency
    6. 10-6. Converting Byte Arrays to and from Strings
    7. 10-7. Converting Character Streams and Buffers
  19. Chapter 11: Working with Databases
    1. 11-1. Connecting to a Database
    2. 11-2. Handling Connection and SQL Exceptions
    3. 11-3. Querying a Database and Retrieving Results
    4. 11-4. Performing CRUD Operations
    5. 11-5. Simplifying Connection Management
    6. 11-6. Guarding Against SQL Injection
    7. 11-7. Performing Transactions
    8. 11-8. Creating a Scrollable ResultSet
    9. 11-9. Creating an Updatable ResultSet
    10. 11-10. Caching Data for Use When Disconnected
    11. 11-11. Joining RowSet Objects When Not Connected to the Data Source
    12. 11-12. Filtering Data in a RowSet
    13. 11-13. Querying and Storing Large Objects
    14. 11-14. Storing Array Values
    15. 11-15. Retrieving Array Values
    16. 11-16. Invoking Stored Procedures
    17. 11-17. Handling Resources Automatically
  20. Chapter 12: Java 2D Graphics
    1. Helper Class for This Chapter
    2. 12-1. Creating Points
    3. 12-2. Drawing Lines
    4. 12-3. Drawing Shapes
    5. 12-4. Filling Shapes
    6. 12-5. Gradients
    7. 12-6. Transforming Shapes
    8. 12-7. Making Complex Shapes
    9. 12-8. Creating Interactive Shapes
    10. 12-9. Changing Text Font
    11. 12-10. Adding Attributes to Text
    12. 12-11. Measuring Text
    13. 12-12. Display Multiple Lines of Text
    14. 12-13. Adding Shadows to Drawings
    15. 12-14. Printing Documents
    16. 12-15. Loading and Drawing an Image
    17. 12-16. Altering an Image
    18. 12-17. Storing an Image
  21. Chapter 13: Java3D
    1. 13-1. Installing Java3D
    2. 13-2. Creating a Simple 3D Object
    3. 13-3. Transforming Objects
    4. 13-4. Animating a 3D object
    5. 13-5. Navigating the Created 3D Universe
    6. 13-6. Responding to Keyboard Events
    7. 13-7: Changing an Object's Lighting
  22. Chapter 14: Swing API
    1. 14-1. Creating a GUI
    2. 14-2. Running a Swing Application
    3. 14-3. Adding Components to a GUI
    4. 14-4. Laying Out GUI Components
    5. 14-5. Generating Events with Buttons
    6. 14-6. Refreshing a User Interface
    7. 14-7. Submitting Form Values to a Database
    8. 14-8. Making a Multi-Window Program
    9. 14-9. Adding a Menu to an Application
    10. 14-10. Adding Tabs to a Form
    11. 14-11. Drawing on a Canvas
    12. 14-12. Generating and Laying Out Icons
    13. 14-13. Designing and Manipulating Borders
    14. 14-14. Creating Text Components
    15. 14-15. Associating Action Objects with Editing Commands
    16. 14-16. Creating Keyboard Shortcuts
    17. 14-17. Creating a Document
    18. 14-18. Developing a Dialog Box
    19. 14-19. Associating Listeners with a Document
    20. 14-20. Formatting GUI Applications with HTML
    21. 14-21. Changing the Look and Feel of a GUI
    22. 14-22. Distributing a Swing Application
    23. 14-23. Creating an Animation
    24. 14-24. Working with the JLayer Component
    25. 14-25. Adding Printing Support to Swing Components
  23. Chapter 15: JavaFX Fundamentals
    1. 15-1. Installing Required Software
    2. 15-2. Creating a Simple User Interface
    3. 15-3: Drawing Text
    4. 15-4: Changing Text Fonts
    5. 15-5. Creating Shapes
    6. 15-6. Assigning Colors to Objects
    7. 15-7. Creating Menus
    8. 15-8. Adding Components to a Layout
    9. 15-9. Generating Borders
    10. 15-10. Binding Expressions
    11. 15-11. Creating and Working with Observable Lists
    12. 15-12. Generating a Background Process
    13. 15-13. Associating Keyboard Sequences to Applications
    14. 15-14. Creating and Working with Tables
    15. 15-15. Organizing UI with Split Views
    16. 15-16. Adding Tabs to the UI
    17. 15-17. Developing a Dialog Box
  24. Chapter 16: Graphics with JavaFX
    1. 16-1. Creating Images
    2. 16-2. Generating an Animation
    3. 16-3. Animating Shapes Along a Path
    4. 16-4. Manipulating Layout via Grids
    5. 16-5. Enhancing with CSS
  25. Chapter 17: Media with JavaFX
    1. 17-1. Playing Audio
    2. 17-2. Playing Video
    3. 17-3. Controlling Media Actions and Events
    4. 17-4. Marking a Position in a Video
    5. 17-5. Synchronizing Animation and Media
  26. Chapter 18: Working with Servlets and Applets
    1. 18-1. Setting Up a Servlet Environment
    2. 18-2. Developing a Servlet
    3. 18-3. Packaging, Compiling, and Deploying a Servlet
    4. 18-4. Registering Servlets without WEB-XML
    5. 18-5. Setting Initialization Parameters
    6. 18-6. Handling Requests and Responses
    7. 18-7. Setting Application-Wide Parameters
    8. 18-8. Filtering Web Requests
    9. 18-9. Forwarding Requests to Other Web Resources
    10. 18-10. Listening for Servlet Container Events
    11. 18-11. Listening for Attribute Changes
    12. 18-12. Managing Session Information
    13. 18-13. Finalization of a Servlet
    14. 18-14. Creating an Applet
    15. 18-15. Packaging an Applet and Embedding into a Web Page
    16. 18-16. Creating Draggable Applets
    17. 18-17. Loading External Libraries for an Applet
    18. 18-18. Using Swing Components Within an Applet
  27. Chapter 19: Intro to Android
    1. 19-1. Setting Up a Development Environment with Eclipse
    2. 19-2. Creating a Basic Application
    3. 19-3. Compiling and Testing in an Emulator
    4. 19-4. Constructing a User Interface with XML Layout
    5. 19-5. Constructing a User Interface Programmatically
    6. 19-6. Handling Application Events and Activity Changes
    7. 19-7. Tying All Application Components Together
    8. 19-8. Handling Incoming Call Events
    9. 19-9. Building a Tabbed User Interface
    10. 19-10. Embedding Web Pages into an Application View
    11. 19-11. Granting Application Device Permissions
  28. Chapter 20: JavaFX on the Web
    1. 20-1. Embedding JavaFX Applications in a Web Page
    2. 20-2. Displaying HTML5 Content
    3. 20-3. Manipulating HTML5 Content with Java Code
    4. 20-4. Responding to HTML Events
    5. 20-5. Displaying Content from the Database
  29. Chapter 21: E-mail
    1. 21-1. Installing JavaMail
    2. 21-2. Sending an E-mail
    3. 21-3. Attaching a File to an E-mail Message
    4. 21-4. Sending E-mail to a Group
    5. 21-5. Checking E-mail
    6. 21-6 Monitoring an E-mail Account
  30. Chapter 22: XML Processing
    1. 22-1. Writing an XML File
    2. 22-2. Reading an XML File
    3. 22-3. Transforming XML
    4. 22-4. Validating XML
    5. 22-5. Creating Java Bindings for an XML Schema
    6. 22-6. Unmarshalling XML to a Java Object
    7. 22-7. Building an XML Document with JAXB
  31. Chapter 23: Networking
    1. 23-1. Defining a Network Connection to a Server
    2. 23-2. Listening for Connections on the Server
    3. 23-3. Bypassing TCP for InfiniBand to Gain Performance Boosts
    4. 23-4. Broadcasting to a Group of Recipients
    5. 23-5. Generating and Reading from URLs
    6. 23-6. Parsing a URL
  32. Index