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 ...

Get SAMS Teach Yourself Programming with Java™ in 24 Hours, FOURTH EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.