-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathItemDemo.java
More file actions
119 lines (102 loc) · 3.12 KB
/
ItemDemo.java
File metadata and controls
119 lines (102 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class ItemDemo extends JPanel
{
public ItemDemo()
{
setBorder( new EmptyBorder(10, 10, 10, 10) );
setLayout( new GridLayout(0, 2, 10, 5) );
add( new JLabel( "Item Basic Use" ) );
add( new JLabel( "Item As Wrapper" ) );
// Create combo box showing basic use of the Item class
JComboBox<Item<String>> comboBox = new JComboBox<Item<String>>();
add( comboBox );
comboBox.addItem( new Item<String>("CA", "Canada" ) );
comboBox.addItem( new Item<String>("GB", "United Kingdom" ) );
comboBox.addItem( new Item<String>("US", "United States" ) );
comboBox.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JComboBox comboBox = (JComboBox)e.getSource();
Item item = (Item)comboBox.getSelectedItem();
String code = (String)item.getValue();
System.out.println( code );
}
});
// Create combo box using Item class as a wrapper
JComboBox<Item<Car>> comboBox2 = new JComboBox<Item<Car>>();
add( comboBox2 );
Car c1 = new Car("Ford", "Crown Victoria");
Car c2 = new Car("Ford", "Focus");
Car c3 = new Car("Ford", "Fusion");
Car c4 = new Car("Honda", "Civic");
Car c5 = new Car("Honda", "Fit");
Car c6 = new Car("Nissan", "Altima");
Car c7 = new Car("Toyota", "Camry");
Car c8 = new Car("Toyota", "Corolla");
comboBox2.setModel( new SortedComboBoxModel<Item<Car>>() );
comboBox2.addItem( new Item<Car>(c1, c1.getModel()) );
comboBox2.addItem( new Item<Car>(c2, c2.getModel()) );
comboBox2.addItem( new Item<Car>(c3, c3.getModel()) );
comboBox2.addItem( new Item<Car>(c4, c4.getModel()) );
comboBox2.addItem( new Item<Car>(c5, c5.getModel()) );
comboBox2.addItem( new Item<Car>(c6, c6.getModel()) );
comboBox2.addItem( new Item<Car>(c7, c7.getModel()) );
comboBox2.addItem( new Item<Car>(c8, c8.getModel()) );
comboBox2.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JComboBox comboBox = (JComboBox)e.getSource();
Item item = (Item)comboBox.getSelectedItem();
Car car = (Car)item.getValue();
System.out.println( car );
}
});
}
private static void createAndShowUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("ItemDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new ItemDemo() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
static class Car
{
private String make;
private String model;
public Car(String make, String model)
{
this.make = make;
this.model = model;
}
public String getMake()
{
return make;
}
public String getModel()
{
return model;
}
public String toString()
{
return make + " : " + model;
}
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}