1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.event;
21
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.awt.event.MouseAdapter;
25 import java.awt.event.MouseEvent;
26
27 import javax.swing.JComponent;
28 import javax.swing.JList;
29 import javax.swing.JTable;
30 import javax.swing.JTree;
31
32 /***
33 * Convenience class for a double click listener in a table. Use it like this:
34 *
35 * <pre>
36 * JTable jta;
37 * Action action;
38 * jta.addMouseListener(new DoubleClickListener(action, jta));
39 * </pre>
40 */
41 public class DoubleClickListener extends MouseAdapter {
42
43
44
45 private ActionListener ac;
46 private JComponent jc;
47
48
49
50 public DoubleClickListener(JComponent jc, ActionListener ac)
51 {
52 this.ac = ac;
53 this.jc = jc;
54 }
55
56
57
58 public static DoubleClickListener install(JComponent jc, ActionListener ac)
59 {
60 DoubleClickListener listener = new DoubleClickListener(jc, ac);
61 jc.addMouseListener(listener);
62 return listener;
63 }
64
65 public void mousePressed(MouseEvent e)
66 {
67 if (e.getClickCount() == 2) {
68 int row = 0;
69 if (jc instanceof JTable) {
70 row = ((JTable)jc).rowAtPoint(e.getPoint());
71
72 }
73 else if (jc instanceof JList) {
74 row = ((JList)jc).locationToIndex(e.getPoint());
75 }
76 else if (jc instanceof JTree) {
77 row = ((JTree)jc).getRowForLocation(e.getX(), e.getY());
78 }
79
80 if (row != -1) {
81 ac.actionPerformed(new ActionEvent(jc, 0, ""));
82 }
83 }
84 }
85
86 }
87