Bonjour,
J'ai un JDialog avec des boutons, des labels et des textfields. J'utilise un keybinding pour pouvoir exécuter une action lorsque l'on appuie sur des touches en particulier.
Le problème est lorsque l'on sélectionne du texte dans un textfield, il prend le focus et les touches ne sont plus "écoutées.
Comment remédier à cela.
En gros le code que j'utilise est similaire à ça mais avec en plus des JPanel, des boutons, ... :
J'ai aussi essayé
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRootPane; import javax.swing.JTextField; import javax.swing.KeyStroke; public class MainWindow extends JFrame{ private MainWindow windowInstance; public MainWindow(){ super("MainWindow"); windowInstance = this; setDefaultCloseOperation(EXIT_ON_CLOSE); JButton bouton = new JButton("JDialog"); bouton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { new myDialog(); } }); JPanel panel = new JPanel(); panel.add(bouton); add(panel); setSize(300, 150); setLocationRelativeTo(null); setVisible(true); setFocusable(true); } public class myDialog extends JDialog{ private int cptr; private JTextField textField; public myDialog(){ super(windowInstance, "myDialog", true); cptr = 0; JLabel label = new JLabel("Label"); textField = new JTextField("numero = 0"); textField.setPreferredSize(new Dimension(100, 16)); textField.setEditable(false); textField.addFocusListener(new FocusListener() { @Override public void focusLost(FocusEvent e) { System.out.println("focusLost"); rootPane.requestFocus(true); } @Override public void focusGained(FocusEvent e) { System.out.println("focusGained"); } }); JTextField texField2 = new JTextField("JTextField"); texField2.setEditable(false); texField2.addFocusListener(new FocusListener() { @Override public void focusLost(FocusEvent e) { System.out.println("focusLost"); rootPane.requestFocus(true); } @Override public void focusGained(FocusEvent e) { System.out.println("focusGained"); } }); JPanel panel = new JPanel(); panel.add(label); panel.add(textField); panel.add(texField2); add(panel); setSize(200, 100); setLocationRelativeTo(windowInstance); setVisible(true); } @Override protected JRootPane createRootPane(){ JRootPane rootPane = new JRootPane(); Action actionLeft = new AbstractAction() { public void actionPerformed(ActionEvent e) { cptr--; textField.setText("numero = "+String.valueOf(cptr)); } }; rootPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("LEFT"), "doLeft"); rootPane.getActionMap().put("doLeft", actionLeft); Action actionRight = new AbstractAction() { public void actionPerformed(ActionEvent e) { cptr++; textField.setText("numero = "+String.valueOf(cptr)); } }; rootPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("RIGHT"), "doRight"); rootPane.getActionMap().put("doRight", actionRight); rootPane.setFocusable(true); return rootPane; } } public static void main(String[] args) { new MainWindow(); } }sans succès.
Code : Sélectionner tout - Visualiser dans une fenêtre à part WHEN_IN_FOCUSED_WINDOW
Cordialement.
Partager