-
Notifications
You must be signed in to change notification settings - Fork 3
/
EditListAction.java
106 lines (86 loc) · 2.72 KB
/
EditListAction.java
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/*
* A simple popup editor for a JList that allows you to change
* the value in the selected row.
*
* The default implementation has a few limitations:
*
* a) the JList must be using the DefaultListModel
* b) the data in the model is replaced with a String object
*
* If you which to use a different model or different data then you must
* extend this class and:
*
* a) invoke the setModelClass(...) method to specify the ListModel you need
* b) override the applyValueToModel(...) method to update the model
*/
public class EditListAction extends AbstractAction
{
private JList list;
private JPopupMenu editPopup;
private JTextField editTextField;
private Class<?> modelClass;
public EditListAction()
{
setModelClass(DefaultListModel.class);
}
protected void setModelClass(Class modelClass)
{
this.modelClass = modelClass;
}
protected void applyValueToModel(String value, ListModel model, int row)
{
DefaultListModel dlm = (DefaultListModel)model;
dlm.set(row, value);
}
/*
* Display the popup editor when requested
*/
public void actionPerformed(ActionEvent e)
{
list = (JList)e.getSource();
ListModel model = list.getModel();
if (! modelClass.isAssignableFrom(model.getClass())) return;
// Do a lazy creation of the popup editor
if (editPopup == null)
createEditPopup();
// Position the popup editor over top of the selected row
int row = list.getSelectedIndex();
Rectangle r = list.getCellBounds(row, row);
editPopup.setPreferredSize(new Dimension(r.width, r.height));
editPopup.show(list, r.x, r.y);
// Prepare the text field for editing
editTextField.setText( list.getSelectedValue().toString() );
editTextField.selectAll();
editTextField.requestFocusInWindow();
}
/*
* Create the popup editor
*/
private void createEditPopup()
{
// Use a text field as the editor
editTextField = new JTextField();
Border border = UIManager.getBorder("List.focusCellHighlightBorder");
editTextField.setBorder( border );
// Add an Action to the text field to save the new value to the model
editTextField.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String value = editTextField.getText();
ListModel model = list.getModel();
int row = list.getSelectedIndex();
applyValueToModel(value, model, row);
editPopup.setVisible(false);
}
});
// Add the editor to the popup
editPopup = new JPopupMenu();
editPopup.setBorder( new EmptyBorder(0, 0, 0, 0) );
editPopup.add(editTextField);
}
}