Copy Link
Add to Bookmark
Report

Java Coffee Break Newsletter Volume 2 Issue 01

             Java Coffee Break Newsletter Volume 2, Issue 1 
http://www.davidreilly.com/jcb/
ISSN 1442-3790

=========================================================================

In this issue

* Java Book Selections
* Article : Unraveling threads
* Q&A : How do I use stacks?
* Q&A : How do I detect if command line arguments are empty?

=========================================================================

Java Book Selections

With the release of Java 1.2, and the SWING/JFC packages, even experts
have a little catching up to do - let alone beginners! We thought this
selection of books might help you get up to speed. There's nothing like
a book, whether to learn new concepts and techniques, or as a desktop
reference to consult as you program. You can order these books from your
local bookstore, or online for convenience through Amazon.com


Core Java Foundation Classes
by Kim Topley

Review courtesy of Amazon.com

With the arrival of Sun's Java Foundation Classes (JFC), developers can
write Java applications that behave just like "native"-style
applications, such as those written in Windows. Core Java Foundation
Classes shows you how you can take advantage of the new JFC Swing classes
to create Java applications with all the advanced features that today's
users expect.

After a tour of the JFC/Swing packages, the author jumps right into
building basic Swing applications, focusing on layout managers (which
control the arrangement of JFC components onscreen) and basic graphics
programming. The book aims to let you create sophisticated Java
applications, and so the author discusses topics such as menus and the
toolbar, keyboard handling (and mouseless operation), and dialog boxes to
let you do this. The book also covers powerful Swing components, the tree
and table controls. The author even looks at creating multiple document
interface (MDI) applications. --Richard Dragan

[more] http://www.davidreilly.com/goto.cgi?isbn=0130803014

Graphic Java 1.2, Mastering the Jfc : Swing (Java Series , Vol 2)
by David Geary

Review courtesy of Amazon.com

Java developers know that the Abstract Windowing Toolkit (AWT) is still
an effective way to build functional user interfaces, whether inside Web
browsers or in stand-alone applications. David Geary's guide to AWT
programming in Graphic Java 1.2 shows you all the basics of AWT newly
refitted for the new Java Developer's Kit (JDK) 1.2 standard from Sun,
including some excellent 2-D animation examples.

After introducing the basics of the AWT library, the author looks at
simple painting and 2-D graphics. Standout sections here look at
displaying images--a staple of Internet programming--as well as
transforming images with "filters" (such as dimming and rescaling an
image). Graphic Java 1.2 shows you a variety of advanced techniques
for getting control of your bit-mapped content. The tour of Java
graphics capabilities moves on with coverage of layout managers and
basic AWT components such as buttons, text fields, lists, and choices.
For those who want to create stand-alone Java applications, there's much
material on using Java menus.

The book's advanced material highlights new features of JDK 1.2, such as
its support for lightweight controls, built-in double-buffering support,
and advanced "native-style" operations (such as clipboard support and
"rubber-banding" classes). The author also shows off his custom classes
for 2-D sprite animation. --Richard Dragan

[more] http://www.davidreilly.com/goto.cgi?isbn=0130796662

Java Swing
by Robert Eckstein, Marc Loy & David Wood

Review courtesy of Amazon.com

Java Swing is an excellent introduction to the latest developments in
Java-interface technology. The authors explain how (and why) to use Swing
components, and meanwhile proceed to document the entire Swing API with
the thoroughness and accuracy programmers have come to expect from
O'Reilly & Associates.

Eckstein, Loy, and Wood start with an architectural overview of Swing and
its relationship to the Abstract Window Toolkit (AWT) and the rest of
Java. They talk a little bit about converting programs from the old AWT
to the Swing-enhanced AWT, and explain how Swing manages components'
"look and feel" characteristics. There's also coverage of actions, which
are among Swing's handiest new features.

From that point, they proceed to guide readers through the Swing forest,
pointing out all the important stuff along the way. Mostly, this tour
takes the form of graphical user interface (GUI) component documentation,
with chapters devoted to buttons, lists, tables, panes, and the other
thingamajigs you can put on-screen with Swing. All the classes in each
category get entries, many of which include good commentary and some
examples. The authors give some attention to the Accessibility API and
its associated utilities, too.

A detailed chapter that walks the reader through the process of creating
a custom look and feel distinguishes Java Swing from its competitors -
this potentially confusing process is explained clearly and thoroughly.
--David Wall


