每当我添加新类时,Treeitem都会覆盖。如何解决呢?

在添加新对象时,应动态增加treeitem的尝试:

  • 添加而不使用列表
  • 使用列表添加了
  • 尝试使用循环
  • 添加

    这是一个示例代码,对命名错误表示抱歉。

    用--------标记了发出地点

    PanesClass.java

    public class PanesClass extends Application {
        ObservableList<Connections> cList = FXCollections.observableArrayList();
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @SuppressWarnings("all")@Override
        public void start(Stage primaryStage) throws Exception {
            NewConnection newConnection = new NewConnection();
            SplitPane root = new SplitPane();
            AnchorPane first = new AnchorPane();
            AnchorPane second = new AnchorPane();
            TreeTableView activeConnections = new TreeTableView();
            HBox buttonBox = new HBox();
            BorderPane topBar = new BorderPane();
            Button nConnection = new Button("+");
            Button deleteConnection = new Button("X");
            Button connect = new Button("Connect");
    
            buttonBox.setSpacing(10);
            buttonBox.getChildren().addAll(nConnection, deleteConnection, connect);
            topBar.setTop(buttonBox);
    
            TreeTableColumn<String, Connections > cNameColoumn = new TreeTableColumn<>("Name");
    
            cNameColoumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("cname"));
    
            TreeTableColumn<String, Connections> cStatusColoumn = new TreeTableColumn<>("Status");
            cStatusColoumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("cstatus"));
    
            activeConnections.getColumns().addAll(cNameColoumn, cStatusColoumn);
            activeConnections.setLayoutX(20);
            activeConnections.setLayoutY(40);
            activeConnections.setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY);
    
            first.getChildren().addAll(topBar, activeConnections);
            root.getItems().addAll(first, second);
    
            Scene sc = new Scene(root, 600, 480);
    
            primaryStage.setScene(sc);
            primaryStage.show();
    
            nConnection.setOnAction(new EventHandler<ActionEvent>() {@Override
                public void handle(ActionEvent event) {
                    newConnection.getConnection(activeConnections);
                }
            });
        }
    }
    

    NewConnection.java

    public class NewConnection {
        Connections connection = null;
        ObservableList<Connections> cList = FXCollections.observableArrayList();
        PanesClass panesClass = new PanesClass();
        TreeItem cItem = null;
        TreeItem nItem = null;
    
        public void getConnection(TreeTableView<Connections> activeConnections) {
            Stage secondaryStage = new Stage();
            VBox root = new VBox();
            GridPane cDetails = new GridPane();
            HBox actionButtons = new HBox();
            Button connect = new Button("Connect");
            Button save = new Button("Save");
            Button cancel = new Button("Cancel");
    
            actionButtons.getChildren().addAll(connect, save, cancel);
            actionButtons.setSpacing(10);
    
            Label name = new Label("Username : ");
    
            cDetails.add(name, 0, 0);
    
            TextField uName = new TextField();
    
            cDetails.setHgrow(uName, Priority.ALWAYS);
            cDetails.add(uName, 1, 0);
    
            Label password = new Label("Password : ");
    
            cDetails.add(password, 0, 1);
    
            TextField pwd = new TextField();
    
            cDetails.add(pwd, 1, 1);
    
            Label urllink = new Label("URL : ");
    
            cDetails.add(urllink, 0, 2);
    
            TextField url = new TextField();
    
            cDetails.add(url, 1, 2);
            cDetails.setVgap(10);
            cDetails.setStyle("-fx-padding: 10;" + "-fx-border-style: solid inside;" + "-fx-border-width: 1;" + "-fx-border-insets: 5;" + "-fx-border-radius: 5;" + "-fx-border-color: black;");
            root.getChildren().addAll(cDetails, actionButtons);
    
            Scene sc = new Scene(root, 500, 200);
    
            secondaryStage.setScene(sc);
            secondaryStage.initModality(Modality.APPLICATION_MODAL);
            secondaryStage.show();
    
            save.setOnAction(new EventHandler<ActionEvent>() {
                //*-----------------------------------------------------------------------*
                @Override
                public void handle(ActionEvent event) {
                    cItem = getitem(cItem);
                    activeConnections.setRoot(cItem);
                    activeConnections.setShowRoot(false);
                    secondaryStage.close();
                }
    
                private TreeItem getitem(TreeItem cItem) {
                    cList.add(new Connections(uName.getText()));
                    System.out.println(cList);
    
                    for (Connections temp: cList) {
                        System.out.println(temp);
                        nItem = new TreeItem<Connections>(temp);
                        System.out.println(nItem);
                        cItem.getChildren().add(nItem);
                    }
                    return cItem;
                }
            });
            System.out.println(cList);
        }
    }
    

    连接.java

    public class Connections {
        private String cname = null;
        private String cstatus = null;
        private String cpwd = null;
        private String curl = null;
    
        public Connections() {
    
        }
    
        public Connections(String cname, String cpwd, String curl) {
            super();
            this.cname = cname;
            this.cpwd = cpwd;
            this.curl = curl;
        }
    
        public Connections(String cname, String cstatus) {
            super();
            this.cname = cname;
            this.cstatus = cstatus;
        }
    
        public String getCpwd() {
            return cpwd;
        }
    
        public void setCpwd(String cpwd) {
            this.cpwd = cpwd;
        }
    
        public String getCurl() {
            return curl;
        }
    
        public void setCurl(String curl) {
            this.curl = curl;
        }
    
        public String getCname() {
            return cname;
        }
        public void setCname(String cname) {
            this.cname = cname;
        }
        public String getCstatus() {
            return cstatus;
        }
        public void setCstatus(String cstatus) {
            this.cstatus = cstatus;
        }
    
        @Override
        public String toString() {
            return "Connections [cname=" + cname + ", cstatus=" + cstatus + ", cpwd=" + cpwd + ", curl=" + curl + "]";
        }
    }
    

    最佳答案

    您不是在填充树,而是在创建新项目而不将其添加到树中。
    首先,您需要创建一个根目录:

    // Instead of this line
    // TreeItem nItem = null;
    TreeItem rootItem = new TreeItem();
    

    然后:

    activeConnections.setRoot(rootItem);
    save.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            // clear old connections
            rootItem.getChildren().clear();
    
            // Add new connection
            cList.add(new Connections(uName.getText(), pwd.getText(), url.getText()));
    
            // create new items and add them to rootItem
            for (Connections temp : cList) {
                rootItem.getChildren().add(new TreeItem<Connections>(temp));
            }
    
            secondaryStage.close();
            event.consume();
        }
    });
    

    注意:如果您没有其他原因保留cList,则可以将其删除并直接添加新项目(无需每次都清除和重新生成项目):

    save.setOnAction(event -> {
        Connections newConnection = new Connections(uName.getText(), pwd.getText(), url.getText());
        rootItem.getChildren().add(new TreeItem<>(newConnection));
    
        secondaryStage.close();
        event.consume();
    });
    

    09-25 18:37
    查看更多