回答編集履歴
1
質問者様追加コメントへの補足とサンプル追加
test
CHANGED
@@ -287,3 +287,257 @@
|
|
287
287
|
|
288
288
|
|
289
289
|
![イメージ説明](2285bd2135f0e153a2c8552fcabd6b6b.png)
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
---
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
## コメントへの追記 @ 1/29
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
私が試した環境(JRE 1.8.0_151および、JDK 9.0.4)だけかもしれませんが、
|
302
|
+
|
303
|
+
表のデータ数(行数)に合わせて、ボタンは自動で追加・削除されます。
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
上記のサンプルでは、表のデータを4件(26行目で設定)としていますが、
|
308
|
+
|
309
|
+
後から、データを追加したり、削除しても問題ありませんでした(全件入れ直しも同様)。
|
310
|
+
|
311
|
+
表データを追加・削除するサンプルを記載しましたので、あわせてご確認ください。
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
```java
|
316
|
+
|
317
|
+
import javafx.application.Application;
|
318
|
+
|
319
|
+
import javafx.beans.property.SimpleStringProperty;
|
320
|
+
|
321
|
+
import javafx.collections.FXCollections;
|
322
|
+
|
323
|
+
import javafx.collections.ObservableList;
|
324
|
+
|
325
|
+
import javafx.scene.Scene;
|
326
|
+
|
327
|
+
import javafx.scene.control.Button;
|
328
|
+
|
329
|
+
import javafx.scene.control.TableColumn;
|
330
|
+
|
331
|
+
import javafx.scene.control.TableView;
|
332
|
+
|
333
|
+
import javafx.scene.control.TextArea;
|
334
|
+
|
335
|
+
import javafx.scene.control.cell.PropertyValueFactory;
|
336
|
+
|
337
|
+
import javafx.scene.layout.BorderPane;
|
338
|
+
|
339
|
+
import javafx.scene.layout.HBox;
|
340
|
+
|
341
|
+
import javafx.stage.Stage;
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
public class ButtonTableAp2 extends Application {
|
346
|
+
|
347
|
+
private ObservableList<Entity> tableData;
|
348
|
+
|
349
|
+
private TextArea console;
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
public static void main(String[] args) {
|
354
|
+
|
355
|
+
launch(args);
|
356
|
+
|
357
|
+
}
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
@Override
|
362
|
+
|
363
|
+
public void start(Stage stage) {
|
364
|
+
|
365
|
+
stage.setWidth(400);
|
366
|
+
|
367
|
+
stage.setHeight(300);
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
// 表の作成
|
372
|
+
|
373
|
+
TableView<Entity> table = new TableView<>();
|
374
|
+
|
375
|
+
table.setItems(tableData=FXCollections.observableArrayList(
|
376
|
+
|
377
|
+
new Entity("名前1", "詳細1"),
|
378
|
+
|
379
|
+
new Entity("名前2", "詳細2"),
|
380
|
+
|
381
|
+
new Entity("名前3", "詳細3"),
|
382
|
+
|
383
|
+
new Entity("名前4", "詳細4")));
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
// 1列名: 名前
|
388
|
+
|
389
|
+
TableColumn firstNameCol = new TableColumn("名前");
|
390
|
+
|
391
|
+
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
|
392
|
+
|
393
|
+
table.getColumns().addAll(firstNameCol);
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
// 2列名: ボタン
|
398
|
+
|
399
|
+
TableColumn actionCol = new TableColumn("詳細");
|
400
|
+
|
401
|
+
//actionCol.setCellValueFactory(new PropertyValueFactory<>("DUMMY")); 設定しなくても動く
|
402
|
+
|
403
|
+
actionCol.setCellFactory(new ButtonCellFactory<Entity>("ボタン", e -> onTableButtonClick(e)));
|
404
|
+
|
405
|
+
table.getColumns().addAll(actionCol);
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
// 表データを変更するためのボタン
|
410
|
+
|
411
|
+
Button addBtn =new Button("表データ追加");
|
412
|
+
|
413
|
+
Button removeBtn =new Button("表データ削除");
|
414
|
+
|
415
|
+
addBtn.setOnAction(event -> addToTable());
|
416
|
+
|
417
|
+
removeBtn.setOnAction(event -> removeFromTable());
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
HBox box=new HBox();
|
422
|
+
|
423
|
+
box.getChildren().addAll(addBtn, removeBtn);
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
// レイアウト
|
430
|
+
|
431
|
+
console = new TextArea();
|
432
|
+
|
433
|
+
BorderPane pane = new BorderPane();
|
434
|
+
|
435
|
+
pane.setTop(box);
|
436
|
+
|
437
|
+
pane.setLeft(table);
|
438
|
+
|
439
|
+
pane.setCenter(console);
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
Scene scene = new Scene(pane);
|
444
|
+
|
445
|
+
stage.setScene(scene);
|
446
|
+
|
447
|
+
stage.show();
|
448
|
+
|
449
|
+
}
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
private void addToTable(){
|
454
|
+
|
455
|
+
int index=tableData.size()+1;
|
456
|
+
|
457
|
+
tableData.add(new Entity("追加名:"+index, "ボタンで追加されたデータです@"+index));
|
458
|
+
|
459
|
+
}
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
private void removeFromTable(){
|
464
|
+
|
465
|
+
if(tableData.size()>0){
|
466
|
+
|
467
|
+
tableData.remove(tableData.size()-1);
|
468
|
+
|
469
|
+
}
|
470
|
+
|
471
|
+
}
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
/** 表のボタン押下時の処理 */
|
476
|
+
|
477
|
+
private void onTableButtonClick(Entity selected) {
|
478
|
+
|
479
|
+
console.setText("名前:\r\n\t" + selected.getName() + "\r\n" + "詳細:\r\n\t" + selected.getDetails());
|
480
|
+
|
481
|
+
}
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
/** 表の行のデータ */
|
486
|
+
|
487
|
+
public static class Entity {
|
488
|
+
|
489
|
+
private final SimpleStringProperty name;
|
490
|
+
|
491
|
+
private final SimpleStringProperty details;
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
private Entity(String name, String details) {
|
496
|
+
|
497
|
+
this.name = new SimpleStringProperty(name);
|
498
|
+
|
499
|
+
this.details = new SimpleStringProperty(details);
|
500
|
+
|
501
|
+
}
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
public String getName() {
|
506
|
+
|
507
|
+
return name.get();
|
508
|
+
|
509
|
+
}
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
public void setName(String nm) {
|
514
|
+
|
515
|
+
name.set(nm);
|
516
|
+
|
517
|
+
}
|
518
|
+
|
519
|
+
|
520
|
+
|
521
|
+
public String getDetails() {
|
522
|
+
|
523
|
+
return details.get();
|
524
|
+
|
525
|
+
}
|
526
|
+
|
527
|
+
|
528
|
+
|
529
|
+
public void setDetails(String dt) {
|
530
|
+
|
531
|
+
details.set(dt);
|
532
|
+
|
533
|
+
}
|
534
|
+
|
535
|
+
}
|
536
|
+
|
537
|
+
}
|
538
|
+
|
539
|
+
```
|
540
|
+
|
541
|
+
![追加前](3c416b2fe081cdadf47a7ec6537756fd.png)
|
542
|
+
|
543
|
+
![追加後](3a1de8a1d3519dc0948987eb7897881f.png)
|