Workshop: Creating Your Own Component

As you may recall, one of the main advantages of object-oriented coding 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.

Listing 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 app 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 app that displays a frame, an applet, or any other kind of program that features a GUI. To try out this panel, create the ClockFrame app, which is contained in Listing 13.4.

Listing 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: FlowLayout flo = new FlowLayout();
10: setLayout(flo);
11: ClockPanel time = new ClockPanel();
12: add(time);
13: setVisible(true);
14: }
15:
16: public static void main(String[] arguments) {
17: ClockFrame clock = new ClockFrame();
18: }
19: }


When you run the app, it should resemble Screenshot.

Screenshot Displaying a clock panel component.

Java ScreenShot


      
Comments