2016-10-30 7 views
1

私はクラスとネストされたJavaFX Controllerのコードを持っています。私はJavaFXコントローラクラスを初期化します。パブリッククラス内のネストされたJavaFXコントローラ

SpeechWindowクラス:SpeechWindowのインスタンスを作成し、メインのJavaFXアプリケーションクラスからそれを呼び出すようにしてください

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.TextArea?> 
<?import javafx.scene.layout.BorderPane?> 


<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1"> 
    <center> 
     <TextArea fx:id="textArea" editable="false" prefHeight="200.0" prefWidth="200.0" promptText="...no response" wrapText="true" BorderPane.alignment="CENTER" /> 
    </center> 
</BorderPane> 

import java.io.IOException; 
import java.net.URL; 
import java.util.ResourceBundle; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

import application.Main; 
import javafx.application.Platform; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Scene; 
import javafx.scene.control.TextArea; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

/** 
* SpeechWindow 
* 
*/ 
public class SpeechWindow extends Stage { 

    private Container container = new Container(); 

    /** 
    * Constructor 
    */ 
    public SpeechWindow() { 

     setTitle("SpeechResults"); 
     setScene(new Scene(container)); 
    } 

    /** 
    * @param text 
    */ 
    public void appendText(String text) { 
     container.appendText(text); 
    } 

    /** 
    * 
    */ 
    public void clear() { 
     container.clear(); 
    } 

    public class Container extends BorderPane implements Initializable { 

     @FXML 
     private TextArea textArea; 

     public Container() { 
      // FXMLLOADER 
      FXMLLoader loader = new FXMLLoader(getClass().getResource("SpeechWindow.fxml")); 
      try { 
       loader.load(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 

     @Override 
     public void initialize(URL location , ResourceBundle resources) { 
      System.out.println("Container has been initialized.."); 
     } 

     public void appendText(String text) { 
      Platform.runLater(() -> textArea.appendText(text + "\n")); 
     } 

     public void clear() { 
      Platform.runLater(textArea::clear); 
     } 

    } 

} 

ここでは、FXMLコードですappendText();メソッドを呼び出します。なぜNULLポインタが出てくるのですか?

目標は次のとおりです。

はその後、私は外側のクラスのインスタンスを作成し、それを使用したいです。


行く何を間違っている:私は、メソッドappendText();SpeechWindowクラスのインスタンスを作成して呼び出す場合

は、私はそれが起こるのはなぜ、私はすべてfine.Whatが行わ見る... nullポインタ例外を取得私の間違い?

答えて

2

Containerクラスを盗ん。

コントローラクラスを設定するには、FXMLLoaderの場合はsetControllerを使用します。

さらに、fxmlを何らかの形でシーンにロードした結果を追加する必要があります。おそらく、fxmlのルート要素をfx:root要素で置き換えるべきです。ところで

:努力Cryptorため

public Container() { 
    // FXMLLOADER 
    FXMLLoader loader = new FXMLLoader(getClass().getResource("SpeechWindow.fxml")); 

    // set controller & root 
    loader.setController(this); 
    loader.setRoot(this); 
    try { 
     loader.load(); 
    } catch (IOException ex) { 
     // throw as cause of RuntimeException 
     throw new IllegalStateException(ex); 
    } 
} 
<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.TextArea?> 
<?import javafx.scene.layout.BorderPane?> 

<fx:root type="javafx.scene.layout.BorderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1"> 
    <center> 
     <TextArea fx:id="textArea" editable="false" prefHeight="200.0" prefWidth="200.0" promptText="...no response" wrapText="true" BorderPane.alignment="CENTER" /> 
    </center> 
</fx:root> 
+0

Oh daaaamn、私はそれが動作するかどうかを確認し、答えとしてマークします... – GOXR3PLUS

1

メソッドを呼び出す前にContainerクラスを取得するには、FXMLLoader.getController()メソッドを使用する必要があります。代わりにContainerのコンストラクタのSpeechWindowクラスから

FXMLLoader loader = new FXMLLoader(getClass().getResource(".../SpeechWindow.fxml")); 

を呼び出すための

してみてください。そして、あなたはFXMLにコントローラのインスタンスを接続することはありません

Container container = loader.getController(); 
+0

おかげで...このような例外が発生した場合、インスタンスは、使用できない場合は、単純に、負荷例外をキャッチするために悪いアイデアのように思えるが、ここで私は 'fx:root'を使いたいと思っています。私はあなたがFabianの答えで見ることができる何かを見逃しました。 – GOXR3PLUS

+0

fxmlには 'fx:controller'属性が指定されていないので、' FXMLLoader'でコントローラが作成されていないので、この回答はうまくいかないでしょう。 – fabian

関連する問題