2013-06-18 19 views
0

我在嘗試使用netbeans,javFX和MySQL創建Sql語句時遇到了問題。這是我的主要方法:如何將變量從用戶界面傳遞到控制器中

public void start(Stage primaryStage) throws Exception { 
    primaryStage.setTitle("Create Product"); 
    Pane myPane = (Pane)FXMLLoader.load(getClass().getResource 
("createProduct.fxml")); 
    Scene myScene = new Scene(myPane); 
    primaryStage.setScene(myScene); 
    primaryStage.show(); 
} 

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

我使用JavaFX設計我的用戶接口和I整個面板作爲myPane存儲如上述的代碼。這裏是我的createProduct.fxml。它只包含一個文本字段和一個按鈕。

<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml" fx:controller="it2297proj.SampleController"> 

這是我的事件處理程序類。點擊按鈕時,它會將整個面板傳遞到CreateProductController。

public class SampleController implements Initializable { 
private SampleController myPane = null; 

@FXML 
private void handleButtonAction(ActionEvent event) { 
    System.out.println("You clicked me!"); 
} 
@FXML 
private void createProduct(ActionEvent event){ 
CreateProductController controller = new CreateProductController(); 
boolean result = controller.create(myPane); //Error here 
} 

而在我的CreateProductController類中,我只是簡單地獲取文本並將其傳遞給實體。

public class CreateProductController { 
public boolean create(SampleController panel){ 
    String name = panel.getnameTextField.getText(); 
} 
} 

但是,事件處理程序類有一個錯誤,布爾結果= controller.create(myPane);這條線。錯誤消息是創建類型(Sample Controller)類是錯誤的。我不知道爲什麼,因爲它在eclipse中工作正常。任何幫助,將不勝感激。

在此先感謝。

回答

1

目前還不清楚您使用的是哪個版本的JavaFX,以及您正在嘗試做什麼。 JavaFX 2.2使創建自定義控制器非常簡單。您可以直接從FXML或編程方式將參數傳遞給它們。如果您使用2.2版本,則可能會從閱讀Creating a Custom Control with FXML獲益。你正在尋找的答案可能在那裏,尤其是關於FXMLLoaders的部分。

這個thread也許有幫助。

+0

好的,非常感謝。我已經知道了。在場景構建器設計完成後,我沒有點擊製作資源。謝謝 –

相關問題