2017-02-16 1 views
0

JavaFx를 사용하여 구걸하고 있습니다. 런타임에 TreeItem을 사용하여 TreeView를 업데이트하는 데 도움이 필요하다는 것을 깨달았으며 기본 창에서 업데이트해야합니다. 새로운 작은사용자의 데이터로 JavaFX의 TreeView를 업데이트하는 방법

enter image description here

더 큰 메인 창입니다 그것은 (새 프로젝트 >> 파일을 클릭하여) 전화 : 여기

, 당신은 두 개의 창문의 스크린 샷을 볼 수 있습니다. 작은 창에서 입력 한 문자열을 가져올 수 있으며 입력 버튼을 클릭 한 것입니다.

문제는 : "새 프로젝트 창"(사진의 더 작은 창)으로 만든 새 항목을 기본 창 (더 큰 것)의 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 파일에서 확인 모든 매핑한다고 가정하십시오. 감사합니다.

답변

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

감사합니다. 하지만 ... 어떻게 작동하는지 모르겠다. 내 코드가 try와 catch를 모두 실행하고있다. oO –

+0

그래서 스택 트레이스를 캐치에 출력하고 예외를 던지고있는 것이 무엇인지 알아냅니다. –

관련 문제