User Interface / Tool Bars & Menus
Subtitles of the Movie
A program that has a toolbar normally has a menu that contains the same set of options. Some people like to use toolbars and others like to use menus, so it's a good idea to provide both. It's easy to do. Let me show you how. This is the same program that was used in the lesson earlier explaining how to build toolbars, but it has had a menu added to it. The options on the menu are the same as the ones on the toolbar. To combine a menu and a toolbar, you need to import everything for both as in the previous examples. You will need to implement the action listener interface. The same action listener will receive events from the toolbar and from the menu. The menu bar and the tool bar are built separately, but in the same ways that they were built individually in the previous examples. The only thing new here is the fact that they are both included in the same window. The strings assigned to the toolbar buttons are important. They should be the same as the ones on the menu. At least it makes programming a lot easier if they are the same. The menu that's built for this example has one menu item button for each toolbar button and the menu buttons are given similar names, but they're not exactly the same names as the buttons for the toolbar. They call to set action command for the menu item does the same thing it does for the toolbar. This is the string that will be reported to the action performed method. Here at the bottom of the program is the method that's called each time the menu item or a toolbar button is selected. This example copies the action command to the text area in the window. If you wanted to, you could call a method named get source in the action event and determine which button was pressed; toolbar or menu. But it usually doesn't matter. All you need is the command string. You don't really care which button the user selected with a mouse. All you really want to know is the name of the command. You can see where you could have other buttons in the window issue the same exact commands. It's not unusual to have multiple ways for a user to do the same things. Here you have it; different buttons, same results. In the next lesson, I'm going to show you how to add tool tips to let your interface explain itself.
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 