如何使用来自用户的数据更新JavaFX中的TreeView [英] How to Update a TreeView in JavaFX with data from users

查看:488
本文介绍了如何使用来自用户的数据更新JavaFX中的TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在乞求JavaFx,我意识到我需要一些帮助来在运行时更新带有某些TreeItem的TreeView,并且应该在主窗口中对其进行更新.

I am begging with JavaFx, and I realized that I need some help to update a TreeView with some TreeItems in runtime, and it should be updated in the main window.

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

Here, you can see a screenshot of the two windows:

主窗口越大,它调用(通过单击文件>>新建项目),新窗口越小.在较小的窗口中,我可以输入要键入的字符串,然后单击Enter按钮.

The bigger is the main window and it calls (by clicking in File >> New Project), new smaller. In the smaller window, I could get the String that is typed and than the enter button is clicked.

麻烦是:如何在主窗口(较大)的TreeView中显示由新项目窗口"(图片中的较小窗口)创建的新项目? 树状视图位于主窗口的左侧.

The trouble is: How can I show the new items created by the "new project window" (the smaller window in the pic) in the TreeView in the main window(the bigger)? The treeview is in the left side of the main window.

我希望我很清楚. 这是这些窗口的控制器的代码:

I hope I was clear. Here is the code of the controllers of these windows:

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文件中映射了所有内容. 谢谢

Please, assume that I mapped everything ok in the FXML file. thanks

推荐答案

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

这篇关于如何使用来自用户的数据更新JavaFX中的TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