Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Swing Book, Front Matter. Easy for reading, Click here!

Custom Search
Swing Book, Front Matter. Easy for reading, Click here!

[ Return to Swing (Book) ]




Swing

Matt: To my parents, Joan M. Robinson and Kenneth B. Robinson, D.M.D.

Pavel: To my wife Marina Vorobiev.

Foreword

by Kirk Brown

When I was running an Engineering group in an early Java startup software company in 1996, we were building a commercial Java 1.0 application for the U.S. Postal service that was being offered to an end user population of 50,000 people. These users had a variable mix of old and new PCs, Macs and UNIX boxes. Implementing a user interface, based on early AWT, that was consistent across multiple platforms was nearly impossible to say the least. It was painful and meticulous work using the early layout managers in aligning the panels and hundreds of AWT widgets to match a two-sided fill-in-the-form paper specification for the GUI. The Postal Service had been using a paper form for years and they wanted their thousands of bulk mailers to avoid any re-training with the electronic version over the Web. This seemed like an easy request at the time, until we got into the guts of managing hundreds of AWT widgets in an attempt to replicate the paper form. The engineering team kept saying, “There's got to be a better way in dealing with AWT every time a change to the GUI was requested!�? At this point in time, all a manager could do was offer more free pizza and caffeine, and pray that the rumors of some GUI foundation classes would soon be released by Sun Microsystems and Netscape.


It was late in the Spring of 1996 that I first heard James Gosling had pulled a team together to begin working on a new set of GUI components called Swing. It would be too late for my Postal project, but knowing that future GUI development would be accelerated and would be more efficient was a great relief.


A common requirement of many Java applications these days is cross-platform interoperability, or an application that just works well on a single platform. What GUI tools does one select for this? If it runs on a Mac, don't you want the Mac users to be familiar with the interface controls? What if you are programming an interactive Java Web game? Shouldn't the interface look customized and match the game's theme? If your application isn't platform specific and you want it distributed across multiple platforms; isn't there a generic GUI that can be used for this scenario?


The answer to these questions is the Java Foundation Classes (JFC) which include the components from the original Swing project. JFC is an official part of the Java core that includes AWT, Swing, Java 2D, Drag and Drop, and Accessibility for users with disabilities. Not only can you choose well-known appearances and behaviors of windowing components, such as Mac OS, Windows, or Motif; but you can use the provided cross-platform Java Look and Feel or invent your own custom L&F.


During a recent technical Java presentation at a very large company (with an army of programmers representing a number of senior backgrounds, such as C/C++, Visual Basic and others), the engineers began complaining of the many short-comings they were having with Java. Most had implemented several large Java applications over the last 10 months. The griping grew and grew, one comment dove-tailing another with, “Yea, I found the way Java handles exceptions to be impossible to deal with in my situation ...�? and “I agree with Mike, we have been able to get it to work on a Mac, PC and Unix but ...�? and so on. After the ground-swell of intelligent remarks and points of view died down a bit, I looked across the talented group of gurus and offered, “Well, you can always go back and try to do this in C++.�? All at once their faces cringed as if they all had bitten into a sour lemon at the same time. Some grabbed their heads as if suffering from a hang-over. A few applauded and laughed with Java-biased glee. Let's face it, Java makes our programming lives easier and now it only gets better with Swing.

 

After completing a few projects using the Swing components and infrastructure discussed in this book, you might have the same reaction when a return to VB, MFC or C++ is suggested. At any rate, within these pages you will find a superb programmer's guide that provides an exhaustive coverage of Swing. A particularly excellent job was done presenting each component with a solid and detailed explanation. This, combined with numerous light-shedding examples, has resulted in the best comprehensive Swing book available today.


Kirk Brown

Senior Engineer

Sun Microsystems Advanced Technology Group

 

Preface

 

Audience

 

This book is best described as a programmer’s guide, serving both as a reference and a tutorial. We place an emphasis on using Swing to solve a broad selection of realistic and creative problems. We assume an intermediate knowledge of Java, including the basics of putting together an AWT-based GUI, how the 1.1 event model works, and familiarity with anonymous and explicit inner classes. Those who do not have this background can pick it up in any beginner book on AWT or Swing. We recommend Manning’s own Up to Speed With Swing, second edition, by Steven Gutz.

 

