質問編集履歴

3

コードの不要な部分を削除しました。

2017/01/08 23:58

投稿

fkd4650
fkd4650

スコア12

test CHANGED
File without changes
test CHANGED
@@ -16,61 +16,15 @@
16
16
 
17
17
  テキストを入力する部分のキー入力イベントの補足はなんとかできました。
18
18
 
19
- 以下にコードをそのまま掲載します。
19
+ 以下にコードを抜粋して掲載します。
20
-
21
- netBeansで作成中です。自動生成コードも全て掲載しております。
22
20
 
23
21
 
24
22
 
25
23
  JComboBoxの変数名:cb1
26
24
 
27
- JTextAreaの変数名:TA1
25
+ JTextAreaの変数名:ta1
28
26
 
29
27
  ```java
30
-
31
- /*
32
-
33
- * To change this license header, choose License Headers in Project Properties.
34
-
35
- * To change this template file, choose Tools | Templates
36
-
37
- * and open the template in the editor.
38
-
39
- */
40
-
41
- package menutest;
42
-
43
-
44
-
45
- import java.awt.EventQueue;
46
-
47
- import java.awt.event.KeyAdapter;
48
-
49
- import java.awt.event.KeyEvent;
50
-
51
- import java.awt.event.KeyListener;
52
-
53
- import javax.swing.ComboBoxModel;
54
-
55
- import javax.swing.JTextField;
56
-
57
-
58
-
59
-
60
-
61
- public class shitsumon extends javax.swing.JFrame {
62
-
63
-
64
-
65
- /**
66
-
67
- * Creates new form shitumon
68
-
69
- */
70
-
71
- public shitsumon() {
72
-
73
- initComponents();
74
28
 
75
29
 
76
30
 
@@ -92,11 +46,11 @@
92
46
 
93
47
  if(text.isEmpty()){ //文字入力が無い場合
94
48
 
95
- TA1.append("文字を入力してください\n");
49
+ ta1.append("文字を入力してください\n");
96
50
 
97
51
  } else {
98
52
 
99
- TA1.append("【" + text + "】が入力されました。\n" );
53
+ ta1.append("【" + text + "】が入力されました。\n" );
100
54
 
101
55
  }
102
56
 
@@ -114,188 +68,6 @@
114
68
 
115
69
 
116
70
 
117
- }
118
-
119
-
120
-
121
- /**
122
-
123
- * This method is called from within the constructor to initialize the form.
124
-
125
- * WARNING: Do NOT modify this code. The content of this method is always
126
-
127
- * regenerated by the Form Editor.
128
-
129
- */
130
-
131
- @SuppressWarnings("unchecked")
132
-
133
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
134
-
135
- private void initComponents() {
136
-
137
-
138
-
139
- cb1 = new javax.swing.JComboBox<>();
140
-
141
- jScrollPane1 = new javax.swing.JScrollPane();
142
-
143
- TA1 = new javax.swing.JTextArea();
144
-
145
-
146
-
147
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
148
-
149
-
150
-
151
- cb1.setEditable(true);
152
-
153
- cb1.setFont(new java.awt.Font("MS UI Gothic", 0, 24)); // NOI18N
154
-
155
- cb1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "アイテム1", "アイテム2", "iitem3", "item4" }));
156
-
157
- cb1.setSelectedIndex(-1);
158
-
159
-
160
-
161
- TA1.setColumns(20);
162
-
163
- TA1.setRows(5);
164
-
165
- jScrollPane1.setViewportView(TA1);
166
-
167
-
168
-
169
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
170
-
171
- getContentPane().setLayout(layout);
172
-
173
- layout.setHorizontalGroup(
174
-
175
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
176
-
177
- .addGroup(layout.createSequentialGroup()
178
-
179
- .addContainerGap()
180
-
181
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
182
-
183
- .addComponent(jScrollPane1)
184
-
185
- .addComponent(cb1, 0, 419, Short.MAX_VALUE))
186
-
187
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
188
-
189
- );
190
-
191
- layout.setVerticalGroup(
192
-
193
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
194
-
195
- .addGroup(layout.createSequentialGroup()
196
-
197
- .addContainerGap()
198
-
199
- .addComponent(cb1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
200
-
201
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
202
-
203
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
204
-
205
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
206
-
207
- );
208
-
209
-
210
-
211
- pack();
212
-
213
- }// </editor-fold>
214
-
215
-
216
-
217
- /**
218
-
219
- * @param args the command line arguments
220
-
221
- */
222
-
223
- public static void main(String args[]) {
224
-
225
- /* Set the Nimbus look and feel */
226
-
227
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
228
-
229
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
230
-
231
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
232
-
233
- */
234
-
235
- try {
236
-
237
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
238
-
239
- if ("Nimbus".equals(info.getName())) {
240
-
241
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
242
-
243
- break;
244
-
245
- }
246
-
247
- }
248
-
249
- } catch (ClassNotFoundException ex) {
250
-
251
- java.util.logging.Logger.getLogger(shitumon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
252
-
253
- } catch (InstantiationException ex) {
254
-
255
- java.util.logging.Logger.getLogger(shitumon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
256
-
257
- } catch (IllegalAccessException ex) {
258
-
259
- java.util.logging.Logger.getLogger(shitumon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
260
-
261
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
262
-
263
- java.util.logging.Logger.getLogger(shitumon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
264
-
265
- }
266
-
267
- //</editor-fold>
268
-
269
-
270
-
271
- /* Create and display the form */
272
-
273
- java.awt.EventQueue.invokeLater(new Runnable() {
274
-
275
- public void run() {
276
-
277
- new shitumon().setVisible(true);
278
-
279
- }
280
-
281
- });
282
-
283
- }
284
-
285
-
286
-
287
- // Variables declaration - do not modify
288
-
289
- private javax.swing.JTextArea TA1;
290
-
291
- private javax.swing.JComboBox<String> cb1;
292
-
293
- private javax.swing.JScrollPane jScrollPane1;
294
-
295
- // End of variables declaration
296
-
297
- }
298
-
299
71
  ```
