質問編集履歴

2

追記忘れです。すみません。fx:controllerはMainControllerに紐づけております

2017/12/19 07:21

投稿

hositaka
hositaka

スコア14

test CHANGED
File without changes
test CHANGED
@@ -166,14 +166,6 @@
166
166
 
167
167
  HelloController controller = loader.getController();
168
168
 
169
- //this.replaceSceneContent(controller);
170
-
171
- /*Scene scene = new Scene(root);
172
-
173
- stage.setScene(scene);
174
-
175
- stage.show();*/
176
-
177
169
  stage2.setTitle("Hello");
178
170
 
179
171
 
@@ -196,16 +188,6 @@
196
188
 
197
189
  }
198
190
 
199
- /**
200
-
201
- * Get Instance
202
-
203
- *
204
-
205
- * @return
206
-
207
- */
208
-
209
191
  public static Original getInstance() {
210
192
 
211
193
  return instance;
@@ -677,3 +659,35 @@
677
659
  }
678
660
 
679
661
  ```
662
+
663
+ Main.fxml
664
+
665
+ ```
666
+
667
+ <?xml version="1.0" encoding="UTF-8"?>
668
+
669
+
670
+
671
+ <?import javafx.scene.control.*?>
672
+
673
+ <?import javafx.scene.text.*?>
674
+
675
+ <?import java.lang.*?>
676
+
677
+ <?import javafx.scene.layout.*?>
678
+
679
+ <?import javafx.collections.FXCollections ?>
680
+
681
+
682
+
683
+
684
+
685
+
686
+
687
+ <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fx.MainController">
688
+
689
+ <略>
690
+
691
+ </AnchorPane>
692
+
693
+ ```

1

追記いたしました

2017/12/19 07:21

投稿

hositaka
hositaka

スコア14

test CHANGED
File without changes
test CHANGED
@@ -12,125 +12,7 @@
12
12
 
13
13
  java.lang.NullPointerException
14
14
 
15
- at fx.Original.MainController(Original.java:112)
15
+ <略>
16
-
17
- at fx.HelloController.onHelloButtonClicked(HelloController.java:27)
18
-
19
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
20
-
21
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
22
-
23
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
24
-
25
- at java.lang.reflect.Method.invoke(Method.java:498)
26
-
27
- at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
28
-
29
- at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
30
-
31
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
32
-
33
- at java.lang.reflect.Method.invoke(Method.java:498)
34
-
35
- at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
36
-
37
- at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
38
-
39
- at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
40
-
41
- at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
42
-
43
- at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
44
-
45
- at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
46
-
47
- at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
48
-
49
- at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
50
-
51
- at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
52
-
53
- at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
54
-
55
- at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
56
-
57
- at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
58
-
59
- at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
60
-
61
- at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
62
-
63
- at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
64
-
65
- at javafx.event.Event.fireEvent(Event.java:198)
66
-
67
- at javafx.scene.Node.fireEvent(Node.java:8413)
68
-
69
- at javafx.scene.control.Button.fire(Button.java:185)
70
-
71
- at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
72
-
73
- at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
74
-
75
- at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
76
-
77
- at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
78
-
79
- at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
80
-
81
- at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
82
-
83
- at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
84
-
85
- at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
86
-
87
- at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
88
-
89
- at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
90
-
91
- at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
92
-
93
- at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
94
-
95
- at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
96
-
97
- at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
98
-
99
- at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
100
-
101
- at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
102
-
103
- at javafx.event.Event.fireEvent(Event.java:198)
104
-
105
- at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
106
-
107
- at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
108
-
109
- at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
110
-
111
- at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
112
-
113
- at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
114
-
115
- at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
116
-
117
- at java.security.AccessController.doPrivileged(Native Method)
118
-
119
- at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:417)
120
-
121
- at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
122
-
123
- at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
124
-
125
- at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
126
-
127
- at com.sun.glass.ui.View.notifyMouse(View.java:937)
128
-
129
- at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
130
-
131
- at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
132
-
133
- at java.lang.Thread.run(Thread.java:748)
134
16
 
135
17
 
136
18
 
@@ -479,3 +361,319 @@
479
361
  java version "1.8.0_151"
480
362
 
481
363
  eclipse:Version: Oxygen.1a Release (4.7.1a)
364
+
365
+
366
+
367
+ ###追記
368
+
369
+ MainController.java
370
+
371
+ ```
372
+
373
+ package fx;
374
+
375
+
376
+
377
+ import <略>
378
+
379
+
380
+
381
+ public class MainController extends AnchorPane implements Initializable{
382
+
383
+
384
+
385
+ @FXML
386
+
387
+ private ChoiceBox<String> dropDown;
388
+
389
+
390
+
391
+ @FXML
392
+
393
+ private TextField textBox;
394
+
395
+
396
+
397
+ @FXML
398
+
399
+ private Button button;
400
+
401
+
402
+
403
+ @Override
404
+
405
+ public void initialize(URL location, ResourceBundle resources) {
406
+
407
+ // 何もしない
408
+
409
+ }
410
+
411
+
412
+
413
+ public MainController() {
414
+
415
+
416
+
417
+ }
418
+
419
+
420
+
421
+
422
+
423
+ @FXML
424
+
425
+ public void onClick(ActionEvent event) {
426
+
427
+ // テキストボックスに文字列をセットする
428
+
429
+ textBox.setText("ボタンを押しました。");
430
+
431
+ }
432
+
433
+
434
+
435
+ }
436
+
437
+ ```
438
+
439
+ MainConrollerへの切り替え Hellocontroller.java
440
+
441
+ ```
442
+
443
+ package fx;
444
+
445
+
446
+
447
+ import java.io.IOException;
448
+
449
+ import java.net.URL;
450
+
451
+ import java.util.ResourceBundle;
452
+
453
+
454
+
455
+ import javafx.event.ActionEvent;
456
+
457
+ import javafx.fxml.FXML;
458
+
459
+ import javafx.fxml.FXMLLoader;
460
+
461
+ import javafx.fxml.Initializable;
462
+
463
+ import javafx.scene.control.Button;
464
+
465
+ import javafx.scene.control.Label;
466
+
467
+ import javafx.scene.layout.AnchorPane;
468
+
469
+
470
+
471
+ public class HelloController extends AnchorPane{
472
+
473
+
474
+
475
+ @FXML private Button helloButton;
476
+
477
+ @FXML private Label helloLabel;
478
+
479
+
480
+
481
+ private int cnt;
482
+
483
+
484
+
485
+
486
+
487
+ @FXML
488
+
489
+ public void onHelloButtonClicked(ActionEvent event) throws IOException {
490
+
491
+ /*this.helloLabel.setText("clicked! : " + cnt);
492
+
493
+ cnt++;*/
494
+
495
+ Original.getInstance().MainController();
496
+
497
+ }
498
+
499
+
500
+
501
+ public void onClick(ActionEvent event) {
502
+
503
+ /*this.helloLabel.setText("clicked! : " + cnt);
504
+
505
+ cnt++;*/
506
+
507
+ //Original.getInstance().MainController();
508
+
509
+ }
510
+
511
+ }
512
+
513
+ ```
514
+
515
+ Original.java
516
+
517
+ ```
518
+
519
+ package fx;
520
+
521
+
522
+
523
+ import java.io.IOException;
524
+
525
+
526
+
527
+ import javafx.application.Application;
528
+
529
+ import javafx.fxml.FXMLLoader;
530
+
531
+ import javafx.scene.Parent;
532
+
533
+ import javafx.scene.Scene;
534
+
535
+ import javafx.stage.Stage;
536
+
537
+
538
+
539
+ public class Original extends Application {
540
+
541
+
542
+
543
+ private static Original instance;
544
+
545
+
546
+
547
+ private Stage stage;
548
+
549
+ @Override
550
+
551
+ public void start(Stage primaryStage) throws Exception {
552
+
553
+
554
+
555
+ // インスタンス
556
+
557
+ instance = this;
558
+
559
+
560
+
561
+ try {
562
+
563
+ FXMLLoader loader = new FXMLLoader(getClass().getResource("hello.fxml"));
564
+
565
+ Parent root = loader.load(); // fx:rootに紐付いたノードインスタンスが得られる
566
+
567
+ stage = primaryStage;
568
+
569
+ // タイトルセット
570
+
571
+ primaryStage.setTitle("Hello");
572
+
573
+
574
+
575
+ // シーン生成
576
+
577
+ Scene scene = new Scene(root);
578
+
579
+
580
+
581
+ //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
582
+
583
+ primaryStage.setScene(scene);
584
+
585
+ primaryStage.show();
586
+
587
+ } catch(Exception e) {
588
+
589
+ e.printStackTrace();
590
+
591
+ }
592
+
593
+
594
+
595
+
596
+
597
+
598
+
599
+ }
600
+
601
+
602
+
603
+ public static void main(String[] args) {
604
+
605
+ launch(args);
606
+
607
+ }
608
+
609
+
610
+
611
+ public void sendhelloController(String labelText) {
612
+
613
+
614
+
615
+ stage.setTitle("Hello");
616
+
617
+
618
+
619
+ HelloController controller = new HelloController();
620
+
621
+ this.replaceSceneContent(controller);
622
+
623
+ }
624
+
625
+
626
+
627
+ private void replaceSceneContent(Parent controller) {
628
+
629
+ Scene scene = stage.getScene();
630
+
631
+ if (scene == null) {
632
+
633
+ scene = new Scene(controller);
634
+
635
+ stage.setScene(scene);//このステージで使用されるシーンを指定
636
+
637
+ System.out.println("a");//確認用文字出力
638
+
639
+ } else {
640
+
641
+ stage.getScene().setRoot(controller);
642
+
643
+
644
+
645
+ }
646
+
647
+ }
648
+
649
+
650
+
651
+ public void MainController() throws IOException {
652
+
653
+ stage.setTitle("Main");
654
+
655
+ FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
656
+
657
+ Parent root = loader.load(); // fx:rootに紐付いたノードインスタンスが得られる
658
+
659
+ MainController controller = loader.getController(); // fx:controllerに紐付いたコントローラインスタンスが得られる
660
+
661
+ this.replaceSceneContent(controller);
662
+
663
+
664
+
665
+ }
666
+
667
+
668
+
669
+ public static Original getInstance() {
670
+
671
+ return instance;
672
+
673
+ }
674
+
675
+
676
+
677
+ }
678
+
679
+ ```