1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.ui;
12
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
15 import javax.swing.JComboBox;
16
17 /***
18 *
19 * A history combo box that remembers previous input and allows you to pick
20 * from a list
21 *
22 * @author M Hill
23 */
24
25
26 public class JHistoryComboBox extends JComboBox
27 {
28 public JHistoryComboBox()
29 {
30 super();
31 setEditable(true);
32
33 addActionListener(
34 new ActionListener()
35 {
36 public void actionPerformed(ActionEvent e)
37 {
38 updateList(getText());
39 }
40 }
41 );
42 }
43
44 public void setDefaultList(String[] list)
45 {
46 for (int i=0; i<list.length; i++)
47 {
48 addItem(list[i]);
49 }
50 }
51
52 /*** Checks to see if the input is already in the list, and if not adds it
53 */
54 public void updateList(Object item)
55 {
56 for (int i=0;i<getItemCount();i++) {
57 if (item.equals(getItemAt(i))) {
58 return;
59 }
60 }
61
62 addItem(item);
63 }
64
65 /***
66 * Sets entry
67 */
68 public void setItem(Object newEntry)
69 {
70 updateList(newEntry);
71 getEditor().setItem(newEntry);
72 }
73
74 public String getText()
75 {
76 return getEditor().getItem().toString();
77 }
78
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96