300
72
 
301
73
 

2

不足していた情報を加筆しました。

2017/01/08 23:58

投稿

fkd4650
fkd4650

スコア12

test CHANGED
File without changes
test CHANGED
@@ -22,6 +22,10 @@
22
22
 
23
23
 
24
24
 
25
+ JComboBoxの変数名:cb1
26
+
27
+ JTextAreaの変数名:TA1
28
+
25
29
  ```java
26
30
 
27
31
  /*

1

抽象的で分かりにくかった内容を具体的にしました。

2017/01/08 12:28

投稿

fkd4650
fkd4650

スコア12

test CHANGED
File without changes
test CHANGED
@@ -4,22 +4,298 @@
4
4
 
5
5
 
6
6
 
7
- editableなjComboBoxのリスト内で方向キーやエンターなどのキー操作があっ際に、どのキーが押されたのか取得する方法があればご教授願ます。
8
-
9
-
10
-
11
- ↓こちらのサイ部で紹介される方法参考にてMenuKeyListenerなどを試してみましが無反応した
12
-
13
- http://www.ne.jp/asahi/hishidama/home/tech/java/swing/JComboBox.html
14
-
15
-
16
-
17
- また他にもKeyAdapterなど複数のイベントリスナーを試してみましたが、まったく反応がありません
18
-
19
-
20
-
21
- 質問内容抽象的になってしまい恐れ入りますが、どなたかご助力頂けないでしょうか
22
-
23
-
7
+ 【実現したいこと】
8
+
9
+ 編集可能なJComboBoxnにおいて、テキスト入力部分のイベントとリスト部分のイベントを、別々のリスナーで補足したいです。
10
+
11
+ 特にリスト部は、項目を選択する際の「エンターキーが押された」とうイベント補足した
12
+
13
+
14
+
15
+ 【現状】
16
+
17
+ テキストを入力する部分キー入力イベントの補足はなんとかできました。
18
+
19
+ 以下に全コードをそのまま掲載します。
20
+
21
+ netBeansで作成中です。自動生成コード掲載ております。
22
+
23
+
24
+
25
+ ```java
26
+
27
+ /*
28
+
29
+ * To change this license header, choose License Headers in Project Properties.
30
+
31
+ * To change this template file, choose Tools | Templates
32
+
33
+ * and open the template in the editor.
34
+
35
+ */
36
+
37
+ package menutest;
38
+
39
+
40
+
41
+ import java.awt.EventQueue;
42
+
43
+ import java.awt.event.KeyAdapter;
44
+
45
+ import java.awt.event.KeyEvent;
46
+
47
+ import java.awt.event.KeyListener;
48
+
49
+ import javax.swing.ComboBoxModel;
50
+
51
+ import javax.swing.JTextField;
52
+
53
+
54
+
55
+
56
+
57
+ public class shitsumon extends javax.swing.JFrame {
58
+
59
+
60
+
61
+ /**
62
+
63
+ * Creates new form shitumon
64
+
65
+ */
66
+
67
+ public shitsumon() {
68
+
69
+ initComponents();
70
+
71
+
72
+
73
+ JTextField field = (JTextField) cb1.getEditor().getEditorComponent();
74
+
75
+
76
+
77
+ class KeyTest extends KeyAdapter {
78
+
79
+ public void keyTyped(KeyEvent e){
80
+
81
+ EventQueue.invokeLater(new Runnable() {
82
+
83
+ @Override public void run() {
84
+
85
+ JTextField tfd = (JTextField) cb1.getEditor().getEditorComponent();
86
+
87
+ String text = tfd.getText();
88
+
89
+ if(text.isEmpty()){ //文字入力が無い場合
90
+
91
+ TA1.append("文字を入力してください\n");
92
+
93
+ } else {
94
+
95
+ TA1.append("【" + text + "】が入力されました。\n" );
96
+
97
+ }
98
+
99
+ }
100
+
101
+ });
102
+
103
+ }
104
+
105
+ }
106
+
107
+ KeyListener kl = new KeyTest();
108
+
109
+ field.addKeyListener(kl);
110
+
111
+
112
+
113
+ }
114
+
115
+
116
+
117
+ /**
118
+
119
+ * This method is called from within the constructor to initialize the form.
120
+
121
+ * WARNING: Do NOT modify this code. The content of this method is always
122
+
123
+ * regenerated by the Form Editor.
124
+
125
+ */
126
+
127
+ @SuppressWarnings("unchecked")
128
+
129
+ // <editor-fold defaultstate="collapsed" desc="Generated Code">
130
+
131
+ private void initComponents() {
132
+
133
+
134
+
135
+ cb1 = new javax.swing.JComboBox<>();
136
+
137
+ jScrollPane1 = new javax.swing.JScrollPane();
138
+
139
+ TA1 = new javax.swing.JTextArea();
140
+
141
+
142
+
143
+ setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
144
+
145
+
146
+
147
+ cb1.setEditable(true);
148
+
149
+ cb1.setFont(new java.awt.Font("MS UI Gothic", 0, 24)); // NOI18N
150
+
151
+ cb1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "アイテム1", "アイテム2", "iitem3", "item4" }));
152
+
153
+ cb1.setSelectedIndex(-1);
154
+
155
+
156
+
157
+ TA1.setColumns(20);
158
+
159
+ TA1.setRows(5);
160
+
161
+ jScrollPane1.setViewportView(TA1);
162
+
163
+
164
+
165
+ javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
166
+
167
+ getContentPane().setLayout(layout);
168
+
169
+ layout.setHorizontalGroup(
170
+
171
+ layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
172
+
173
+ .addGroup(layout.createSequentialGroup()
174
+
175
+ .addContainerGap()
176
+
177
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
178
+
179
+ .addComponent(jScrollPane1)
180
+
181
+ .addComponent(cb1, 0, 419, Short.MAX_VALUE))
182
+
183
+ .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
184
+
185
+ );
186
+
187
+ layout.setVerticalGroup(
188
+
189
+ layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
190
+
191
+ .addGroup(layout.createSequentialGroup()
192
+
193
+ .addContainerGap()
194
+
195
+ .addComponent(cb1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
196
+
197
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
198
+
199
+ .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
200
+
201
+ .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
202
+
203
+ );
204
+
205
+
206
+
207
+ pack();
208
+
209
+ }// </editor-fold>
210
+
211
+
212
+
213
+ /**
214
+
215
+ * @param args the command line arguments
216
+
217
+ */
218
+
219
+ public static void main(String args[]) {
220
+
221
+ /* Set the Nimbus look and feel */
222
+
223
+ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
224
+
225
+ /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
226
+
227
+ * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
228
+
229
+ */
230
+
231
+ try {
232
+
233
+ for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
234
+
235
+ if ("Nimbus".equals(info.getName())) {
236
+
237
+ javax.swing.UIManager.setLookAndFeel(info.getClassName());
238
+
239
+ break;
240
+
241
+ }
242
+
243
+ }
244
+
245
+ } catch (ClassNotFoundException ex) {
246
+
247
+ java.util.logging.Logger.getLogger(shitumon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
248
+
249
+ } catch (InstantiationException ex) {
250
+
251
+ java.util.logging.Logger.getLogger(shitumon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
252
+
253
+ } catch (IllegalAccessException ex) {
254
+
255
+ java.util.logging.Logger.getLogger(shitumon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
256
+
257
+ } catch (javax.swing.UnsupportedLookAndFeelException ex) {
258
+
259
+ java.util.logging.Logger.getLogger(shitumon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
260
+
261
+ }
262
+
263
+ //</editor-fold>
264
+
265
+
266
+
267
+ /* Create and display the form */
268
+
269
+ java.awt.EventQueue.invokeLater(new Runnable() {
270
+
271
+ public void run() {
272
+
273
+ new shitumon().setVisible(true);
274
+
275
+ }
276
+
277
+ });
278
+
279
+ }
280
+
281
+
282
+
283
+ // Variables declaration - do not modify
284
+
285
+ private javax.swing.JTextArea TA1;
286
+
287
+ private javax.swing.JComboBox<String> cb1;
288
+
289
+ private javax.swing.JScrollPane jScrollPane1;
290
+
291
+ // End of variables declaration
292
+
293
+ }
294
+
295
+ ```
296
+
297
+
298
+
299
+ ご助力頂けますと幸いです。
24
300
 
25
301
  宜しくお願い致します。