2016-10-30 3 views
1

클래스와 중첩 된 JavaFX Controller이 포함 된 아래 코드가 있습니다. JavaFX 컨트롤러 클래스를 초기화하고 볼 수있는 것처럼 인스턴스를 가져옵니다.공용 클래스 내의 중첩 된 JavaFX 컨트롤러

SpeechWindow 클래스 : SpeechWindow의 인스턴스를 생성하는 기본 자바 FX 애플리케이션 클래스에서 호출하는

<?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); 
     } 

    } 

} 

IT는 FXML 코드입니다 그리고 appendText(); 메쏘드를 호출하십시오. 널 포인터가 나오는 이유는 무엇입니까?

목표는 다음과 같습니다

그런 다음 내가 외부 클래스 인스턴스를 생성하고 그것을 사용하려고합니다.


간다 무엇이 잘못 : 내가 방법 appendText();SpeechWindow 클래스의 인스턴스를 생성하고 호출하는 경우

내가 그런 일이 왜, 내가 모든 것을 해 괜찮아가 완료보고 ... 널 포인터 예외가 내 실수?

답변

2

하여 Container 클래스를 retreive.

컨트롤러 클래스를 설정하려면 FXMLLoadersetController을 사용하십시오.

또한 fxml을 어떻게 든 장면에로드 한 결과를 추가해야합니다. 아마도 fxml의 루트 요소를 fx:root 요소로 대체해야합니다.

은 BTW : ... 단순히 이러한 예외가 발생하는 경우 인스턴스가 사용할 수없는 경우, 예외 부하를 잡기 위해 나쁜 생각처럼 노력 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 내가 Fabian에게 감사 드려요! 나는 그것이 작동하는지 확인한 다음 대답으로 표시 할 것입니다 ... – GOXR3PLUS

1

메서드를 호출하기 전에 Container 클래스를 얻으려면 FXMLLoader.getController() 메서드를 사용해야합니다.

봅니다 SpeechWindow 클래스 대신 Container의 생성자에서

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

를 호출합니다. 그런 다음 당신은 fxml에 컨트롤러 인스턴스를 연결하지

Container container = loader.getController(); 
+0

감사를 보인다 여기'fx : root'를 사용하고 싶습니다. Fabian의 답변에서 볼 수있는 것을 놓쳤습니다. – GOXR3PLUS

+0

fxml에 지정된'fx : controller' 속성이 없기 때문에 어쨌든이 응답은'FXMLLoader'에 의해 생성 된 컨트롤러가 없으므로 작동하지 않아야합니다 ... – fabian