質問編集履歴
3
FXMLを追記いたしました
test
CHANGED
File without changes
|
test
CHANGED
@@ -325,3 +325,57 @@
|
|
325
325
|
}
|
326
326
|
|
327
327
|
```
|
328
|
+
|
329
|
+
```FXML
|
330
|
+
|
331
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
<?import javafx.scene.control.Button?>
|
336
|
+
|
337
|
+
<?import javafx.scene.control.TableColumn?>
|
338
|
+
|
339
|
+
<?import javafx.scene.control.TableView?>
|
340
|
+
|
341
|
+
<?import javafx.scene.layout.AnchorPane?>
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="maker.makerControl">
|
346
|
+
|
347
|
+
<children>
|
348
|
+
|
349
|
+
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="400.0" prefWidth="600.0">
|
350
|
+
|
351
|
+
<children>
|
352
|
+
|
353
|
+
<TableView fx:id="fxTableView" layoutX="33.0" layoutY="52.0" prefHeight="296.0" prefWidth="534.0">
|
354
|
+
|
355
|
+
<columns>
|
356
|
+
|
357
|
+
<TableColumn fx:id="fxMakerName" prefWidth="174.0" text="メーカー名" />
|
358
|
+
|
359
|
+
<TableColumn fx:id="fxMakerNameShort" prefWidth="93.0" text="略称" />
|
360
|
+
|
361
|
+
<TableColumn fx:id="fxMakerComment" prefWidth="266.0" text="備考" />
|
362
|
+
|
363
|
+
</columns>
|
364
|
+
|
365
|
+
</TableView>
|
366
|
+
|
367
|
+
</children>
|
368
|
+
|
369
|
+
</AnchorPane>
|
370
|
+
|
371
|
+
<Button fx:id="fxOK" layoutX="523.0" layoutY="361.0" mnemonicParsing="false" onAction="#btnAction" text="F12 保存" />
|
372
|
+
|
373
|
+
<Button fx:id="fxCancel" layoutX="461.0" layoutY="361.0" mnemonicParsing="false" text="F9 取消" />
|
374
|
+
|
375
|
+
</children>
|
376
|
+
|
377
|
+
</AnchorPane>
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
```
|
2
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,8 +14,6 @@
|
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
-
javaFXのテキストには
|
18
|
-
|
19
17
|
以下のmakerControlクラスのshowTableメソッドでエラーが発生しています。
|
20
18
|
|
21
19
|
DBから取得した表(ObservableList)をTableViewへセットする際に発生しているようですが、デバッグモードで見ても表に値は入っているようです。
|
1
途中で送信されていました。申し訳ございません。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,20 +1,34 @@
|
|
1
|
+
JavaFXで、共通化したSQL実行から取得した表をTableViewに表示させたい
|
2
|
+
|
3
|
+
|
4
|
+
|
1
5
|
### 前提・実現したいこと
|
2
6
|
|
3
7
|
|
4
8
|
|
5
9
|
postgeSQLとJavaFXを使って様々なデータを参照、登録できるようなシステムを作ろうとしています。
|
6
10
|
|
11
|
+
DBへの接続は共通化しており、SQLの実行も共通化しています(書き方が怪しいですが)
|
12
|
+
|
13
|
+
### 発生している問題・エラーメッセージ
|
14
|
+
|
15
|
+
|
16
|
+
|
7
17
|
javaFXのテキストには
|
8
18
|
|
9
|
-
|
19
|
+
以下のmakerControlクラスのshowTableメソッドでエラーが発生しています。
|
20
|
+
|
10
|
-
|
21
|
+
DBから取得した表(ObservableList)をTableViewへセットする際に発生しているようですが、デバッグモードで見ても表に値は入っているようです。
|
22
|
+
|
11
|
-
|
23
|
+
どういった原因が考えられるでしょうか。
|
12
|
-
|
13
|
-
|
14
|
-
|
24
|
+
|
15
|
-
```
|
25
|
+
```
|
26
|
+
|
16
|
-
|
27
|
+
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
|
28
|
+
|
29
|
+
at com.stockon.maker.makerControl.showTable(makerControl.java:116)
|
30
|
+
|
17
|
-
|
31
|
+
(以下略)
|
18
32
|
|
19
33
|
```
|
20
34
|
|
@@ -22,26 +36,294 @@
|
|
22
36
|
|
23
37
|
### 該当のソースコード
|
24
38
|
|
25
|
-
|
26
|
-
|
27
|
-
```
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
39
|
+
長くなりますので、関係ない箇所は省略しています。
|
40
|
+
|
41
|
+
```Java
|
42
|
+
|
43
|
+
public class makerModel {
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
private String makerName;
|
48
|
+
|
49
|
+
private String makerNameShort;
|
50
|
+
|
51
|
+
private String makerComment;
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
public makerModel(String name, String nameShort, String comment) {
|
56
|
+
|
57
|
+
this.makerName = name;
|
58
|
+
|
59
|
+
this.makerNameShort = nameShort;
|
60
|
+
|
61
|
+
this.makerComment = comment;
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
```
|
68
|
+
|
69
|
+
```Java
|
70
|
+
|
71
|
+
public class makerControl extends Application {
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
// 画面項目
|
76
|
+
|
77
|
+
@FXML
|
78
|
+
|
79
|
+
private TableView<makerModel> tableView;
|
80
|
+
|
81
|
+
@FXML
|
82
|
+
|
83
|
+
public static TableColumn<makerModel, String> fxMakerName;
|
84
|
+
|
85
|
+
@FXML
|
86
|
+
|
87
|
+
public static TableColumn<makerModel, String> fxMakerNameShort;
|
88
|
+
|
89
|
+
@FXML
|
90
|
+
|
91
|
+
public static TableColumn<makerModel, String> fxMakerComment;
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
public static ObservableList<makerModel> tableMaker = FXCollections.observableArrayList();
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
@Override
|
100
|
+
|
101
|
+
public void start(Stage stage) throws SQLException, ClassNotFoundException {
|
102
|
+
|
103
|
+
try {
|
104
|
+
|
105
|
+
// FXMLのレイアウトをロード
|
106
|
+
|
107
|
+
AnchorPane root = FXMLLoader.load(getClass().getResource("makerView.fxml"));
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
// シーン生成
|
112
|
+
|
113
|
+
Scene scene = new Scene(root);
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
// タイトルセット
|
118
|
+
|
119
|
+
stage.setTitle("makerView");
|
120
|
+
|
121
|
+
stage.setScene(scene);
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
showTable(stage);
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
stage.show();
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
} catch (IOException ex) {
|
134
|
+
|
135
|
+
Logger.getLogger(makerControl.class.getName()).log(Level.SEVERE, null, ex);
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
public void showTable(Stage stage) throws SQLException, ClassNotFoundException {
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
String makerName = "";
|
148
|
+
|
149
|
+
String makerNameShort = "";
|
150
|
+
|
151
|
+
String makerComment = "";
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
makerModel mm = new makerModel();
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
// SQLを実行してList<Object>型で格納する
|
160
|
+
|
161
|
+
List<Object> arrayData = utilDB.executeSelectQuery("SELECT \"makerName\", \"makerNameShort\", \"makerComment\" FROM st_maker");
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
// List型→配列型
|
166
|
+
|
167
|
+
Object[] obj = arrayData.toArray();
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
int rowCount = arrayData.size();
|
172
|
+
|
173
|
+
for (int i = 0; i < rowCount; i++) {
|
174
|
+
|
175
|
+
String[] rowStr = new String[3];
|
176
|
+
|
177
|
+
rowStr = (String[]) obj[i];
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
//一行ずつ読み込む
|
182
|
+
|
183
|
+
for (int col = 0; col < rowStr.length; col++) {
|
184
|
+
|
185
|
+
// nullの場合は空文字に置き換える
|
186
|
+
|
187
|
+
if (null == rowStr[col]) {
|
188
|
+
|
189
|
+
rowStr[col] = "";
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
switch (col) {
|
194
|
+
|
195
|
+
case 0:
|
196
|
+
|
197
|
+
System.out.println("MakerName():" + rowStr[col]);
|
198
|
+
|
199
|
+
makerName = rowStr[col];
|
200
|
+
|
201
|
+
break;
|
202
|
+
|
203
|
+
case 1:
|
204
|
+
|
205
|
+
System.out.println("makerNameShort():" + rowStr[col]);
|
206
|
+
|
207
|
+
makerNameShort = rowStr[col];
|
208
|
+
|
209
|
+
break;
|
210
|
+
|
211
|
+
case 2:
|
212
|
+
|
213
|
+
System.out.println("makerComment():" + rowStr[col]);
|
214
|
+
|
215
|
+
makerComment = rowStr[col];
|
216
|
+
|
217
|
+
break;
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
// 一行ずつObservableListに追加する
|
224
|
+
|
225
|
+
tableMaker.add(new makerModel(makerName, makerNameShort, makerComment));
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
// TableViewに一覧をセットする
|
230
|
+
|
231
|
+
tableView.itemsProperty().setValue(tableMaker); // Line 116:ここでエラーが発生
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
fxMakerName.setCellValueFactory(new PropertyValueFactory<makerModel,String>(makerName));
|
236
|
+
|
237
|
+
fxMakerNameShort.setCellValueFactory(new PropertyValueFactory<makerModel,String>(makerNameShort));
|
238
|
+
|
239
|
+
fxMakerComment.setCellValueFactory(new PropertyValueFactory<makerModel,String>(makerComment));
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
```
|
246
|
+
|
247
|
+
```Java
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
public static List<Object> executeSelectQuery(String sql) throws SQLException, ClassNotFoundException {
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
// DBに接続する
|
256
|
+
|
257
|
+
connect();
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
// SQLの発行
|
262
|
+
|
263
|
+
resultSet = statement.executeQuery(sql);
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
// 値の取得
|
268
|
+
|
269
|
+
// フィールド一覧を取得
|
270
|
+
|
271
|
+
List<String> resultList = new ArrayList<String>();
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
List<Object> list = new ArrayList<Object>();
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
ResultSetMetaData data = resultSet.getMetaData();
|
280
|
+
|
281
|
+
int colCount = data.getColumnCount();
|
282
|
+
|
283
|
+
for (int i = 1; i <= colCount; i++) {
|
284
|
+
|
285
|
+
resultList.add(data.getColumnName(i));
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
//結果の出力
|
292
|
+
|
293
|
+
int rowCount = 0;
|
294
|
+
|
295
|
+
while (resultSet.next()) {
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
String[] ColArray = new String[colCount];
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
int col = 0;
|
304
|
+
|
305
|
+
for (String field : resultList) {
|
306
|
+
|
307
|
+
System.out.println(field + ":" + resultSet.getString(field));
|
308
|
+
|
309
|
+
ColArray[col] = resultSet.getString(field);
|
310
|
+
|
311
|
+
col++;
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
list.add(rowCount, ColArray);
|
316
|
+
|
317
|
+
rowCount++;
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
// DB切断
|
322
|
+
|
323
|
+
close();
|
324
|
+
|
325
|
+
return list;
|
326
|
+
|
327
|
+
}
|
328
|
+
|
329
|
+
```
|