2016-04-11 85 views
0

這讓我猛撞我的頭靠在牆上!希望有人能指出我的方向。我相信我正在做一些完全愚蠢和愚蠢的事情。我已經搜索和搜索,但似乎無法找出爲什麼這不工作! (在IntelliJ IDE 15.x上使用JDK8.x和Scenebuilder)。我正在嘗試在GUI上顯示數據,但想要通過其他類/方法以編程方式將此數據發送給它......這裏是一個簡單的概念,我正在嘗試在我的大型項目上工作之前先做好工作:從主控制器類的JavaFX訪問功能

Main Class: 

package sample; 

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Main extends Application { 

Controller myController = new Controller(); 

@Override 
public void start(Stage primaryStage) throws Exception{ 
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 
    primaryStage.setTitle("Hello World"); 
    primaryStage.setScene(new Scene(root, 345, 200)); 
    primaryStage.show(); 
    myController.pushBtn(); 
} 

public static void main(String[] args) { 
    launch(args); 
    } 
    } 

我簡單的GUI,在FXML定義的Scenebuilder Controller類:

package sample; 

import java.net.URL; 
import java.util.ResourceBundle; 

import javafx.application.Platform; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.fxml.FXML; 
import javafx.scene.control.Button; 
import javafx.scene.control.TextField; 

public class Controller { 

@FXML // ResourceBundle that was given to the FXMLLoader 
private ResourceBundle resources; 

@FXML // URL location of the FXML file that was given to the FXMLLoader 
private URL location; 

@FXML // fx:id="txtBox" 
private TextField txtBox; // Value injected by FXMLLoader 

@FXML // fx:id="myButton" 
private Button myButton; // Value injected by FXMLLoader 

@FXML // This method is called by the FXMLLoader when initialization is complete 
void initialize() { 
    assert txtBox != null : "fx:id=\"txtBox\" was not injected: check your FXML file 'sample.fxml'."; 
    assert myButton != null : "fx:id=\"myButton\" was not injected: check  our FXML file 'sample.fxml'."; 
} 
@FXML 
private void pressBtn(ActionEvent event){ 
    txtBox.setText("Btn was pushed..."); 
} 

@FXML 
public void pushBtn(){ 
    txtBox.appendText("Sample from Main"); 
} 

} 

..和Finaly我很簡單FXML文件,然後使用ID和事件處理程序代碼一起實施:

<GridPane alignment="center" hgap="10" vgap="10"  xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65"  fx:controller="sample.Controller"> 
    <columnConstraints> 
    <ColumnConstraints /> 
    </columnConstraints> 
    <rowConstraints> 
    <RowConstraints /> 
</rowConstraints> 
<children> 
    <Pane prefHeight="200.0" prefWidth="345.0" style="-fx-background-color: GRAY;"> 
     <children> 
     <TextField fx:id="txtBox" layoutX="63.0" layoutY="28.0"  prefHeight="53.0" prefWidth="224.0" style="-fx-background-color: LIGHTGRAY;" /> 
      <Button fx:id="myButton" layoutX="140.0" layoutY="100.0" mnemonicParsing="false" onAction="#pressBtn" text="Button" /> 
     </children> 
     </Pane> 
    </children> 
</GridPane> 

我想要做的就是調用一個已經在控制器類上聲明爲公共的函數,並讓該函數將文本放置在我已經用FXML定義的txtBox中,如您所見。我在main中創建了該控制器類的一個實例,然後使用該實例訪問此方法。我一直在這裏閱讀和搜索,指出這是正確的做法。但是,這從來沒有工作,而我得到的只是一個錯誤,因此:

Exception in Application start method 
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method 
    at  com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImp l.java:182) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.NullPointerException 
    at sample.Controller.pushBtn(Controller.java:43) 
at sample.Main.start(Main.java:20) 
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherIm pl.java:863) 
at  com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
at  com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at  com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
... 1 more ` 

我錯過了什麼?有沒有另外一種方式來調用它,我是不是正在初始化某些東西?真的很感激任何反饋!

+0

看來你的'txtBox'在'PushBtn'調用空。也許在您嘗試手動調用它時,初始化尚未完成。 –

+0

*「我在main中創建了該控制器類的實例」*。那是這個問題。該實例不是控制器。 –

回答

1

當您致電new Controller()時,您正在創建第二個Controller實例。當然,這並不是Controller實例,它在加載FXML文件時由FXMLLoader創建,因此它不會有任何@FXML注入的字段被初始化。

你需要得到來自FXMLLoaderController實例:

package sample ; 

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Main extends Application { 


    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml")); 
     Parent root = loader.load(); 

     Controller myController = loader.getController(); 

     primaryStage.setTitle("Hello World"); 
     primaryStage.setScene(new Scene(root, 345, 200)); 
     primaryStage.show(); 
     myController.pushBtn(); 

    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

你是一個真正的!非常感謝你選擇了這個!這正是我的愚蠢問題....再次感謝你! – CYBORGCNC