[more] http://www.davidreilly.com/goto.cgi?isbn=156592455X


Java 1.2 Unleashed
by Jamie Jaworski

The "Java Unleashed" series of books have been an extremely popular, not
to mention extremely useful series. As references, and instructional
guides, the "Java Unleashed" series are an invaluable resource. The
latest in the line, "Java 1.2 Unleashed" is no exception - covering
a wide range of new topics, such as SWING/JFC, Java-Media Framework,
networking, RMI & CORBA. A great guide for intermediate to advanced
developers.

[more] http://www.davidreilly.com/goto.cgi?isbn=1575213893


Just Java 1.2
by Peter van der Linden

"Just Java 1.2" is the sequel to the incredibly popular "Just Java"
by Peter van der Linden, author of the "Java Programmers FAQ". Just
published, this book is one of the most up-to-date Java 1.2
resource.

[more] http://www.davidreilly.com/goto.cgi?isbn=0130105341


Upcoming titles

Here are a selection of titles that are scheduled for release in the
next few months. You might like to keep your eyes out for these at
your local bookstore, or you can order advance copies online through
Amazon.com.


The Jfc Swing Components : A Tutorial Guide for Constructing Gui's
by Kathy Walrath http://www.davidreilly.com/goto.cgi?isbn=0201433214

Pure Jfc : The Swing Toolkit (The Pure Series)
by Satyaraj Pantham http://www.davidreilly.com/goto.cgi?isbn=0672314231

Swing Reference : User Interfaces With Java Foundation Classes
by Stephen C. Drye, http://www.davidreilly.com/goto.cgi?isbn=1884777678

Jfc Unleashed (The Unleashed Series)
by Michael Foley, http://www.davidreilly.com/goto.cgi?isbn=0789714663

Java Foundation Classes in a Nutshell : A Desktop Quick Reference for
Java Programmers
by David Flanagan http://www.davidreilly.com/goto.cgi?isbn=1565924886




=========================================================================

Unraveling threads

Multi-threaded programming offers developers substantial
advantages, and users increased performance. Despite the
obvious benefits, novice programmers are reluctant to
write multi-threaded applications because of their
perceived complexity. This article will offer a gentle
introduction to multi-threaded programming, as well as
offering solutions for synchronization of threads.


So, what exactly are threads? Threads are multiple instances of an
application or applet that execute concurrently.

Woah! That's a mouthful isn't it. Let's break it down a little.

When we start an application, it executes in its own process - it has its
own chunk of memory for data storage, and it runs alongside other
processes. Modern operating systems (such as Windows, and Unix) create
the illusion that these processes are running concurrently. A process can
execute at the same time as another process, and the operating shares
time between all running processes (though not always evenly). Each
process represents a single thread of execution.

Now a multi-threaded application is a special kind of application. It
uses only a single process, but that process has multiple threads of
execution. The same code and data is used, but we have more than one
instance of the application running. Each thread runs concurrently, so
when one thread is idle (perhaps waiting for data input), another thread
can be performing some useful task. Easy huh?


What can threads do?

Threads are extremely useful, because they allow developers to write
applications that are more responsive to the user. When your application
is processing data, its poor design for the user-interface to be disabled
until the task is complete. If your application is single-threaded, the
user-interface can't respond to user commands if the application is
performing another task. Furthermore, applications can their idle time to
process things behind the scenes. For example, an applet that displays
some animation can be preparing the next frame.


So how do I use threads?

Threads are actually quite easy to use, provided you do a little
planning. First you have to identify the tasks a thread will perform.
Usually your thread(s) will perform processing, allowing your primary
thread to handle user-interface events. Once you've identified a task,
you have to write the code for your thread.

Our example will be an applet that displays a simple text scrolling
animation. We'll create a new thread to create the animation, leaving
our application free to respond to the user interface. When a user clicks
on the applet, animation stops. When the mouse button is released, the
animation resumes. Other than these two actions, all our applet does is
launch a second thread to perform the animation. Let's look at the thread
specific code.


Creating a new thread

Creating a new thread is pretty simple, and takes only a few lines of
code. In our example, the new thread is created in the applet's start()
method. We first check to see if an animation thread is already running,
and if not, create one.

    // Pass our thread an instance of java.lang.Runnable (us)
    AnimationThread = new Thread(this);

