java - 如何使用用户数据更新 JavaFX 中的 TreeView

标签 java javafx fxml

我向 JavaFx 求助,我意识到我需要一些帮助来在运行时用一些 TreeItems 更新 TreeView,并且应该在主窗口中更新它。

在这里,您可以看到两个窗口的屏幕截图:

enter image description here

较大的是主窗口,它调用(通过单击“文件>>新建项目”),新的较小。在较小的窗口中,我可以获得输入的字符串,然后单击回车按钮。

问题是:如何在主窗口(较大)的 TreeView 中显示“新项目窗口”(图中较小的窗口)创建的新项目? TreeView 位于主窗口的左侧。

我希望我说得清楚。 以下是这些窗口的 Controller 的代码:

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);     

    }

其他 Controller 类:

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 文件中映射了一切正常。 谢谢

最佳答案

@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");
    }
}

关于java - 如何使用用户数据更新 JavaFX 中的 TreeView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42284060/

相关文章:

java - 如何将状态从 servlet 发送到客户端?

java - 为什么我的程序无法读取完整文件?

java - 关闭 javafx 窗口处理程序

java - java中如何保存从jni返回的void*以供以后使用

java - 我应该把 JUnit 测试放在哪里?

java - 使用 javaFX 对齐文本时遇到问题

JavaFX Choiceox 更改不更新图形

JavaFX:使用 MySQL 数据库中的数据填充组合框,StringConverter 破坏组合框

javafx - pane.getChildren().addAll();不在场景javafx中工作

javafx - 使用 FXML 在 JavaFX 中自定义 ListView