Username:
Password:
Java 6 Tutorials

User Interface / Components in a Menu

Subtitles of the Movie

After you work with menus for a bit, you may find that there are some other things you'd like to do with them. Well, you can. The Swing menu system has a few fancy things you can add to your menus. For example, you have checkboxes and radio buttons. Let me show you how radio buttons work. You will need to import a new kind of menu item; a radio button menu item. And you may want to declare your radio buttons so you can get to them later. After all, one of the purposes of a radio button is to click and stay clicked and you may want to be able to read it. You build your menu in the normal way. Here a pull-down menu named choose is added to the menu bar. In a group of radio buttons, when you select one, the others become unselected. It is the button group object that keeps that straight for you. The radio buttons are created and added to the menu in their normal way. Each radio button is also added to the button group and each radio button adds this object as the action listener. In the action performed method, the radio buttons are responded to the same as regular buttons would be. Here's how it works. As you can see from the action and reaction, the menu vanishes each time you make a selection. But when you reshow the menu, it shows that a selection was actually made. One of the things that's done a lot is the grouping of menu items. That's done by entering separators. You do that by inserting one line of code. You can add a separator into the menu anywhere you want. Here's what it looks like. You can see how the separator sort of groups the menu items. Adding a submenu onto a menu is done the same way a pull-down menu is added to the menu bar. You build a menu object, in this example named color, and set lightweight to false, the same as you would with any pull-down menu. Then you add it to an existing pull-down the same as you would add any menu item. Then you can button menu items or any menu items you wish. You could even continue with more submenus. Here's how it looks. When you pull down the parent menu, the submenu is marked so you can tell it's something besides a regular menu button. The submenu comes up in the normal way. You may want to be able to temporarily disable a menu button. Here's how you do that. You build your menu in the normal fashion. N special items are need on import. However, you will need to save a reference to the menu item you wish to disable. This is so you can get to it when you want to change its status. Everything can be done in the normal way. By default, all menu items are enabled. To disable a menu item, you call set enabled and then set it to false. To enable the menu item again, you call set enabled and set it to true. And here's what it looks like. You can see that the first menu item here is enabled, but select disabled and you can disable it and it will appear grayed out and it simply doesn't work. Enabling it again causes it to appear normally and to work. Complex menu arrangements have a dramatic effect on the look and feel of your application. In the next lesson I'm going to show you more information on the variety of look and feel in Java applications.

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