2017-02-16 8 views
0

JavaFxを使用しています。実行時にTreeItemをTreeViewで更新する必要があり、メインウィンドウで更新する必要があることがわかりました。ここでユーザーからのデータでJavaFXのTreeViewを更新する方法

、次の2つのウィンドウのスクリーンショットを見ることができます:

enter image description here

大きなメインウィンドウであり、それは小さな新しい、(新規プロジェクト>>ファイルをクリックして)を呼び出します。小さいウィンドウでは、入力された文字列を取得でき、Enterボタンがクリックされました。

問題は次のとおりです。メインウィンドウのTreeView(大きい方)の「新しいプロジェクトウィンドウ」(写真の小さいウィンドウ)で作成された新しいアイテムを表示するにはどうすればよいですか? ツリービューはメインウィンドウの左側にあります。

私は明確だったと思います。ここ は、これらのウィンドウのコントローラのコードです:

package application; 

import java.net.URL; 

import java.util.ResourceBundle; 

import javafx.beans.value.ChangeListener; 
import javafx.event.ActionEvent; 
import javafx.event.Event; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.MenuItem; 
import javafx.scene.control.TreeItem; 
import javafx.scene.control.TreeItem.TreeModificationEvent; 
import javafx.scene.control.TreeView; 
import javafx.stage.Modality; 
import javafx.stage.Stage; 

/** 
* this class handles with the main window of our LDF Tool 
* @author Vinicius 
* @version 1.0 
*/ 
public class MainController implements Initializable{ 
    @FXML 
    TreeView<String> treeView; 
    @FXML 
    MenuItem newProject; 

    private boolean flag = false; 
    private NewProjectWindowController npwc; 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 

    } 


    @FXML 
    public void newProjectClicked(ActionEvent event){ 
     try{ 
      flag = true; 
      FXMLLoader fxml = new FXMLLoader(getClass().getResource("newProjectWindow.fxml")); 
      Parent root = (Parent) fxml.load();   
      Stage newWindow = new Stage(); 
      newWindow.setTitle("New Project"); 
      newWindow.initModality(Modality.APPLICATION_MODAL); 
      newWindow.setScene(new Scene(root)); 
      newWindow.show(); 


     } catch (Exception e) { 
      System.out.println("caiu na exceção"); 
     } 
    } 

    /** 
    * to this method, choose the project's name as argument, and it will be put on the 
    * tree with the archives that should be created together 
    * @param projectName 
    */ 
    public void doTree(String projectName){  
     TreeItem<String> root = new TreeItem<>("projectName"); 
     root.setExpanded(true);  
     //TreeItem<String> folha1 = new TreeItem<String>(projectName + " arquivo 1"); 
     //root.getChildren().add(folha1); 
     treeView.setRoot(root);  

    } 

他のコントローラクラス:

package application; 

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

import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TextField; 
import javafx.stage.Stage; 

public class NewProjectWindowController implements Initializable{ 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 

    } 

    @FXML 
    Button cancelButton; 
    @FXML 
    Button enterButton; 
    @FXML 
    TextField textInput; 
    private String input; 

    public String getInput(){ 
     return this.input; 
    } 

    @FXML 
    public void cancelButtonClicked(ActionEvent event) { 
     Stage window = (Stage) this.cancelButton.getParent().getScene().getWindow(); 
     window.close(); 
    } 

    @FXML 
    public void enterButtonClicked(ActionEvent event) { 
     input = hasString(); 
     Stage window = (Stage) this.enterButton.getParent().getScene().getWindow(); 
     window.close(); 
    } 

    private String hasString(){ 
     if (this.textInput.getText().isEmpty()) 
      return null; 
     return this.textInput.getText(); 
    } 

} 

してください、私はFXMLファイルで[OK]をすべてマッピングされていることを前提としています。 ありがとう

答えて

1
@FXML 
public void newProjectClicked(ActionEvent event){ 
    try{ 
     flag = true; 
     FXMLLoader fxml = new FXMLLoader(getClass().getResource("newProjectWindow.fxml")); 
     Parent root = (Parent) fxml.load();   
     Stage newWindow = new Stage(); 
     newWindow.setTitle("New Project"); 
     newWindow.initModality(Modality.APPLICATION_MODAL); 
     newWindow.setScene(new Scene(root)); 

     // showAndWait blocks execution until the window closes: 
     newWindow.showAndWait(); 

     NewProjectWindowController controller = fxml.getController(); 
     String input = controller.getInput(); 
     if (input != null) { 
      TreeItem<String> currentItem = treeView.getSelectionModel().getSelectedItem(); 
      if (currentItem == null) currentItem = treeView.getRoot(); 
      currentItem.getChildren().add(new TreeItem<>(input)); 
     } 

    } catch (Exception e) { 
     System.out.println("caiu na exceção"); 
    } 
} 
+0

ありがとうございました。しかし、...私はどのように、私のコードは、試して、キャッチを実行しているのか分からない。 oO –

+0

スタックトレースをキャッチに印刷して、例外をスローしているものを見つけ出してください –

関連する問題