Digital Edition

SYS-CON.TV
Macro Component Use
Macro Component Use

When designing a Java applet or application, you often may wish to place a similar set of components in several different places. Perhaps you would like to have a labeled text entry field, or a more complicated structure like a list of IP addresses and subnet masks, with entry fields for each. You could do these by creating a Panel, placing them there, having a method in your class to create this, and returning the Panel. There is an easier way.

A 'macro' component [treating multiple components as a single object] is a simple step between writing a custom component which handles all the events and drawing its needs, and just creating a new panel in your existing class. From an object-oriented standpoint, it is a cleaner, simpler way to write code. From a Java Abstract Window Toolkit (AWT) standpoint, it is simpler than trying to deal with all the Events and data within a single class.

As an example, consider a labeled choice. The AWT does not contain this construct, but it's likely something you use a lot in your applets. Perhaps you have created a method 'createLabelledChoiceComponent', and it returns a panel which you add to yourself. You could just as easily create a 'labelledChoice' class. For this trivial example, you would need to provide a constructor, taking the label string as a parameter. Additionally, you could provide methods to get at the Choice component methods: addItem(), getItem(), etc. Another way you could do this would be to provide a getChoice() method, simply returning the Choice to the caller.

You may be wondering why this example extends Panel, instead of Choice. If it extended Choice, you would automatically have access to all the Choice methods. The problem is, I need this to be a layout manager in order to hold the two components, the Label and the Choice. v In the example above, there is no benefit to having placed this code in a separate class. It would have been just as simple to create a Panel and parent the Label and the Choice directly on it. The benefit becomes clear, however, when there is a need to do event handling.

Say you wished to create a list of IP addresses. You would like to prevent the user from entering something that wasn't an IP address in it. Additionally, you would like to group this list, somehow, so that it stood out as a unit.

In the next example, I have created a new class which presents the user with a labeled box for entering IP addresses. Drawing a box around it was easy since it is a component of its own and has bounds(). In addition, I don't allow any keys other than editing keys and '0-9' plus '.' in the IP address field. The event handler discards any other key. The outcome of this is shown in Figure 1.

If you examine the source code in Listing 2, you will see how easy it was to accomplish this entire component. The border around it was obtained by overriding the paint function to always draw a box of our bounding size. The key-filtering in the IP field was accomplished by passing only those keys which we wanted. Recall that if an event handler returns 'true', then it has handled the event, and the next event handler won't see it. Thus, I return 'true' if it is a key I don't want.

The benefits of making this list into a component are much clearer than the first example:

  • It was trivial to draw a border around it.
  • Event handling was simple -- It didn't conflict with the rest of the applet.
  • We can now create and add multiple lists of this type trivially.
  • There is a single piece of code to change if we want additional functionality.
  • There is a possibility we can share this component with other applets.

    This simple (and obvious) technique can make your coding simpler, provide you with a library of reusable components for your other applets, and make documentation simpler.

    About Don Bowman
    Don Bowman is a software designer with Hewlett-Packard. He is the co-webmaster for his division. Don has worked with Java on HP-UX and Windows 95 environments.

  • In order to post a comment you need to be registered and logged in.

    Register | Sign-in

    Reader Feedback: Page 1 of 1



    ADS BY GOOGLE
    Subscribe to the World's Most Powerful Newsletters

    ADS BY GOOGLE

    Today, we have more data to manage than ever. We also have better algorithms that help us access our...
    Andi Mann, Chief Technology Advocate at Splunk, is an accomplished digital business executive with e...
    A valuable conference experience generates new contacts, sales leads, potential strategic partners a...
    Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: D...
    DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held Novemb...
    DXWorldEXPO LLC announced today that ICOHOLDER named "Media Sponsor" of Miami Blockchain Event by Fi...
    @DevOpsSummit at Cloud Expo, taking place November 12-13 in New York City, NY, is co-located with 22...
    SYS-CON Events announced today that IoT Global Network has been named “Media Sponsor” of SYS-CON's @...
    "We do one of the best file systems in the world. We learned how to deal with Big Data many years ag...
    CloudEXPO New York 2018, colocated with DXWorldEXPO New York 2018 will be held November 11-13, 2018,...
    The best way to leverage your Cloud Expo presence as a sponsor and exhibitor is to plan your news an...
    Adding public cloud resources to an existing application can be a daunting process. The tools that y...
    Using new techniques of information modeling, indexing, and processing, new cloud-based systems can ...
    Disruption, Innovation, Artificial Intelligence and Machine Learning, Leadership and Management hear...
    DXWorldEXPO | CloudEXPO are the world's most influential, independent events where Cloud Computing w...
    Enterprises are striving to become digital businesses for differentiated innovation and customer-cen...
    DXWorldEXPO LLC announced today that Telecom Reseller has been named "Media Sponsor" of CloudEXPO | ...
    Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As au...
    To Really Work for Enterprises, MultiCloud Adoption Requires Far Better and Inclusive Cloud Monitori...
    Consumer-driven contracts are an essential part of a mature microservice testing portfolio enabling ...