Swing Components / Combo Box
Subtitles of the Movie
One of the simples components to use the combo box. You can simply declare yourself an array of choices and use that in the construction of the combo box. The component then handles the pull-down list and display of the current selection. All you have to do in your program is read the result. This example uses a JComboBox component to let the user select from among the names of the planets in our solar system. You can add the items to be chosen from one at a time or you can put them in an array as in this example. The list of items to be chosen from in the array is included in the constructor. In a ComboBox, one of the items is always shown as being the one selected. You can specify the index of the default selection. If you don't specify, the default is item 0, the first member of the list. This class implements the action listener interface so it can be added to the list of action listeners for the ComboBox. This is the method that's called every time a selection is made from the ComboBox. This method displays the name of the selected item and the index of the selected item. Here's how it works. Here you see the original appearance of the ComboBox with just the selected item showing. If you click the button, you get a list of items. Notice the scroll bar that appears with the list. This what happens when there are more items in the list than can fit into the display. The ComboBox has a maximum row count that limits the number of items it displays and you can change that if you wish. The default setting is shown here. You can select any member of the list. And you can see that both the string and the index of the selection are available inside the program. Let me show you a slight variation of the ComboBox. It is possible to edit the contents of the ComboBox. This example is exactly like the previous one except for one line of code. This, the call to set editable. This call makes it possible for the user to enter a name as well as select one that's already there. Now you can enter a name like this. You can see that the program got the name. It was not a member of the list, so it's index is minus one. Notice that even though the new name was entered, it did not modify the list.
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
United States 