Notice the parameter we use when creating a new Thread object. This form
of constructor accepts an instance of java.lang.Runnable, which is an
interface our applet implements. We'll see how this works later, but for
those who are curious, it is an interface that indicates our applet has
work a thread can perform. We use the this keyword to indicate that the
current class/object is being referred to.

Next, we call that thread's start method. Once started, a thread will
execute independently, until it completes its task, or the thread is
suspended or stopped. Here's the full code for our start method.

// Check to see if thread is active
if (AnimationThread == null)
{
    // Pass our thread an instance of java.lang.Runnable (us)
    AnimationThread = new Thread(this);

    // Start the thread
    AnimationThread.start();
}


For the full article, including an example applet and source code, see
http://www.davidreilly.com/jcb/articles/threads/

=========================================================================

Q&A : How do I detect if command line arguments are empty?

Whenever you write Java applications, its vital that you check your
command line arguments before using them. Not all users will remember to
put in the correct number of parameters, and your application will
terminate with an ArrayIndexOutOfBoundsException. Fortunately, its
extremely easy to check!

Here's an example program, that checks that at least one parameter
exists :

public class argsdemo
{
public static void main(String args[])
{
if (args.length == 0)
{
System.err.println ("No arguments!");
System.exit(0);
}
}
}

Just add a if statement before using a parameter, and then perhaps exit
with a message. You can check for a specific number of parameters, and
terminate if they're missing.

=========================================================================

Q&A : How do I use stacks in Java?

A stack is a data structure that allows data to be inserted (a 'push'
operation), and removed (a 'pop' operation). Many stacks also support a
read ahead (a 'peek' operation), which reads data without removing it. A
stack is a LIFO-queue, meaning that the last data to be inserted will be
the first data to be removed.

When we insert data into a stack, it is placed at the head of a queue.
This means that when we remove data, it will be in reverse order. Adding
1,2,3,4 will return 4,3,2,1. Stacks aren't the most frequently used data
structure, but they are extremely useful for certain tasks.

+-------+ +-------+
+ 3 + + 4 +
+ 2 + + 3 +
+ 1 + + 2 +
+-------+ + 1 +
+-------+

Figure 1.1 Stack before push operation, and stack after


+-------+ +-------+
+ 4 + + 3 +
+ 3 + + 2 +
+ 2 + + 1 +
+ 1 + +-------+
+-------+

Figure 1.1 Stack before pop operation, and stack after


Java provides a stack implementation, in the form of java.util.Stack.
Stack's are a subclass of java.util.Vector, and share some similarities.
A Vector, after all, is a queue, and a Stack is an ordered LIFO queue.

Stacks are quite easy to use. Here's a sample application that uses a
stack to reverse a list of numbers.

import java.util.Stack;

public class StackDemo
{
public static void main(String args[])
{
// Create a new, empty stack
Stack lifo = new Stack();

// Let's add some items to it
for (int i = 1; i <= 10; i++)
{
lifo.push ( new Integer(i) );
}

// Last in first out means reverse order
while ( !lifo.empty() )
{
System.out.print ( lifo.pop() );
System.out.print ( ',' );
}

// Empty, let's lift off!
System.out.println (" LIFT-OFF!");
}

}

For more information, see the Java documentation for java.util.Stack.

=========================================================================

The Java Coffee Break Newsletter is only sent out to email subscribers
who have requested it, and to readers of the comp.lang.java.programmer
and comp.lang.java.help newsgroups.

If you'd like to receive our newsletter, and get the latest Java
news, tips and articles from our site, then get your FREE subscription
from

http://www.davidreilly.com/jcb/newsletter/

If you are an email subscriber and no longer wish to receive the JCB
Newsletter, please unsubscribe using the WWW form located at

http://www.davidreilly.com/jcb/newsletter/unsubscribe.html

← previous
next →
loading
sending ...
New to Neperos ? Sign Up for free
download Neperos App from Google Play
install Neperos as PWA

Let's discover also

Recent Articles

Recent Comments

Neperos cookies
This website uses cookies to store your preferences and improve the service. Cookies authorization will allow me and / or my partners to process personal data such as browsing behaviour.

By pressing OK you agree to the Terms of Service and acknowledge the Privacy Policy

By pressing REJECT you will be able to continue to use Neperos (like read articles or write comments) but some important cookies will not be set. This may affect certain features and functions of the platform.
OK
REJECT