Sams Teach Yourself Java 2 in 24 Hours

Sams Teach Yourself Java 2 in 24 Hours

By Rogers Cadenhead

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:

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.

13fig07.gif

Figure 13.7 Displaying a clock panel component.

Share ThisShare This

Informit Network