-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathListAction.java
More file actions
87 lines (68 loc) · 1.88 KB
/
ListAction.java
File metadata and controls
87 lines (68 loc) · 1.88 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
import java.awt.event.*;
import javax.swing.*;
/*
* Add an Action to a JList that can be invoked either by using
* the keyboard or a mouse.
*
* By default the Enter key will will be used to invoke the Action
* from the keyboard although you can specify and KeyStroke you wish.
*
* A double click with the mouse will invoke the same Action.
*
* The Action can be reset at any time.
*/
public class ListAction implements MouseListener
{
private static final KeyStroke ENTER = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
private JList list;
private KeyStroke keyStroke;
/*
* Add an Action to the JList bound by the default KeyStroke
*/
public ListAction(JList list, Action action)
{
this(list, action, ENTER);
}
/*
* Add an Action to the JList bound by the specified KeyStroke
*/
public ListAction(JList list, Action action, KeyStroke keyStroke)
{
this.list = list;
this.keyStroke = keyStroke;
// Add the KeyStroke to the InputMap
InputMap im = list.getInputMap();
im.put(keyStroke, keyStroke);
// Add the Action to the ActionMap
setAction( action );
// Handle mouse double click
list.addMouseListener( this );
}
/*
* Add the Action to the ActionMap
*/
public void setAction(Action action)
{
list.getActionMap().put(keyStroke, action);
}
// Implement MouseListener interface
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
Action action = list.getActionMap().get(keyStroke);
if (action != null)
{
ActionEvent event = new ActionEvent(
list,
ActionEvent.ACTION_PERFORMED,
"");
action.actionPerformed(event);
}
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}