Java Coffee Break Newsletter Volume 2, Issue 8 http://www.javacoffeebreak.com/ ISSN 1442-3790 ================================================================ In this issue * We've moved - to www.javacoffeebreak.com * Java in the news * Book Reviews - Java Programming for Beginners * Tutorial - Get your GUI Swinging! * Q&A : How do I minimize and restore frames and JFrames? ================================================================ We've moved location Over the past two years of publication, the Java Coffee Break has gone from strength to strength. What started out as a small hobby site has become a fully fledged e-zine publication. I'd like to thank all my readers, both of the online site and this newsletter, for their interest and support over the years. With the large increase in visitors to the site, and subscribers to this newsletter, the time has come for the Java Coffee Break to move to its own domain name. You can now find all of our published articles, tutorials, reviews & FAQ's at the a new location, http://www.javacoffeebreak.com/ ================================================================ In the News Here are a selection of recent news items that may be of interest to Java developers. /*/ HotSpot Performance Engine 1.0.1 now available Sun has released an early access version of their HotSpot performance engine, through the Java Developer Connection (JDC). The list of features it boasts is impressive to say the least - "pauseless" garbage collection, and a fourfold increase over that of Java2. - dynamic compilation, and on-the-fly optimization of performance critical code. To download HotSpot for yourself, goto http://developer.java.sun.com/developer/ ================================================================ Book Reviews - Java Programming for Beginners Java Gently Author : Judy Bishop Publisher : Addison-Wesley ISBN : 0201342979 *This review relates to the first edition* Java Gently teaches the principles of computer programming, and the Java language, rather than just teaching Java in isolation. Unlike other books, it requires no previous programming experience, because it teaches fundamental concepts alongside Java. It has a good emphasis on object orientated design; class diagrams are used extensively throughout the book to make it easy to understand how examples work. Working through the book will teach you how to program - not just how to write simple applets. This isn't a quick guide to Java, so not everyone will appreciate this approach. Examples in the book teach a wide range of topics, from simple concepts like conditional branching and looping, to high level concepts like exception handling, abstraction, data structures and networking. The approach taken by the book introduces topics gradually, and makes it easy to pick up the skills needed to program in Java. Other books attempt to teach a narrow range of topics, such as Java applets or user interfaces within a very short time-frame, whereas this book has a more broad scope that will make future programming easier. If Java is your first programming language, and you're considering it as a career, you should make this one of your first books.--David Reilly. For more information about this title, or to order it, visit http://www.davidreilly.com/goto.cgi?isbn=0201342979 Beginning Java 2 Author : Ivor Horton Publisher : Wrox Press ISBN : 1861002238 Review Courtesy of Amazon.com Beginning Java 2 is one of the two best introductory Java 2 books available. (The other is Peter van der Linden's Just Java 1.2.) Assuming nothing more than curiosity and tenacity, this book explains how to create programs with the Java programming language. And not just simple, academic programs either--the applets and applications that Ivor Horton describes in later chapters take advantage of the latest features of Java 2. After a brief introduction to the characteristics of Java, Beginning Java 2 digs into variables, data types, operators, control structures, and basic Java syntax--the stuff you absolutely have to understand in order to get anything done. Horton then explains streams, files, and threads before getting into the graphical stuff, where he details how to build attractive, functional user interfaces with the Swing components (with solid coverage of the Java 2 event model). Later chapters address Java2D graphics and database connectivity. The author treats object orientation as integral to the rest of Java programming, which is appropriate to the language. Readers get to see how concepts work, as regular Try It Out sections include illustrative code listings and the resulting output. The author and publisher deserve kudos for printing the complete source code of example programs rather than just key excerpts. These example programs also appear on the publisher's Web site. --David Wall For more information about this title, or to order it, visit http://www.davidreilly.com/goto.cgi?isbn=1861002238 Just Java 1.2 Author : Peter van der Linden Publisher : Prentice Hall ISBN : 0130105341 Review Courtesy of Amazon.com So, you're starting from zero in an effort to learn the Java programming language. What book will help you most? Just Java 1.2 should be on your desktop. It's one of a select group of introductory Java books that honestly earns its cover price. Shying away from complicated aspects of the language that are seldom used in most practical situations, Peter van der Linden focuses his considerable teaching skills upon the parts of the language you really need to understand in order to be a good Java programmer. He covers basic structure and syntax very well, and gives similarly excellent attention to object orientation and the means by which Java implements it. Applets, Beans, input/output streams, basic graphics, Swing, and security all get superb coverage. In short, this book explains everything you really need to know in order to write useful Java programs. With sharp text and very good example programs, the author shows you how to get things done. In the process, van der Linden--a funny guy--pauses often to show you how to make an origami water-bomber or comment upon software error messages like "runt packet." The CD-ROM that comes with this book deserves much praise, as well. Packed with honestly useful tools and all of the author's examples, it employs a fine HTML-based interface that other books ought to imitate. --David Wall. For more information about this title, or to order it, visit http://www.davidreilly.com/goto.cgi?isbn=0130105341 ================================================================ Get your GUI Swinging! By David Reilly Swing offers Java developers greater flexibility and control over the design of graphical user interfaces (GUI), as well as a suite of graphical components ranging from buttons to tables and trees. In this tutorial, we'll help you to put the GRR in swinger! Learn how to use Swing in your applications, to add that professional touch. What is Swing? With the release of the Java 2 platform (and the Java Development Kit 1.2), developers now have access to a wider range of graphical user interface components, and greater flexibility and control over their appearance. The previous graphics toolkit, the Abstract Windowing Toolkit (AWT), was sufficient for simple applets, but was a poor substitute for a commercial quality suite of components. As part of the Java Foundation Classes (JFC), the Swing library of components rectifies the shortcomings of AWT, and gives developers the ability to create professional graphical user interfaces that will be the envy of developers using other languages like Visual Basic or Delphi. The Swing API isn't a replacement for the AWT. Swing is designed to be used in conjunction with existing AWT components, and to compliment it. Not only does Swing offer new components, but it also offers greater control over their appearance, and a customizable "look and feel" (L&F), that allows applications to swing from a "Java look and feel" to a "Windows look and feel" on-the-fly. Developers even have the ability to create their own L&F, for custom applications or games. Getting started with Swing Developing applications with Swing user-interfaces is actually quite easy. You can even convert your existing applications over to use the new Swing components. In this tutorial, we'll show how to get your applications swinging. Learning about Swing The best way to learn about Swing is to actually use an application that has a Swing GUI. A good starting place is the SwingSet demo, that is shipped with JDK1.2. [Figure 1 - SwingSet Demo] http://www.javacoffeebreak.com/tutorials/swing/swing1.gif If you don't already have JDK1.2 installed, you'll need to install it to work with Swing applications. The latest version of JDK1.2 can be found at http://java.sun.com/ Running the SwingSet Demo The demo can be found in the demo/jfc/SwingSet directory, located under your JDK1.2 installation directory. From a DOS Prompt or shell window, change to this directory then run the SwingSet application. c:\>cd \jdk1.2\demo\jfc\SwingSet c:\jdk1.2\demo\jfc\SwingSet>java SwingSet The first thing you should notice is that the demonstration takes awhile to load - a window with a progress bar will be shown as the demonstration loads. The progress bar component is a nice way of letting users know that a slow application is still chugging along, and hasn't fallen over ;) Once the demonstration loads, you can then click on different tab buttons to view the various components and behaviors that make up the Swing API. Take some time to experiment and click on some of the components - you'll be pleasantly surprised at the functionality that's available. While you have the SwingSet demo running, take a look at the menu options. You can now trigger a menu using only the keyboard (e.g. ALT-F for the File menu). Take a look at the Options menu, and the radio buttons provided. These allow you to dynamically change the look-and-feel of components at run-time. [Figure 2 - L&F Options] http://www.javacoffeebreak.com/tutorials/swing/swing2.gif Switch between the Java L&F (also referred to as 'Metal'), and the Motif/Windows L&F. You'll notice that all of the components (including menus) change appearance. This is a very powerful feature of Swing, and gives users control over which GUI style is used. Personally, I think that the Metal L&F is very comfortable to use, though end-users may prefer to stick with Windows L&F. Once you've had a play around with Swing, you'll be reluctant to go back to the older AWT components. Remember however that only the Java 2 Platform supports Swing (though older Java 1.1 Virtual Machines can still run Swing if you include the components in the codebase of your application, for a large download). For the moment at least, it rules out developing Swing applets for normal browsing, unless the Java Plug-in is used to run them under a Java 2 Virtual Machine. For the complete tutorial, including diagrams, goto http://www.javacoffeebreak.com/tutorials/swing/ ================================================================ Q&A: How do I minimize and restore frames and JFrames? This question stumped me when I first took a look at it. There is no minimize or restore method in JFrame, or java.awt.Frame for that matter. However, I knew there had to be a way - as JFrames frequently need to be restored programmatically. I suspected that the problem was a difference in terminology, and after a little searching, found the answer. JFrame inherits the setState method from java.awt.Frame. This method allows you to change the state of a window from "iconified", back to "normal". This is, in actual fact, minimize and restore - but the documentation uses different terms. To minimize or restore a window, we simply call the setState method, and pass it a state parameter to indicate whether we want to minimize or restore the window. For example, to minimize a Frame (or subclass, such as JFrame), we pass the 'iconified' parameter myFrame.setState ( Frame.ICONIFIED ); To restore the frame to its normal state, we call the setState method with the 'normal' parameter myFrame.setState ( Frame.NORMAL ); To demonstrate this effect, I've written a small demonstration, which you can compile and run. In the following example, a new frame is created, and then minimized. After a short delay, it is restored again. import java.awt.*; public class FrameTest { public static void main (String args[]) throws Exception { // Create a test frame Frame frame = new Frame("Hello"); frame.add ( new Label("Minimize demo") ); frame.pack(); // Show the frame frame.setVisible (true); // Sleep for 5 seconds, then minimize Thread.sleep (5000); frame.setState ( Frame.ICONIFIED ); // Sleep for 5 seconds, then restore Thread.sleep (5000); frame.setState ( Frame.NORMAL ); // Sleep for 5 seconds, then kill window Thread.sleep (5000); frame.setVisible (false); frame.dispose(); // Terminate test System.exit(0); } } ================================================================ 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 & back issues from http://www.javacoffeebreak.com/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.javacoffeebreak.com/newsletter/unsubscribe.html