Our goal was to write a book that contains enough explanation and examples to satisfy the most demanding Swing developer, while still catering to the motivated newcomer. We feel that we have accomplished this goal in a unique and generous way. From day one our manuscript has been freely available on the web. By responding and reacting to all feedback and suggestions we received, this book has taken on many diverse perspectives. Together with our own efforts, the Swing developer community has helped shape this book into its current form.

Organization

In general, each chapter starts with class and interface explanations, occasionally interspersed with small examples to demonstrate key features. The bulk of each chapter is then devoted to the construction of several examples, often building on top of previous examples, illustrating more complex aspects of the components under investigation.

 

Part I contains two chapters that introduce Swing and discuss the most significant mechanisms underlying it. Chapter 1 is a brief overview that we suggest for all Swing newcomers. More experienced developers will want to read straight through most of chapter 2, as it provides an understanding of Swing’s most significant underlying behavior. This chapter is refererenced throughout the book, and we expect all readers to refer to it often. We recommend that Swing beginners skim this chapter to at least get a rough idea of what is covered.

 

Part II consists of twelve chapters covering all the basic Swing components with detailed descriptions and nontrivial examples. These chapters discuss the bread and butter of Swing-based GUIs, and each comes packed with usage guidelines written by David Anderson, a usability and interface design expert.

 

Part III contains seven chapters dealing with the more advanced components. These chapters are significantly more complex than those in part II, and require a thorough understanding of Swing’s architecture, as well as the basic Swing components.

 

Part IV consists of six chapters on special topics with a focus on Swing: Printing, Java2D, Accessibility, JavaHelp, CORBA, and contributions from several experienced Swing developers. These chapters vary in complexity. Unfortunately, due to space limitations, we were unable to include the chapters on Accessibility, JavaHelp, CORBA, and contributions. However, they are available to all book owners online at the book’s home page.

 

Most examples are presented in three distinct parts:

The Code: After a general introduction to the example, including one or more screenshots, the underlying code is listed. Annotations appear to the right of significant blocks of code to provide a brief summary of its purpose. Each annotation has a number which links it to the explanation of that code in the “Understanding the Code�? section.

Understanding the Code: This section consists of a detailed explanation of the code. Most paragraphs are accompanied by a number which links that text with the associated annotated code listed in “The Code�? section.

Running the Code: After the code is explained, this brief section provides suggestions for testing out the program. This may also include references and suggestions for taking the example further.

Conventions

{INSERT NOTE ICON HERE} Throughout the book we point out specific behaviors or functionality that either differs from what is expected, or can be achieved through alternate techniques. We also use this icon to denote various other types of notes, such as a reference or suggested background knowledge for material being discussed.

 

{INSERT BUG ALERT ICON HERE} Occasionaly incorrect or unexpected behavior is caused by known Swing bugs. We do not attempt to hide, or gloss over these. Rather, we explicitly discuss these bugs and explain possible workarounds or fixes where applicable.

 

{INSERT UI GUIDELINE ICON HERE} David Anderson, a usability and interface design expert, has provided detailed usage guidelines throughout the book. These guidelines do not represent hard and fastened rules, but are highly recommended for the development of consistent, user-friendly interfaces (see the Bibliography for David’s references and recommended UI Design readings).

 

All source code appears in Courier font. For example:

 

  public void main( String args[] ) {

    Example myExample = new Example();

  }

 

We prefix all instance variables with “m_,�? and capitalize all static variables with underscores separating compound words. For example:

 

  protected int m_index;

  private static int INSTANCE_COUNT;

 

Many examples are built from examples presented earlier in the book. In these cases we have minimized the amount of repeated code by replacing all unchanged code with references to the sections that contain that code. All new and modified code of any class is highlighted in bold. When a completely new class is added, we do not highlight that class in bold (the only exceptions to this rule are anonymous inner classes).



[ Return to Swing (Book) ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy