showMessageDialog( ) and JOptionPane. showConfirmDialog( ).
More to Swing
This section was meant only to give you an introduction to the power of Swing and to get you started so you could see how relatively simple it is to feel your way through the libraries. What you’ve seen so far will probably suffice for a good portion of your UI design needs. However, there’s a lot more to Swing – it’s intended to be a fully-powered UI design tool kit. If you don’t see what you need here, delve into the online documentation from Sun and search the Web. There’s probably a way to accomplish just about everything you can imagine.
Some of the topics that were not covered in this section include:
More specific components such as JColorChooser, JFileChooser, JPasswordField, JHTMLPane (which performs simple HTML formatting and display), and JTextPane (a text editor that supports formatting, word wrap, and images). These are fairly straightforward to use.
The new event types for Swing. In many ways, these are like exceptions: the type is what’s important, and the name can be used to infer just about everything else about them.
New layout managers: Springs & Struts (a la Smalltalk) and BoxLayout.
Splitter control: a divider style splitter bar that allows you to dynamically manipulate the position of other components.
JLayeredPane and JInternalFrame, used together to create child frame windows inside parent frame windows, to produce multiple-document interface (MDI) applications.
Pluggable look and feel, so you can write a single program that can dynamically adapt to behave as expected under different platforms and operating systems.
Custom cursors.
Dockable floating toolbars with the JToolbar API.
Double-buffering and Automatic repaint batching for smoother screen redraws.
Built-in “undo” support.
Drag and drop support.
Chapter 13: Creating Windows & Applets
595
Summary
Of all the libraries in Java, the AWT has seen the most dramatic changes from Java 1.0 to Java 1.2. The Java 1.0 AWT was roundly criticized as being one of the worst designs seen, and while it would allow you to create portable programs, the resulting GUI was “equally mediocre on all platforms.” It was also limiting, awkward, and unpleasant to use compared with native application development tools on a particular platform.
When Java 1.1 introduced the new event model and Java Beans, the stage was set – now it was possible to create GUI components that could be easily dragged and dropped inside visual application builder tools. In addition, the design of the event model and Beans clearly shows strong consideration for ease of programming and maintainable code (something that was not evident in the 1.0 AWT). But it wasn’t until the GUI components – the JFC/Swing classes – appeared that the job was finished. With the Swing components, cross-platform GUI programming can be a civilized experience.
Actually, the only thing that’s missing is the application builder tool, and this is where the real revolution lies. Microsoft’s Visual Basic and Visual C++ require their application builder tools, as does Borland’s Delphi and C++ Builder. If you want the application builder tool to get better, you have to cross your fingers and hope the vendor will give you what you want.
But Java is an open environment, and so not only does it allow for competing application builder environments, it encourages them. And for these tools to be taken seriously, they must support Java Beans. This means a leveled playing field: if a better application builder tool comes along, you’re not tied to the one you’ve been using – you can pick up and move to the new one and increase your productivity. This kind of competitive environment for GUI application builder tools has not been seen before, and the resulting competition can generate only positive results for the productivity of the programmer.
Exercises
1. Create an applet with a text field and three buttons. When you press each button, make some different text appear in the text field.
2. Add a check box to the applet created in Exercise 1, capture the event, and insert different text into the text field.
3. Create an applet and add all the components that cause action( ) to be called, then capture their events and display an appropriate message for each inside a text field.
4. Add to Exercise 3 the components that can be used only with events detected by handleEvent( ). Override handleEvent( ) and display appropriate messages for each inside a text field.
5. Create an applet with a Button and a TextField. Write a handleEvent( ) so that if the button has the focus, characters typed into it will appear in the TextField.