Swing Components / Icons
Subtitles of the Movie
You can use an icon to decorate many of the displayed components. Most often you will see this done on menu buttons, but you can do it with any button. It's quite easy. Let me show you. This example uses an image instead of text as the displayed portion of a button. The image icon class makes this quite easy to do. There are different kinds of icons. There is even one kind where you paint the image on it yourself, but this lets you load the image from a jpg, GIF or PNG file. You create the image icon by naming the image file you want to use. If you prefer, you could specify the URL or an image file on the Internet or if you have an image data already loaded into memory, you can use that. The image icon class contains a media tracker and it uses that to monitor the loading of the image. You use the image icon in the construction of the button the same way you would normally use text. This example also uses another button, one with simple text. Here's what it looks like. As you can see, the image used in this example is a picture of an old license plate. The button responds to the click of the mouse by darkening the background color. The icon image itself doesn't change. You can also see that the size of the button is determined by the size of the image. If the image had been half the size of the screen, the button would have been made big enough to hold it. You can either scale all your icon images before you use them, or you can scale them when you load them. Just changing the size of the button won't work. All that does is flip the image and display what's left. To scale the image, you will first need to extract it from the image icon object. I have devised a method for the purpose of loading scaled icon images. This way I can have a number of buttons show up with icons all the same size. The method creates a new image icon object, just the same as before. The image object is then extracted from it and scaled and the new image object is stored in the image icon. By the way, if you set either width or height to minus one, the scaling is done in such a way that the aspect ration remains the same. That's usually not important with icons, but it can come in handy. The button is then constructed in the normal way using the scaled image from the method. This is how it looks. There you can see that the display looks exactly like it did before, except the image in the button has been scaled to a more button-like size. With very little effort, you can combine text with the icon in the button. It is simply a matter of including text with the icon on the constructor of the button. Now it looks like this. As you can see, the button resizes itself or both the text and the icon. This is a common technique when defining menu buttons. Next we'll be looking at menu bars and menu buttons and some other components you can include on a menu.
Tutorial Information
| Course: | Java 6 |
| Author: | Arthur Griffith |
| SKU: | 33858 |
| ISBN: | 1-934743-59-3 |
| Release Date: | 2008-02-29 |
| Duration: | 7 hrs / 92 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 