質問編集履歴
2
現状
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,6 +6,10 @@
|
|
6
6
|
|
7
7
|
で、チェックボックスが押された行の値を取得することで、それにあたるデータの削除を実現させたいです。
|
8
8
|
|
9
|
+
そこで、チェックボックスで選択した行の値を取得する方法があれば教えていただきたいです。
|
10
|
+
|
11
|
+
色々調べてみましたが、分かりませんでした。
|
12
|
+
|
9
13
|
|
10
14
|
|
11
15
|
|
1
現状
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,5 +1,553 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
現在、TableViewに
|
3
|
+
現在、TableViewにMySQLのテーブルの中身を挿入しています。
|
4
|
+
|
4
|
-
|
5
|
+
そこで私は、TableViewのカラムに削除用のチェックボックスを追加しました。
|
6
|
+
|
5
|
-
チェックされた行の値を取得すること
|
7
|
+
で、チェックボックスが押された行の値を取得することで、それにあたるデータの削除を実現させたいです。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
### 該当のソースコード
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
package application;
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
import java.io.IOException;
|
22
|
+
|
23
|
+
import java.net.URL;
|
24
|
+
|
25
|
+
import java.sql.Connection;
|
26
|
+
|
27
|
+
import java.sql.DriverManager;
|
28
|
+
|
29
|
+
import java.sql.PreparedStatement;
|
30
|
+
|
31
|
+
import java.sql.ResultSet;
|
32
|
+
|
33
|
+
import java.sql.SQLException;
|
34
|
+
|
35
|
+
import java.util.ResourceBundle;
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
import javafx.beans.property.BooleanProperty;
|
40
|
+
|
41
|
+
import javafx.beans.property.IntegerProperty;
|
42
|
+
|
43
|
+
import javafx.beans.property.SimpleBooleanProperty;
|
44
|
+
|
45
|
+
import javafx.beans.property.SimpleIntegerProperty;
|
46
|
+
|
47
|
+
import javafx.beans.property.SimpleStringProperty;
|
48
|
+
|
49
|
+
import javafx.beans.property.StringProperty;
|
50
|
+
|
51
|
+
import javafx.collections.FXCollections;
|
52
|
+
|
53
|
+
import javafx.collections.ObservableList;
|
54
|
+
|
55
|
+
import javafx.event.ActionEvent;
|
56
|
+
|
57
|
+
import javafx.fxml.FXML;
|
58
|
+
|
59
|
+
import javafx.fxml.FXMLLoader;
|
60
|
+
|
61
|
+
import javafx.scene.Node;
|
62
|
+
|
63
|
+
import javafx.scene.Parent;
|
64
|
+
|
65
|
+
import javafx.scene.Scene;
|
66
|
+
|
67
|
+
import javafx.scene.control.Button;
|
68
|
+
|
69
|
+
import javafx.scene.control.TableColumn;
|
70
|
+
|
71
|
+
import javafx.scene.control.TableView;
|
72
|
+
|
73
|
+
import javafx.scene.control.cell.CheckBoxTableCell;
|
74
|
+
|
75
|
+
import javafx.scene.control.cell.PropertyValueFactory;
|
76
|
+
|
77
|
+
import javafx.scene.control.cell.TextFieldTableCell;
|
78
|
+
|
79
|
+
import javafx.stage.Modality;
|
80
|
+
|
81
|
+
import javafx.stage.Stage;
|
82
|
+
|
83
|
+
import javafx.util.converter.IntegerStringConverter;
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
public class SampleController {
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
private Connection connection = null;
|
92
|
+
|
93
|
+
private PreparedStatement ps = null;
|
94
|
+
|
95
|
+
private ResultSet rs = null;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
@FXML
|
100
|
+
|
101
|
+
private ResourceBundle resources;
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
@FXML
|
106
|
+
|
107
|
+
private URL location;
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
@FXML
|
112
|
+
|
113
|
+
private TableView<dataClass> table;
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
@FXML
|
118
|
+
|
119
|
+
private TableColumn<dataClass, String> nameColumn;
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
@FXML
|
124
|
+
|
125
|
+
private TableColumn<dataClass, Integer> priceColumn;
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
@FXML
|
130
|
+
|
131
|
+
private TableColumn<dataClass, Integer> zaikoColumn;
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
@FXML
|
136
|
+
|
137
|
+
private TableColumn<dataClass, Boolean> discardColumn;
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
@FXML
|
142
|
+
|
143
|
+
private Button btsyukko;
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
@FXML
|
148
|
+
|
149
|
+
private Button btupdate;
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
@FXML
|
154
|
+
|
155
|
+
private Button btnyuuko;
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
@FXML
|
160
|
+
|
161
|
+
void clicknyuuko(ActionEvent event)
|
162
|
+
|
163
|
+
{
|
164
|
+
|
165
|
+
//入庫画面
|
166
|
+
|
167
|
+
try {
|
168
|
+
|
169
|
+
Parent parent = FXMLLoader.load(getClass().getResource("Nyuuko.fxml"));
|
170
|
+
|
171
|
+
Scene scene = new Scene(parent);
|
172
|
+
|
173
|
+
Stage stage = new Stage();
|
174
|
+
|
175
|
+
stage.setScene(scene);
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
// 以下の2行を加えると、モーダルopenになる
|
180
|
+
|
181
|
+
stage.initModality(Modality.WINDOW_MODAL);
|
182
|
+
|
183
|
+
stage.initOwner(((Node)event.getSource()).getScene().getWindow());
|
184
|
+
|
185
|
+
stage.show();
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
}catch(IOException e) {
|
190
|
+
|
191
|
+
e.printStackTrace();
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
}finally {
|
196
|
+
|
197
|
+
closeSQL(connection, ps, rs);
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
@FXML
|
206
|
+
|
207
|
+
void clicksyukko(ActionEvent event)
|
208
|
+
|
209
|
+
{
|
210
|
+
|
211
|
+
//出庫画面
|
212
|
+
|
213
|
+
try {
|
214
|
+
|
215
|
+
Parent parent = FXMLLoader.load(getClass().getResource("Syukko.fxml"));
|
216
|
+
|
217
|
+
Scene scene = new Scene(parent);
|
218
|
+
|
219
|
+
Stage stage = new Stage();
|
220
|
+
|
221
|
+
stage.setScene(scene);
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
// 以下の2行を加えると、モーダルopenになる
|
226
|
+
|
227
|
+
stage.initModality(Modality.WINDOW_MODAL);
|
228
|
+
|
229
|
+
stage.initOwner(((Node)event.getSource()).getScene().getWindow());
|
230
|
+
|
231
|
+
stage.show();
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
}catch(IOException e) {
|
236
|
+
|
237
|
+
e.printStackTrace();
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
}finally {
|
242
|
+
|
243
|
+
closeSQL(connection, ps, rs);
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
//更新
|
252
|
+
|
253
|
+
@FXML
|
254
|
+
|
255
|
+
void clickupdate(ActionEvent event)
|
256
|
+
|
257
|
+
{
|
258
|
+
|
259
|
+
table.getItems().setAll(DB());
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
@FXML
|
266
|
+
|
267
|
+
void initialize() {
|
268
|
+
|
269
|
+
assert table != null : "fx:id=\"table\" was not injected: check your FXML file 'Sample.fxml'.";
|
270
|
+
|
271
|
+
assert nameColumn != null : "fx:id=\"nameColumn\" was not injected: check your FXML file 'Sample.fxml'.";
|
272
|
+
|
273
|
+
assert priceColumn != null : "fx:id=\"priceColumn\" was not injected: check your FXML file 'Sample.fxml'.";
|
274
|
+
|
275
|
+
assert zaikoColumn != null : "fx:id=\"zaikoColumn\" was not injected: check your FXML file 'Sample.fxml'.";
|
276
|
+
|
277
|
+
assert discardColumn != null : "fx:id=\"discardColumn\" was not injected: check your FXML file 'Sample.fxml'.";
|
278
|
+
|
279
|
+
assert btsyukko != null : "fx:id=\"btsyukko\" was not injected: check your FXML file 'Sample.fxml'.";
|
280
|
+
|
281
|
+
assert btupdate != null : "fx:id=\"btupdate\" was not injected: check your FXML file 'Sample.fxml'.";
|
282
|
+
|
283
|
+
assert btnyuuko != null : "fx:id=\"btnyuuko\" was not injected: check your FXML file 'Sample.fxml'.";
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
//関連付け
|
288
|
+
|
289
|
+
nameColumn.setCellValueFactory(new PropertyValueFactory<dataClass, String>("name"));
|
290
|
+
|
291
|
+
priceColumn.setCellValueFactory(new PropertyValueFactory<dataClass, Integer>("price"));
|
292
|
+
|
293
|
+
zaikoColumn.setCellValueFactory(new PropertyValueFactory<dataClass, Integer>("zaiko"));
|
294
|
+
|
295
|
+
//discardColumn.setCellValueFactory(new PropertyValueFactory<dataClass, Boolean>("discard"));
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
//編集可能
|
300
|
+
|
301
|
+
table.setEditable(true);
|
302
|
+
|
303
|
+
//編集可能にするためにセルにTextFieldを埋め込む
|
304
|
+
|
305
|
+
nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());
|
306
|
+
|
307
|
+
//Integerを可能にする
|
308
|
+
|
309
|
+
priceColumn.setCellFactory(TextFieldTableCell.<dataClass, Integer>forTableColumn(new IntegerStringConverter()));
|
310
|
+
|
311
|
+
zaikoColumn.setCellFactory(TextFieldTableCell.<dataClass, Integer>forTableColumn(new IntegerStringConverter()));
|
312
|
+
|
313
|
+
//チェックボックス埋め込み
|
314
|
+
|
315
|
+
discardColumn.setCellFactory(CheckBoxTableCell.forTableColumn(discardColumn));
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
table.getItems().setAll(DB());
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
public class dataClass
|
326
|
+
|
327
|
+
{
|
328
|
+
|
329
|
+
private StringProperty name = new SimpleStringProperty();
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
public StringProperty nameProperty()
|
334
|
+
|
335
|
+
{
|
336
|
+
|
337
|
+
return name;
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
public void setname(String val)
|
344
|
+
|
345
|
+
{
|
346
|
+
|
347
|
+
name.set(val);
|
348
|
+
|
349
|
+
}
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
//
|
354
|
+
|
355
|
+
private IntegerProperty price = new SimpleIntegerProperty();
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
public IntegerProperty priceProperty()
|
360
|
+
|
361
|
+
{
|
362
|
+
|
363
|
+
return price;
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
public void setprice(int val)
|
370
|
+
|
371
|
+
{
|
372
|
+
|
373
|
+
price.set(val);
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
//
|
380
|
+
|
381
|
+
private IntegerProperty zaiko = new SimpleIntegerProperty();
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
public IntegerProperty zaikoProperty()
|
386
|
+
|
387
|
+
{
|
388
|
+
|
389
|
+
return zaiko;
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
public void setzaiko(int val)
|
396
|
+
|
397
|
+
{
|
398
|
+
|
399
|
+
zaiko.set(val);
|
400
|
+
|
401
|
+
}
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
//
|
406
|
+
|
407
|
+
private BooleanProperty discard = new SimpleBooleanProperty();
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
public BooleanProperty discardProperty()
|
412
|
+
|
413
|
+
{
|
414
|
+
|
415
|
+
return discard;
|
416
|
+
|
417
|
+
}
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
public void setdiscard(Boolean val)
|
422
|
+
|
423
|
+
{
|
424
|
+
|
425
|
+
discard.set(val);
|
426
|
+
|
427
|
+
}
|
428
|
+
|
429
|
+
}
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
private ObservableList<dataClass> DB()
|
434
|
+
|
435
|
+
{
|
436
|
+
|
437
|
+
ObservableList<dataClass> data = FXCollections.observableArrayList();
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
try
|
442
|
+
|
443
|
+
{
|
444
|
+
|
445
|
+
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/orderdb?characterEncoding=UTF-8&serverTimezone=JST", "", "");
|
446
|
+
|
447
|
+
ps = connection.prepareStatement("select * from product");
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
rs = ps.executeQuery();
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
while(rs.next()) {
|
456
|
+
|
457
|
+
|
458
|
+
|
459
|
+
//1の部分はカラム番号
|
460
|
+
|
461
|
+
String name = rs.getString(1);
|
462
|
+
|
463
|
+
int price = rs.getInt(2);
|
464
|
+
|
465
|
+
int zaiko = rs.getInt(3);
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
dataClass column = new dataClass();
|
470
|
+
|
471
|
+
column.setname(name);
|
472
|
+
|
473
|
+
column.setprice(price);
|
474
|
+
|
475
|
+
column.setzaiko(zaiko);
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
data.add(column);
|
480
|
+
|
481
|
+
}
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
// データベースとの接続に失敗した場合
|
486
|
+
|
487
|
+
}catch(SQLException ex) {
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
ex.printStackTrace();
|
492
|
+
|
493
|
+
System.err.println("MySQLに接続できませんでした。");
|
494
|
+
|
495
|
+
|
496
|
+
|
497
|
+
}finally {
|
498
|
+
|
499
|
+
closeSQL(connection, ps, rs);
|
500
|
+
|
501
|
+
}
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
return data;
|
506
|
+
|
507
|
+
}
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
void closeSQL(Connection connection, PreparedStatement pre_statement, ResultSet result_set) {
|
512
|
+
|
513
|
+
try {
|
514
|
+
|
515
|
+
// データベースとの接続を解除
|
516
|
+
|
517
|
+
if (connection != null) {
|
518
|
+
|
519
|
+
connection.close();
|
520
|
+
|
521
|
+
}
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
if (pre_statement != null) {
|
526
|
+
|
527
|
+
pre_statement.close();
|
528
|
+
|
529
|
+
}
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
if (result_set != null) {
|
534
|
+
|
535
|
+
result_set.close();
|
536
|
+
|
537
|
+
}
|
538
|
+
|
539
|
+
|
540
|
+
|
541
|
+
}catch(SQLException ex) {
|
542
|
+
|
543
|
+
System.err.println("Error closeSql Func");
|
544
|
+
|
545
|
+
//どこで、エラーが起こっているかが分かる
|
546
|
+
|
547
|
+
ex.printStackTrace();
|
548
|
+
|
549
|
+
}
|
550
|
+
|
551
|
+
}
|
552
|
+
|
553
|
+
}
|