www.developer.com/java/ent/article.php/629221
|
By Richard G. Baldwin August 23, 2000
Java Programming, Lecture Notes # 1020
PrefaceRecommended supplementary reading It is recommended that in addition to studying this set of lessons, you also study my earlier lessons on Swing. A list of some of my Swing lessons can be found in an earlier lesson (see Swing from A to Z: Some Simple Components ) in this series. The lessons themselves can be found at Baldwin's Java Programming Tutorials. The earlier lessons will introduce you to the use of Swing while avoiding much of the detail included in this series. A recommended lesson Since this lesson deals with borders, I particularly recommend the lesson entitled "Swing, Hidden Buttons with Icons, Icon Images, Borders, Tool Tips, Nested Buttons, and Other Fun Stuff." That lesson illustrates some very interesting uses of borders with buttons to cause buttons to rise up from the surface when you point at them with the mouse. IntroductionIn an earlier lesson, I provided lists of properties, events, and methods defined in JComponent and its superclasses: Container, Component, and Object. Default appearance and behavior Because most Swing components extend JComponent, the properties, events, and methods defined in those classes provide the default appearance and behavior of most of the Swing components. Understanding common properties, events, and methods The next few lessons concentrate on understanding of these common properties, events, and methods in order to provide an overall knowledge of the appearance and behavior of Swing components. Will discuss specialized appearance and behavior later After I have illustrated this common appearance and behavior, I will embark on a study of the additional specialized appearance and behavior associated with individual components. What's in this lesson? This is the first of several lessons that emphasize an understanding of the border property along with the use of that property to construct components having different border styles. Several parts are needed Because of the large amount of material involved, I have decided to break this discussion into several parts. This is Part 1. It sets the background for future discussions, and also deals specifically with the use of the EtchedBorder class. Sample ProgramThe name of the sample program that I will discuss to illustrate borders is Swing13. A screen shot of the GUI that is produced when the program is run is shown below as Figure 1.
Twelve JLabel objects The program creates and displays twelve different JLabel objects, applying a different border style to each of them. As you can see, a wide variety of possible border styles are available, and this is just a sampling of the possibilities. Apply to all Swing components These different border styles can be applied to all Swing components that extend JComponent, either directly or indirectly. Can also define your own borders If you want to, you can also define your own custom borders as well. Interesting Code FragmentsThe controlling class Listing 1 shows the beginning of the controlling class and the main() method.
The controlling class extends JFrame so that an object of the controlling class is a GUI that can be placed directly on the desktop. Instantiate an object of the controlling class The main() method instantiates an object of the controlling class, causing the GUI to appear on the screen. The makeLabel() method Listing 2 shows the beginning of a convenience method named makeLabel(). This method is designed to instantiate and to return a reference to an opaque JLabel object with a specified border and a pink background.
A convenience method This method is provided to reduce the amount of code required to instantiate the twelve JLabel objects with borders. By using this method, it is possible to avoid the requirement to repeat the same code twelve times. Text content of the label The text content of the label is provided as the first incoming parameter to the method. The required border style The required border style is provided as the second incoming parameter. Note that this parameter is a reference to an object of type Border. The new code The only thing new in the code in Listing 2 is the use of the setBorder() method to set the border property of the label object. This setter method requires a parameter that is a reference to an object of a class that implements the Border interface (a reference to an object of type Border). What does Sun have to say? Here is what Sun has to say about the setBorder() method.
The insets The term insets is a term that is used to describe the width of the border in pixels. The getInsets() method returns the value of the insets property of the object on which the method is invoked. This value is returned as a reference to an object of the Insets class. The Insets class An object of the Insets class encapsulates four public fields of type int. The four fields contain the widths in pixels of the left, right, bottom, and top sections of the border. Because they are public, these field values can be accessed simply by joining the name of the field to the name of a reference to the Insets object. (No accessor method is required.) The remainder of the makeLabel() method Listing 3 shows the remainder of the method named makeLabel().
The getInsets() method is used to get the inset values for the left border and top border. These two values are concatenated with the specified text content so that they will be displayed as the text on the label when the label is rendered. Returns a JLabel object The method returns a reference to a JLabel object, with an opaque pink background, having the specified borders, and having a text property whose value is the concatenation of the specified text value and the inset values for the left and top borders. The constructor Listing 4 shows the beginning of the constructor, which sets the layout manager to FlowLayout.
Will honor preferred size of the labels As mentioned in an earlier lesson, this layout manager will attempt to honor both dimensions of the preferred size while displaying each label. In this case, the value of the preferredSize property is automatically set, taking the borders and the text content of the label into account. The rest of the constructor Following this, the constructor contains twelve fairly complex statements. Each of the twelve statements causes a JLabel object to be displayed with
At this point, it may be helpful for you to open a copy of this lesson in another browser window so you can see the screen shot of the GUI while I discuss the different border styles. EtchedBorder,2,2 Listing 5 shows the code fragment that causes the label in the upper-left corner of the screen shot to be added to the JFrame container.
Uses the makeLabel() method This fragment invokes the makeLabel() method discussed above, passing the two required parameters. The specified text content The first parameter is the string "EtchedBorder" that is to become part of the text content of the label. As you will recall, this string is concatenated with the inset values for the border and set into the text property for the label. This, in turn, causes the concatenated string to be displayed as the text on the face of the label. For this case, the resulting text on the face of the label is: EtchedBorder,2,2 The specified border style The second parameter to the makeLabel() method is a reference to a new object of the class EtchedBorder. This class (as well as the other classes that I will discuss in the following paragraphs) extends the class named AbstractBorder. AbstractBorder The AbstractBorder class implements the Border interface. Therefore, a reference to an object of the EtchedBorder class satisfies the requirement that the second parameter to the makeLabel() method be a reference to an object of type Border. Creating your own borders If you decide to create your own custom borders, probably the best way to do so is to extend the AbstractBorder class. Now back to EtchedBorder Here is part of what Sun has to say about the EtchedBorder class.
The default case The default case for the noarg constructor that I used is to produce a border that appears to be etched or chiseled out of the surface (a ditch). It has a very modest three-dimensional appearance created using highlights and shadows. Other constructors available As you can probably surmise from the Sun text provided above, there are other constructors that allow you to cause the border to appear as a small hill instead of a ditch. It is also possible to specify the colors used for highlights and shadows as well. The size of the label As you can see from the screen shot, the default size of the label object with the etched border is barely large enough to accommodate its text, particularly on the ends. All in all, it is pretty ugly. We will see how to remedy this situation later by using a compound border. The inset values As you can also see from the text displayed in the label in the screen shot, the inset (width) of the left border and the inset of the top border are each two pixels. Although I didn't display the inset for the right border and the bottom border, they appear to be the same for this border style. SummaryWhere To From Here?
Complete Program Listing
Copyright 2000, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited. About the authorRichard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine. Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
|