Java Reference
In-Depth Information
Example 10•21: WebBrowser.java (continued)
String animationMessage; // The "loading..." message to display
int animationFrame = 0; // What "frame" of the animation are we on
String[] animationFrames = new String[] { // The content of each "frame"
"-", "\\", "|", "/", "-", "\\", "|", "/",
",", ".", "o", "0", "O", "#", "*", "+"
};
/** This object calls the animate() method 8 times a second */
javax.swing.Timer animator =
new javax.swing.Timer(125, new ActionListener() {
public void actionPerformed(ActionEvent e) { animate(); }
});
/** Display the next frame. Called by the animator timer */
void animate() {
String frame = animationFrames[animationFrame++]; // Get next frame
messageLine.setText(animationMessage + " " + frame); // Update msgline
animationFrame = animationFrame % animationFrames.length;
}
/** Start the animation. Called by the visit() method. */
void startAnimation(String msg) {
animationMessage = msg;
// Save the message to display
animationFrame = 0;
// Start with frame 0 of the animation
animator.start();
// Tell the timer to start firing.
}
/** Stop the animation. Called by propertyChanged() method. */
void stopAnimation() {
animator.stop();
// Tell the timer to stop firing events
messageLine.setText(" ");
// Clear the message line
}
}
Describing GUIs with Properties
At its core, the task of specifying a graphical user interface is a descriptive one.
This descriptive task does not map well onto a procedural and algorithm-based
programming language such as Java. You end up writing lots of code that creates
components, sets properties, and adds components to containers. Instead of sim-
ply describing the structure of the GUI you want, you must write the step-by-step
code to build the GUI.
One way to avoid writing this tedious GUI construction code is to create a GUI-
description language of some sort, then write code that can read that language and
automatically create the described GUI. One common approach is to describe a
GUI using an XML grammar. In this chapter, we'll rely on the simpler syntax of
Java properties files as used by the ResourceBundle class. (See Chapter 7, Inter na-
tionalization for examples using java.util.ResourceBundle .)
A java.util.Properties object is a hashtable that maps string keys to string val-
ues. The Properties class can read and write a simple text file format in which
each name : value line defines a single property. Furthermore, a Properties object
can have a parent Properties object. When you look up the value of a property
Search WWH ::




Custom Search