Creating Custom Components
In this chapter and the previous chapter, we’ve worked with many different user interface objects and made a lot of new classes that are sort of like components. Our new classes do one particular thing well; a number of them can be added to applets or other containers just like the standard Swing components; and several of them are lightweight components that use system resources efficiently because they don’t rely on a peer. But we haven’t created new components; we’ve just used Swing’s impressive repertoire of components as building blocks. In this section, we’ll create an entirely new component, a dial.
Up until now, our new classes still haven’t really been
components. If you think about it, all our classes have been fairly
self-contained; they know everything about what to do and don’t
rely on other parts of the program to do much processing. Therefore,
they are overly specialized. Our menu example created a
DinnerFrame class that had a menu of dinner
options, but it included all the processing needed to handle the
user’s selections. If we wanted to process the selections
differently, we’d have to modify the class. A true component
separates the detection of user choices from the processing of those
choices. It lets the user take some action and then calls another
part of the program to process the action.
Generating Events
So we need a way for our new classes to communicate with other parts of the program. Since we want our new classes to be components, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access