Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Thinking in Java
Prev Contents / Index Next

Exercises

Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from www.BruceEckel.com.

  1. Create an applet/application using the Console class as shown in this chapter. Include 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/application using Console. In the JDK documentation from java.sun.com, find the JPasswordField and add this to the program. If the user types in the correct password, use Joptionpane to provide a success message to the user.
  4. Create an applet/application using Console, and add all the Swing components that have an addActionListener( ) method. (Look these up in the JDK documentation from java.sun.com. Hint: use the index.) Capture their events and display an appropriate message for each inside a text field.
  5. Create an applet/application using Console, with a JButton and a JTextField. Write and attach the appropriate listener so that if the button has the focus, characters typed into it will appear in the JTextField.
  6. Create an applet/application using Console. Add to the main frame all the components described in this chapter, including menus and a dialog box.
  7. Modify TextFields.java so that the characters in t2 retain the original case that they were typed in, instead of automatically being forced to upper case.
  8. Locate and download one or more of the free GUI builder development environments available on the Internet, or buy a commercial product. Discover what is necessary to add BangBean to this environment and to use it.
  9. Add Frog.class to the manifest file as shown in this chapter and run jar to create a JAR file containing both Frog and BangBean. Now either download and install the Bean Builder from Sun, or use your own Beans-enabled program builder tool and add the JAR file to your environment so you can test the two Beans.
  10. Create your own JavaBean called Valve that contains two properties: a boolean called “on” and an int called “level.” Create a manifest file, use jar to package your Bean, then load it into the Bean Builder or into a Beans-enabled program builder tool so that you can test it.
  11. Modify MessageBoxes.java so that it has an individual ActionListener for each button (instead of matching the button text).
  12. Monitor a new type of event in TrackEvent.java by adding the new event handling code. You’ll need to discover on your own the type of event that you want to monitor.
  13. Inherit a new type of button from JButton. Each time you press this button, it should change its color to a randomly selected value. See ColorBoxes.java for an example of how to generate a random color value.
  14. Modify TextPane.java to use a JTextArea instead of a JTextPane.
  15. Modify Menus.java to use radio buttons instead of check boxes on the menus.
  16. Simplify List.java by passing the array to the constructor and eliminating the dynamic addition of elements to the list.
  17. Modify SineWave.java to turn SineDraw into a JavaBean by adding “getter” and “setter” methods.
  18. Remember the “sketching box” toy with two knobs, one that controls the vertical movement of the drawing point, and one that controls the horizontal movement? Create one of those, using SineWave.java to get you started. Instead of knobs, use sliders. Add a button that will erase the entire sketch.
  19. Starting with SineWave.java, create a program (an applet/application using the Console class) that draws an animated sine wave that appears to scroll past the viewing window like an oscilloscope, driving the animation with a Thread. The speed of the animation should be controlled with a java.swing.JSlider control.
  20. Modify Exercise 19 so that multiple sine wave panels are created within the application. The number of sine wave panels should be controlled by HTML tags or command-line parameters.
  21. Modify Exercise 19 so that the java.swing.Timer class is used to drive the animation. Note the difference between this and java.util.Timer.
  22. Create an “asymptotic progress indicator” that gets slower and slower as it approaches the finish point. Add random erratic behavior so it will periodically look like it’s starting to speed up.
  23. Modify Progress.java so that it does not share models, but instead uses a listener to connect the slider and progress bar.
  24. Follow the instructions in the section titled “Packaging an applet into a JAR file” to place TicTacToe.java into a JAR file. Create an HTML page with the simple version of the applet tag along with the archive specification to use the JAR file. Run HTMLconverter on file to produce a working HTML file.
  25. Create an applet/application using Console. This should have three sliders, one each for the red, green, and blue values in java.awt.Color. The rest of the form should be a JPanel that displays the color determined by the three sliders. Also include non-editable text fields that show the current RGB values.
  26. In the JDK documentation for javax.swing, look up the JColorChooser. Write a program with a button that brings up the color chooser as a dialog.
  27. Almost every Swing component is derived from Component, which has a setCursor( ) method. Look this up in the JDK documentation. Create an applet and change the cursor to one of the stock cursors in the Cursor class.
  28. Starting with ShowAddListeners.java, create a program with the full functionality of c10:ShowMethods.java.
  29. Turn c12:TestRegularExpression.java into an interactive Swing program that allows you to put an input string in one TextArea and a regular expression in a TextField. The results should be displayed in a second TextArea.
  30. Modify InvokeLaterFrame.java to use invokeAndWait( ).



[74] A variation on this is called “the principle of least astonishment,” which essentially says: “don’t surprise the user.”

[75] Note that IBM created a new open-source GUI library for their Eclipse editor (www.Eclipse.org), which you may want to consider as an alternative to Swing.

[76] Application frameworks are an example of the design pattern called the Template Method.

[77] It is assumed that the reader is familiar with the basics of HTML. It’s not too hard to figure out, and there are lots of books and resources.

[78] In my opinion. And after you learn about Swing, you won’t want to waste your time on the pre-Swing stuff.

[79] As described earlier, “Frame” was already taken by the AWT, so Swing uses JFrame.

[80] This will make sense after you’ve read further in this chapter. First, make the reference JApplet a static member of the class (instead of a local variable of main( )), and then call applet.stop( ) and applet.destroy( ) inside WindowAdapter.windowClosing( ) before you call System.exit( ).

[81] There is no MouseMotionEvent even though it seems like there ought to be. Clicking and motion is combined into MouseEvent, so this second appearance of MouseEvent in the table is not an error.

[82] In Java 1.0/1.1 you could not usefully inherit from the button object. This was only one of numerous fundamental design flaws.

[83] This section and the next were created by Jeremy Meyer.

[84] This concept is more fully explored in Thinking in Enterprise Java, at www.BruceEckel.com.

[85] This section was created by Jeremy Meyer.

Thinking in Java
Prev Contents / Index Next

 
 
   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire