Username:
Password:
Java 6 Tutorials

User Interface / Tool Bars

Subtitles of the Movie

You can add a toolbar to any window. It's just a row of buttons inside a toolbar manager. Actually, you can include any kind of component you want in a toolbar, but people have to come to expect small buttons with images on them. Toolbars are normally included along with menus. Toolbars come in handy for people who use the application a lot and don't need prompting for the actions they want to take. Here's how you build a toolbar. The JToolbar class is a toolbar manager, so you'll need to import that. The toolbar in this example is made from buttons with pictures on them. So it needs the JButton class and the image icon class. The BorderLayout manager plays an important role in managing toolbars. You construct your window from components as usual and place them in the center position of the border layout and the edge positions are all used by the toolbar. You can do this with any kind of window. This example uses a JFrame and you'll need to implement the action listener interface to listen to the buttons. The BuildFrame method constructs the layout for this example. You can use anything you want in the center. This example uses a text area inside a ScrollPane just to display the results of the toolbar selections. A toolbar is constructed and a number of buttons are added to it. The buttons are all scaled to the same size so the toolbar is uniform. I'll show you that here in just a minute. The toolbar is added to the BorderLayout at one of the edges. Any of the four positions will work. This example uses north; the top of the window. The MakeToolButton method is used to build the buttons. Two parameters are required to define each button. One is the name of the file containing the image and the other is the command string that will be assigned to the button. None of the buttons display text, so it's not necessary to provide command strings so the program can recognize which button has been selected. Each image is scaled to the same size, so the buttons will all be the same size on the toolbar. The action command is assigned to the button and the button is give this object to be its action listener. The action command method retrieves the command string from the button and adds it to the new line in the text area. This text area is then notified to repaint itself. Here's how it works. You can see the toolbar across the top of the window with the four buttons in it. Each time a button is selected, the command string for that button is printed in the window. The toolbar can be dragged from one position in the BorderLayout to another and it still works the same as before. It can be placed in any of the four sides. It can even be parked elsewhere in the screen. The buttons still work normally. In the next lesson, I will show you how to combine a menu and a toolbar in the same application and how to add tool tips for both of them.

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
  • 81,350 Video Tutorials (20,800 free)
  • Video Available as Flash or QuickTime
  • Over 782 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available