2014-01-06 12 views
0

我錯過了一些基本的東西。我有一個在類(FirstController.java)中聲明的字符串數組(String [] strArray),然後通過方法初始化。我想從另一個類(SecondController.java)打印這個數組,但返回的數組是空的。請讓我明白爲什麼會發生這種情況。謝謝!請看下面的代碼(禮貌James_D):從哪裏的String [] strArray字符串數組類型變量的值未得到更新(從另一個類引用時返回空)。 JavaFX

FirstController.java(字符串[] strArray聲明和這裏的初始化)

public class FirstController { 

    private Stage Stage2; 
    public String[] strArray; // this is the string array in question. 

    @FXML 
    private Button openStage2; 
    @FXML 
    private TextArea textArea1; 

    @FXML 
    private void openStage2Action(ActionEvent event) throws IOException { 
     Stage2 = new Stage(); 

     FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Second.fxml")); 
     Object root = fxmlLoader.load(); 
     Scene scene = new Scene((Parent) root); 
     Stage2.setScene(scene); 
     SecondController secondController = (SecondController)fxmlLoader.getController(); 
     secondController.textProperty().addListener(new ChangeListener<String>() { 
      @Override 
      public void changed(ObservableValue<? extends String> obs, String oldValue, String newValue) { 
       textArea1.setText(newValue); 
       strArray = newValue.split("\n"); // here the string array is initialized. 
       // this string array gets printed by this 
       for(String s:strArray){ 
        System.out.println(s); 
       } 
      } 
     }); 
     Stage2.show(); 
    } 

    //public String[] getStrArray(){ 
    // return strArray; 
    //} 

} 

SecondController.java( 「其他類」結果如空的,打印時)

public class SecondController { 
    private StringProperty text = new SimpleStringProperty(this, "text", "");; 
    FirstController firstController = new FirstController(); 

    @FXML 
    private TextArea textArea2 ; 
    public StringProperty textProperty() { 
    return text ; 
    } 
    public final String getText2() { 
    return text.get(); 
    } 
    public final void setText(String text) { 
    this.text.set(text); 
    } 

    // ... 

    @FXML 
    private void showTextAction(ActionEvent event) { 
    text.set(textArea2.getText()); 
    System.out.println(firstController.strArray); // this line prints empty array [] 

    System.out.println(firstController.strArray.toString()); // this will result NullPointerException because the array is empty. But why it is empty? 
    } 
} 

First.java(應用類):

public class First extends Application { 
    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("First.fxml")); 
     Scene scene = new Scene(root); 
     stage.setScene(scene); 
     stage.show(); 
    } 
    public static void main(String[] args) {launch(args);} 
} 

回答

2

因此,它看起來像SecondController需要訪問從其他地方填充的某種字符串數組。只需在SecondController中定義一個ObservableList並給它一個get(...)方法即可。

public class SecondController { 
    private StringProperty text = new SimpleStringProperty(this, "text", ""); 
    // Remove any reference to FirstController 
    //FirstController firstController = new FirstController(); 
    private ObservableList<String> strArray = FXCollections.observableArrayList(); 

    @FXML 
    private TextArea textArea2 ; 
    public StringProperty textProperty() { 
    return text ; 
    } 
    public final String getText2() { 
    return text.get(); 
    } 
    public final void setText(String text) { 
    this.text.set(text); 
    } 
    public ObservableList<String> getStrArray() { 
    return strArray ; 
    } 

    // ... 

    @FXML 
    private void showTextAction(ActionEvent event) { 
    text.set(textArea2.getText()); 
    System.out.println(firstController.strArray); // this line prints empty array [] 

    System.out.println(firstController.strArray.toString()); // this will result NullPointerException because the array is empty. But why it is empty? 
    } 
} 
在FirstController

現在:

@FXML 
private void openStage2Action(ActionEvent event) throws IOException { 
    Stage2 = new Stage(); 

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Second.fxml")); 
    Object root = fxmlLoader.load(); 
    Scene scene = new Scene((Parent) root); 
    Stage2.setScene(scene); 
    SecondController secondController = (SecondController)fxmlLoader.getController(); 
    secondController.textProperty().addListener(new ChangeListener<String>() { 
     @Override 
     public void changed(ObservableValue<? extends String> obs, String oldValue, String newValue) { 
      textArea1.setText(newValue); 
      strArray = newValue.split("\n"); // here the string array is initialized. 
      // this string array gets printed by this 
      for(String s:strArray){ 
       System.out.println(s); 
      } 
     } 
    }); 
    secondController.getStrArray().setAll(...); // or addAll(...) as needed. 
    Stage2.show(); 
} 
+0

非常感謝!我所理解的是處理可觀察屬性更好(而不是實例化類或UI元素)。您的解決方案奏效 – Vaib

+0

是的:您不希望將您的「內部」控制器與任何特定上下文耦合,因爲這會降低重用的機會。而且您不想公開UI組件:您可能想稍後更改UI,例如用ComboBoxes替換TextField,或其他。如果其他類依賴於特定的設計,這變得不可能。 一般來說,考慮一個班級需要的數據並從那裏開始工作。 –

相關問題