Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Swing and the Abstract Windowing Toolkit
- Using Components
- Workshop: Creating Your Own Component
- Summary
- Q&A
- Quiz
- Activities
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Workshop: Creating Your Own Component
As you may recall, one of the main advantages of object-oriented programming is the capability to reuse classes in different projects. For this hour's workshop, you will create a special panel component that can be reused in different Java programs. The component, ClockPanel, displays the current date and time in a manner similar to the ClockTalk project from Hour 7, "Using Conditional Tests to Make Decisions."
The first step in creating your own user interface component is to decide the existing component from which to inherit. The ClockPanel component is most like a panel, so it will be a subclass of JPanel.
The ClockPanel class is defined in Listing 13.3. This class represents panel components that include a label displaying the current date and time. Enter the text of Listing 13.3 and save the file as ClockPanel.java.
Example 13.3. The Full Text of ClockPanel.java
1: import javax.swing.*;
2: import java.awt.*;
3: import java.util.*;
4:
5: public class ClockPanel extends JPanel {
6: public ClockPanel() {
7: super();
8: String currentTime = getTime();
9: JLabel time = new JLabel("Time: ");
10: JLabel current = new JLabel(currentTime);
11: add(time);
12: add(current);
13: }
14:
15: String getTime() {
16: String time;
17: // get current time and date
18: Calendar now = Calendar.getInstance();
19: int hour = now.get(Calendar.HOUR_OF_DAY);
20: int minute = now.get(Calendar.MINUTE);
21: int month = now.get(Calendar.MONTH) + 1;
22: int day = now.get(Calendar.DAY_OF_MONTH);
23: int year = now.get(Calendar.YEAR);
24:
25: String monthName = "";
26: switch (month) {
27: case (1):
28: monthName = "January";
29: break;
30: case (2):
31: monthName = "February";
32: break;
33: case (3):
34: monthName = "March";
35: break;
36: case (4):
37: monthName = "April";
38: break;
39: case (5):
40: monthName = "May"; 41: break; 42: case (6): 43: monthName = "June"; 44: break; 45: case (7): 46: monthName = "July"; 47: break; 48: case (8): 49: monthName = "August"; 50: break; 51: case (9): 52: monthName = "September"; 53: break; 54: case (10): 55: monthName = "October"; 56: break; 57: case (11): 58: monthName = "November"; 59: break; 60: case (12): 61: monthName = "December"; 62: } 63: time = monthName + " " + day + ", " + year + " " 64: + hour + ":" + minute; 65: return time; 66: } 67: }
The getTime() method in Lines 15–66 contains the same technique for retrieving the current date and time as the ClockTalk application from Hour 7. The panel is created in the constructor method in Lines 6–13. The following things are taking place:
- Line 8: The date and time are retrieved by calling getTime() and storing the value it returns in a String variable called currentTime.
- Line 9: A new label named time is created with the text "Time: ".
- Line 10: currentTime is used as the text of new label component called current.
- Line 11: The time label is added to the clock panel by calling the panel's add() method with the label as an argument.
- Line 12: The current label is added to the panel in the same manner.
The ClockPanel class cannot be run at a command line. To use it, you must create a Java program that includes an object of this class in its graphical user interface. This could be an application that displays a frame, an applet, or any other kind of program that features a GUI.
To try out this panel, create the ClockFrame application, which is contained in Listing 13.4.
Example 13.4. The Full Text of ClockFrame.java
1: import java.awt.*;
2: import javax.swing.*;
3:
4: public class ClockFrame extends JFrame {
5: public ClockFrame() {
6: super("Clock");
7: setSize(225, 125);
8: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9: Container pane = getContentPane();
10: FlowLayout flo = new FlowLayout();
11: pane.setLayout(flo);
12: ClockPanel time = new ClockPanel();
13: pane.add(time);
14: setContentPane(time);
15: setVisible(true);
16: }
17:
18: public static void main(String[] arguments) {
19: ClockFrame sal = new ClockFrame();
20: }
21: }
When you run the application, it should resemble Figure 13.7.
Figure 13.7 Displaying a clock panel component.
Summary | Next Section

Account Sign In
View your cart