Java Reference
In-Depth Information
L ISTING 17.4 ComboBoxDemo.java
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4
5 public class
ComboBoxDemo extends JFrame
{
6
// Create an array of Strings for flag titles
7
private
String[] flagTitles
= { "Canada" , "China" , "Denmark" ,
country
8
"France" , "Germany" , "India" , "Norway" , "United Kingdom" ,
9
"United States of America" };
10
11
// Declare an ImageIcon array for the national flags of 9 countries
12
private
ImageIcon[] flagImage
= {
image icon
13
new ImageIcon( "image/ca.gif" ),
14
new ImageIcon( "image/china.gif" ),
15
new ImageIcon( "image/denmark.gif" ),
16
new ImageIcon( "image/fr.gif" ),
17
new ImageIcon( "image/germany.gif" ),
18
new ImageIcon( "image/india.gif" ),
19
new ImageIcon( "image/norway.gif" ),
20
new ImageIcon( "image/uk.gif" ),
21
new ImageIcon( "image/us.gif" )
22 };
23
24
// Declare an array of strings for flag descriptions
25
private
String[] flagDescription
= new String[ 9 ];
description
26
27
// Declare and create a description panel
28
private DescriptionPanel descriptionPanel = new DescriptionPanel();
29
30
// Create a combo box for selecting countries
31
32
33 public static void main(String[] args) {
34 ComboBoxDemo frame = new ComboBoxDemo();
35 frame.pack();
36 frame.setTitle( "ComboBoxDemo" );
37 frame.setLocationRelativeTo( null ); // Center the frame
38 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
39 frame.setVisible( true );
40 }
41
42 public ComboBoxDemo() {
43 // Set text description
44 flagDescription[ 0 ] = "The Canadian national flag..." ;
45 flagDescription[ 1 ] = "Description for China ... " ;
46 flagDescription[ 2 ] = "Description for Denmark ... " ;
47 flagDescription[ 3 ] = "Description for France ... " ;
48 flagDescription[ 4 ] = "Description for Germany ... " ;
49 flagDescription[ 5 ] = "Description for India ... " ;
50 flagDescription[ 6 ] = "Description for Norway ... " ;
51 flagDescription[ 7 ] = "Description for UK ... " ;
52 flagDescription[ 8 ] = "Description for US ... " ;
53
54 // Set the first country (Canada) for display
55 setDisplay( 0 );
56
57 // Add combo box and description panel to the frame
58 add(jcbo, BorderLayout.NORTH);
private JComboBox jcbo = new JComboBox(flagTitles);
combo box
create UI
 
Search WWH ::




Custom Search