Chapter 6

Additional Web Questions

1) The AWT is the preferred graphical tool kit for Java2 programming.

a)_____ true

b)_____ false

Answer: B. Though the AWT provides basic GUI building blocks, Swing is the preferred graphical tool kit for Java2 programming.

2) All Swing containers and components are subclasses of JComponent.

a)_____ true

b)_____ false

Answer: A. All Swing containers and components are subclasses of JComponent. This is handy to realize, because all methods applicable to JComponent are applicable to all Swing containers and components as well.

3) An applet can do whatever an application can do.

a)_____ true

b)_____ false

Answer: B. Applets run inside a sandbox, which is a security mechanism to prevent extensive interaction with the system upon which they run. Unless explicitly granted special privileges, applets cannot read or write system files, or perform other tasks that could compromise the security of a system.

4) JLabel objects cannot display images.

a)_____ true

b)_____ false

Answer: B. If you peruse the Java API Documentation, you will find both constructors and methods for displaying text, images or both within a JLabel object. Displaying images within a JLabel is a very useful technique, and is covered in a following chapter.

5) By default, JLabel objects have a transparent background.

a)_____ true

b)_____ false

Answer: A. JLabel objects default to having a transparent background. This is important to recognize so that you don’t tear your hair out for hours wondering why the background color you set for the label does not display properly.

6) JButton objects cannot display images.

a)_____ true

b)_____ false

Answer: B. JButton objects are able to display both text and images. The Java API Documentation details constructors and methods with which images and text can be added to a button.

7) JButton objects always respond to user input, such as mouse-clicks.

a)_____ true

b)_____ false

Answer: B. While all JButton objects respond to mouse-clicks by default, they can be disabled (as can all Swing components) so that mouse-clicks are ignored. The JComponent method setEnabled(boolean enabled) provides this functionality.

8) The JPasswordField object uses the asterisk (*) to represent each input character, and this cannot be changed for security reasons.

a)_____ true

b)_____ false

Answer: B. Though the asterisk is used by default, the setEchoChar(char c) method can be used to set a different character to mask input.

9) The JTextField object is used to solicit multi-line user input.

a)_____ true

b)_____ false

Answer: B. The JTextArea component should be used to solicit multi-line user input.

10) Though useful for soliciting editable text, the JTextArea should not be used for displaying non-editable text because there is no way to prevent a user from highlighting and modifying the contents of the component.

a)_____ true

b)_____ false

Answer: B. No way! Any text-input component can be told to disallow editing of the contents by calling setEditable(boolean editable). You don’t honestly believe that Java would leave you high-and-dry on such an issue, do you?!